Skip to content

Commit

Permalink
Move ThisApi to shared context
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordnibbler committed Aug 1, 2016
1 parent daeccf7 commit 32bbbcf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
65 changes: 2 additions & 63 deletions spec/grape-swagger/entities/response_model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,8 @@
require 'spec_helper'
require_relative '../../../spec/support/shared_contexts/this_api'

describe 'responseModel' do
before :all do
module ThisApi
module Entities
class Kind < Grape::Entity
expose :title, documentation: { type: 'string', desc: 'Title of the kind.' }
end

class Relation < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end
class Tag < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end
class Error < Grape::Entity
expose :code, documentation: { type: 'string', desc: 'Error code' }
expose :message, documentation: { type: 'string', desc: 'Error message' }
end

class Something < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
expose :colors, documentation: { type: 'string', desc: 'Colors', is_array: true }
expose :kind, using: Kind, documentation: { type: 'ThisApi::Kind', desc: 'The kind of this something.' }
expose :kind2, using: Kind, documentation: { desc: 'Secondary kind.' }
expose :kind3, using: ThisApi::Entities::Kind, documentation: { desc: 'Tertiary kind.' }
expose :tags, using: ThisApi::Entities::Tag, documentation: { desc: 'Tags.', is_array: true }
expose :relation, using: ThisApi::Entities::Relation, documentation: { type: 'ThisApi::Relation', desc: 'A related model.' }
end
end

class ResponseModelApi < Grape::API
format :json
desc 'This returns something',
is_array: true,
http_codes: [{ code: 200, message: 'OK', model: Entities::Something }]
get '/something' do
something = OpenStruct.new text: 'something'
present something, with: Entities::Something
end

# something like an index action
desc 'This returns something or an error',
entity: Entities::Something,
http_codes: [
{ code: 200, message: 'OK', model: Entities::Something },
{ code: 403, message: 'Refused to return something', model: Entities::Error }
]
params do
optional :id, type: Integer
end
get '/something/:id' do
if params[:id] == 1
something = OpenStruct.new text: 'something'
present something, with: Entities::Something
else
error = OpenStruct.new code: 'some_error', message: 'Some error'
present error, with: Entities::Error
end
end

add_swagger_documentation
end
end
end
include_context 'this api'

def app
ThisApi::ResponseModelApi
Expand Down
65 changes: 65 additions & 0 deletions spec/support/shared_contexts/this_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
shared_context 'this api' do
before :all do
module ThisApi
module Entities
class Kind < Grape::Entity
expose :title, documentation: { type: 'string', desc: 'Title of the kind.' }
end

class Relation < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end
class Tag < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end
class Error < Grape::Entity
expose :code, documentation: { type: 'string', desc: 'Error code' }
expose :message, documentation: { type: 'string', desc: 'Error message' }
end

class Something < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
expose :colors, documentation: { type: 'string', desc: 'Colors', is_array: true }
expose :kind, using: Kind, documentation: { type: 'ThisApi::Kind', desc: 'The kind of this something.' }
expose :kind2, using: Kind, documentation: { desc: 'Secondary kind.' }
expose :kind3, using: ThisApi::Entities::Kind, documentation: { desc: 'Tertiary kind.' }
expose :tags, using: ThisApi::Entities::Tag, documentation: { desc: 'Tags.', is_array: true }
expose :relation, using: ThisApi::Entities::Relation, documentation: { type: 'ThisApi::Relation', desc: 'A related model.' }
end
end

class ResponseModelApi < Grape::API
format :json
desc 'This returns something',
is_array: true,
http_codes: [{ code: 200, message: 'OK', model: Entities::Something }]
get '/something' do
something = OpenStruct.new text: 'something'
present something, with: Entities::Something
end

# something like an index action
desc 'This returns something or an error',
entity: Entities::Something,
http_codes: [
{ code: 200, message: 'OK', model: Entities::Something },
{ code: 403, message: 'Refused to return something', model: Entities::Error }
]
params do
optional :id, type: Integer
end
get '/something/:id' do
if params[:id] == 1
something = OpenStruct.new text: 'something'
present something, with: Entities::Something
else
error = OpenStruct.new code: 'some_error', message: 'Some error'
present error, with: Entities::Error
end
end

add_swagger_documentation
end
end
end
end

0 comments on commit 32bbbcf

Please sign in to comment.