Skip to content

Instantly share code, notes, and snippets.

@jhollinger
Created November 12, 2014 14:52
Show Gist options
  • Save jhollinger/b1b64357a9fc8caec3c2 to your computer and use it in GitHub Desktop.
Save jhollinger/b1b64357a9fc8caec3c2 to your computer and use it in GitHub Desktop.
Sample Grape/Swagger Cat API
require './main'
run API::Root
require 'bundler/setup'
Bundler.require(:default)
module API
module Entities
class Pajamas < Grape::Entity
expose :id, documentation: {type: 'integer'}
expose :color, documentation: {type: 'string', desc: 'The color'}
end
class Cat < Grape::Entity
expose :id, documentation: {type: 'integer'}
expose :name, documentation: {type: 'string', desc: "The cat's name"}
expose :pajamas, using: API::Entities::Pajamas,
documentation: {type: 'API::Pajamas', desc: "The cat's pajamas"}
end
end
class Cats < Grape::API
version 'v1'
get :cats,
http_codes: [[200, 'Ok', API::Entities::Cat]] do
present [], with: API::Entities::Cat
end
end
class Root < Grape::API
before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
end
mount API::Cats
add_swagger_documentation
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment