Skip to content

Configurable Fake API

astranchet edited this page Dec 19, 2012 · 3 revisions

Configurable Fake API

TeaBook Open Reader uses an API to connect the reader to a server.

For development purpose, it's easier to use a fake API to simulate the exchanges. This fake API can be run in two ways:

  • always yes mode which always returns correct datas
  • configurable mode

Configuration

The configuration file is located at config/mock_api/config.yml. Here is a sample of file:

:yesapi: false
:default:
  :authentication: "config/mock_api/authentication.json"
  :forbidden:      "config/mock_api/forbidden.json"
  :publications:   "config/mock_api/publications.json"
  :download:       "config/mock_api/epubs/example.epub"

:users:
  -
    :id: 1
    :email: johndoe@example.com
  -
    :id: 2
    :email: test2@example.com

Always yes mode

To activate this mode, the configuration file is as simple as:

:yesapi: true
:default:
  :authentication: "config/mock_api/authentication.json"
  :forbidden:      "config/mock_api/forbidden.json"
  :publications:   "config/mock_api/publications.json"
  :download:       "config/mock_api/epubs/example.epub"

Any credentials can be used to log on the reader as one specific user. The fake API will use default responses define given files.

See how to configure those files.

Configurable mode

The configurable mode (yesapi set to false) allows to easily setup different user profiles :

:yesapi: false
:users:
  -
    :id: 1
    :email: johndoe@example.com
    :password: ilovejane
  -
    :id: 2
    :email: test2@example.com

In this example, you can use one of the two accounts to log in :

  • johndoe@example.com / ilovejane
  • test2@example.com with any password.

For each user, configuration files are defined in their specific directory:

config/mock_api/
    1/
        authentication.json
        publications.json
    2/
        authentication.json
        publications.json

How it works: The fake API tries to match the received email/password couple to the ones defined in config/mock_api/config.yml. It responds using the matching configuration :

  • config/mock_api/<user_id>/authentication.json
  • config/mock_api/<user_id>/publications.json

See how to configure those files.

Configuration files

Authentication

This file should be of the form of :

{
  "user": {
    "id": "14",
    "email": "johndoe@host",
    "firstname": "John",
    "lastname": "Doe",
    "birthdate": "1980-07-30T01:00:00+01:00",
    "country": "FR",
    "bookstore": "yyy"
  }, 
  "session":{
    "id": "ec115cd99c07e710413b01bc2799e026",
    "expire":1342793273
  }  
}

See API#wiki-authentication

Publications

[
  {
    "id": "<ebook_id>",
    "bookstore_id": "yyy",
    "title": "Ebook 1",
    "subtitle": "Ebook 1 subtitle",
    "authors": [
      {"first_name": "William", "last_name": "Shakespeare", "role": "role 1", "main": true },
      {"first_name": "Honoré", "last_name": "de Balzac", "role": "role 2", "main": false }
    ],
    "format": "epub", // "epub" | "pdf"
    "language": "fr",
    "license": "none", # "none" | "watermarking" | "drm"
    "publisher": {
      "publisher_name": "Publisher name 1",
      "collection": "Collection 1",
      "publication_date": "2011-07-30T01:00:00+01:00"
    },
    "purchase": {
      "type": "bought", // "bought" | "free" | "sample" | "user uploaded"
      "date": "2011-07-30T01:00:00+01:00"
    },
    "reading_position": {
      "epub_cfi": "#chapter-42",
      "updated_at": "2012-06-14T15:03:21+02:00"
    },
    "descriptions": [
      {  
        "type": "summary",
        "content": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
      }  
    ],
    "cover": "http://example.com/cover.jpg"
  }
]

See API#wiki-collections

For the fake API, all physical ebooks must be located in config/mock_api/epubs/<ebook_id>.epub

More informations

To have more details about how the fake API works, you can read the source code in the file lib/tea_api.rb.

Clone this wiki locally