/grape-rack-roar-swagger

Grape API on Rack + Roar + Swagger (Bonus example with multipart upload)

Primary LanguageRuby

Todo API

Build Status Code Climate

This is an example application showing how to use grape on Rack to create a simple API using ROAR along with grape-swagger. This sample shows how to create a simple API without authentication, caching, etc.

Environment

The sample was developed using the following

  1. JRuby 1.17.19
  2. OS X 10.10.2
  3. Rack
  4. grape
  5. ROAR
  6. grape-swagger

Setup

Gem installation

bundle install --path .bundle/gems --binstubs .bundle/bin

Create and migrate

bundle exec rake db:migrate
bundle exec rake db:migrate RACK_ENV=test

Seed data

bundle exec rake db:seed

Testing

bundle exec rake spec

Run It

Start the server

bundle exec puma

Usage

Getting all todos

curl -i http://localhost:9292/v1/todos

Getting all todos (include todo items)

curl -i http://localhost:9292/v1/todos?type=all

Getting a single todo

curl -i http://localhost:9292/v1/todos/1

Getting a single todo (include todo items)

curl -i http://localhost:9292/v1/todos/1?type=all

Getting the items for a todo

curl -i http://localhost:9292/v1/todos/1/items

Failure to get an item

curl -i http://localhost:9292/v1/items/999

Upload a binary file

curl -X POST -i -F binary_file=@packers.png http://localhost:9292/v1/binary

Swagger Support

First, install Swagger UI somewhere locally.

git clone https://github.com/wordnik/swagger-ui.git

Make sure the app server is running

bundle exec puma

From Chrome or Safari, open the locally installed Swagger swagger-ui/dist/index.html file. In the text field that currently lists http://petstore.swagger.wordnik.com/api/api-docs, change this to http://localhost:9292/swagger_doc

From here, you'll be able to explore the API through the Swagger UI.