Skip to content

Commit

Permalink
Merge branch 'namespace_description'
Browse files Browse the repository at this point in the history
This is on top of pull request ruby-grape#105.

Conflicts:
	lib/grape-swagger.rb
  • Loading branch information
Renier Morales committed Jun 6, 2014
2 parents 50e968c + 19eed77 commit 62a6cfa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Grape
class API
class << self
attr_reader :combined_routes
attr_reader :combined_routes, :combined_namespaces

def add_swagger_documentation(options={})
documentation_class = create_documentation_class
Expand All @@ -26,6 +26,14 @@ def add_swagger_documentation(options={})
@combined_routes[resource] << route
end
end

@combined_namespaces = {}

endpoints.each do |endpoint|
ns = endpoint.settings.stack.last[:namespace]
@combined_namespaces[ns.space] = ns if ns
end

end

private
Expand Down Expand Up @@ -85,6 +93,7 @@ def self.setup(options)
header['Access-Control-Request-Method'] = '*'

routes = target_class::combined_routes
spaces = target_class::combined_namespaces

if @@hide_documentation_path
routes.reject!{ |route, value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
Expand All @@ -95,10 +104,11 @@ def self.setup(options)

url_base = parse_path(route.route_path.gsub('(.:format)', ''), route.route_version) if include_base_url
url_format = '.{format}' unless @@hide_format
{
:path => "/#{local_route}#{url_format}",
:description => "Operations about #{local_route.pluralize}"
}

api = { :path => "#{url_base}/#{local_route}#{url_format}" }
description = spaces[local_route] && spaces[local_route].options[:desc]
api[:description] = description if description
api
end.compact

output = {
Expand Down
4 changes: 4 additions & 0 deletions spec/grape-swagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
Grape::API.should respond_to :combined_routes
end

it "added combined-namespaces" do
Grape::API.should respond_to :combined_namespaces
end

it "added add_swagger_documentation" do
Grape::API.should respond_to :add_swagger_documentation
end
Expand Down
25 changes: 25 additions & 0 deletions spec/non_default_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,29 @@ def app; SimpleJSONFormattedAPI; end
end

end

context "documented namespace description" do
before :all do
class NamespaceWithDescAPI < Grape::API
namespace :aspace, desc: "Description for aspace" do
desc 'This gets something.'
get '/something' do
{ bla: 'something' }
end
end
add_swagger_documentation format: :json
end
get '/swagger_doc'
end

def app; NamespaceWithDescAPI; end

subject do
JSON.parse(last_response.body)['apis'][0]
end

it "shows the namespace description in the json spec" do
expect(subject['description']).to eql('Description for aspace')
end
end
end

0 comments on commit 62a6cfa

Please sign in to comment.