/books-api

A HAL/JSON API for books

Primary LanguageRuby

Books API

Welcome to the Books API, an API for storing a collection of books with associated authors and series. The API endpoints are documented as cucumber tests in the features folder. An example-application is running on Heroku.

Note: since this is just a trial-app, passwords are not hashed in the database.

Installation

The Books API has a few dependencies. First you need Ruby 2.3.3 installed

Secondly you can install all required gems with Bundler by running bundle install.

Finally you need a database, such as Postgres. If you're on a mac you can install it with brew install postgres.

Configuration

Once all dependencies are installed there are a few things you need to configure. The Books API uses environment variables for configuration. Two need to be set:

  • DATABASE_URL: this should be a database url that ActiveRecord can understand. If you're running a local instance of Postgres this could be postgres://localhost/books-api.

  • AUTH_SECRET: this is used to generate JWT tokens and should be set to a random string. One can be generated by following these instructions.

  • TEST_DATABASE_URL: in order to run the tests without disrupting another database, you can set TEST_DATABASE_URL to another database that is then used for tests. This database is cleared with each run of the tests.

Once setup is done you can create a database. For Postgres this can be done with createdb DBNAME and then you can migrate with rake db:migrate. Make sure your DATABASE_URL matches the database you created and the permissions are set up correctly. See your database's documentation for this.

There is some seed data included, which can be loaded into the database with irb db/seed.

Testing

First make sure you have TEST_DATABASE_URL setup as described above, then you should run createdb TESTDBNAME (if using Postgres) and rake db:migrate ENV=test to setup the database for the test environment. This all only has to be done once.

Tests for the API are written in cucumber and can be run with:

cucumber

The code is linted with Rubocop, which can be run with:

rubocop

Running

There a few ways to run the API. The simplest is by doing

bin/server

which starts a WebMachine server. If you're using rack, there is a rack config. This is useful for Heroku where this starts the server out of the box (no Procfile needed).

Client

A simple CLI client is included with the app. It can be run using:

bin/client

The client includes its own help text which you see just by running the bare command. The interface is split into subcommands based on the resources exposed. For instance

bin/client books list

Lists all books in the API.

By default the CLI connects with a local running instance of the API at http://localhost:8080. You can change this with the --server flag. For example:

bin/client books list --server https://stormy-earth-20973.herokuapp.com