Skip to content

sebastian-nanek/api-example

Repository files navigation

A demonstration application showing how to create a thorougely tested API using Ruby on Rails and a bunch of other technologies, including:

  • grape as domain specific language for API creation

  • ActiveModel::Serializers as presentation layer for API responses

  • RSpec to provide test suite

  • VCR with Webmock to pre-record and then simulate requests to external websites

  • Redis for data caching

  • Rack::Throttle to 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).

About

Demonstration repository for building APIs with Grape and ActiveModel::Serializers (and RSpec!)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors