Skip to content

Proposal: Implementable Documentation #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ module Versioner
module Util
autoload :HashStack, 'grape/util/hash_stack'
end

module Meta
autoload :Description, 'grape/meta/description'
autoload :Response, 'grape/meta/response'
end
end

require 'grape/version'
43 changes: 43 additions & 0 deletions lib/grape/meta/description.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Grape
module Meta
# An object containing descriptive metadata for an
# endpoint.
class Description
def initialize(summary = nil, data = {})
@summary = summary
@data = data
@responses = []
@success_response = nil
@failure_response = {}
end

def detail(detail = false)
@detail = detail if detail
@detail = nil if detail.nil?
@detail
end

def summary(summary = false)
@summary = summary if summary
@summary = nil if summary.nil?
@summary
end

def response(entity, status, description)
response = Response.new(entity, status, description)
@responses << response
response
end

def success(entity, status, description)
@success_response = response(entity, status, description)
end

def failure(name, status, description)
failure_response = Response.new(Grape::ErrorEntity.new(name, status, description), status, description)
response failure_response
@failure_responses[name.to_sym] = failure_response
end
end
end
end
11 changes: 11 additions & 0 deletions lib/grape/meta/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Grape
module Meta
class Response
def initialize(entity_class, status = 200, description = nil)
@entity_class = entity
@status = status
@description = description
end
end
end
end
24 changes: 24 additions & 0 deletions spec/grape/meta/description_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe Grape::Meta::Description do
subject { Grape::Meta::Description.new }

describe '#detail' do
it 'should set when a value is provided' do
subject.detail 'this is detail'
expect(subject.detail).to eq('this is detail')
end

it 'should blank when nil is provided' do
subject.detail 'foo'
subject.detail nil
expect(subject.detail).to eq(nil)
end
end

describe '#summary' do
it 'should be settable in the constructor' do
expect(Grape::Meta::Description.new('foo').summary).to eq('foo')
end
end
end