A demonstration application showing how to create a thorougely tested API using Ruby on Rails and a bunch of other technologies, including:
-
grapeas domain specific language for API creation -
ActiveModel::Serializersas presentation layer for API responses -
RSpecto provide test suite -
VCRwith Webmock to pre-record and then simulate requests to external websites -
Redisfor data caching -
Rack::Throttleto limit API queries processed from given client
Please note that all test suites, except integration test (sometimes called “ acceptance”) are done in unit style, this testing the implementation, rather than input-output pairs in black-box style. Integration tests on the other hand, provide across-the-stack tests for the application, including database queries and calls to external webservice.
Additionally, entire business logic is encapsulated in Query and Service objects to provide an easy way to re-use them in another context (i.e. standard Rails controllers). That also allows the developer to provide more thorough test suite, thanks to huge case isolation (i.e. more combinations are tested by 3 layers of tests each containing 3 cases, than 3 tests going through all layers).
Set up environment variables POSTGRES_USERNAME and POSTGRES_PASSWORD to your PostgreSql credentials (no need to alter database.yml), and then:
bundle install rake db:create rake db:migrate rake db:seed rake db:migrate RAILS_ENV=test rails s
et voila! (don’t forget to start Redis in background, if you don’t run it as a daemon).