Skip to content

Commit 343ca78

Browse files
Add some configurability to the generated documentation.
1 parent a76d706 commit 343ca78

File tree

2 files changed

+71
-53
lines changed

2 files changed

+71
-53
lines changed

README.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ end
3535

3636
To explore your API, either download [Swagger UI](https://github.com/wordnik/swagger-ui) and set it up yourself or go to the [online swagger demo](http://petstore.swagger.wordnik.com/) and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc.json)
3737

38+
## Configure
39+
You can pass a hash with some configuration possibilities to ```add_swagger_documentation```, all of these are optional:
40+
* ```:mount_path``` The path were the API documentation is loaded, default '/swagger_doc'
41+
* ```:api_version``` Version of the API that's being exposed
42+
* ```:base_path``` Basepath of the API that's being exposed
43+
3844
## Contributing to grape-swagger
3945

4046
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.

lib/grape-swagger.rb

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,82 @@ def mount(mounts)
1414
def add_swagger_documentation(options={})
1515
documentation_class = create_documentation_class
1616

17-
documentation_class.setup(self)
17+
documentation_class.setup({:target_class => self}.merge(options))
1818
mount(documentation_class)
1919
end
2020

2121
private
2222

23-
def create_documentation_class(options={})
23+
def create_documentation_class
2424
Class.new(Grape::API) do
2525
class << self
2626
def name
27-
'swagger_doc'
27+
@@class_name
2828
end
2929
end
3030

31-
def self.setup(target_class)
32-
@@target_class = target_class
31+
def self.setup(options)
32+
defaults = {
33+
:target_class => nil,
34+
:mount_path => '/swagger_doc',
35+
:base_path => nil,
36+
:api_version => '0.1',
37+
}
38+
options = defaults.merge(options)
39+
40+
@@target_class = options[:target_class]
41+
@@mount_path = options[:mount_path]
42+
@@class_name = options[:class_name] || options[:mount_path].gsub('/','')
43+
@@api_version = options[:api_version]
44+
@@base_path = options[:base_path]
45+
46+
desc 'Swagger compatible API description'
47+
get @@mount_path do
48+
header['Access-Control-Allow-Origin'] = '*'
49+
header['Access-Control-Request-Method'] = '*'
50+
routes = @@target_class::combined_routes
51+
52+
routes_array = routes.keys.map do |route|
53+
{ :path => "#{@@mount_path}/#{route}.{format}" }
54+
end
55+
{
56+
apiVersion: @@api_version,
57+
swaggerVersion: "1.1",
58+
basePath: @@base_path || "http://#{env['HTTP_HOST']}",
59+
operations:[],
60+
apis: routes_array
61+
}
62+
end
63+
64+
desc 'Swagger compatible API description for specific API', :params =>
65+
{
66+
"name" => { :desc => "Class name of mounted API", :type => "string", :required => true },
67+
}
68+
get "#{@@mount_path}/:name" do
69+
header['Access-Control-Allow-Origin'] = '*'
70+
header['Access-Control-Request-Method'] = '*'
71+
routes = @@target_class::combined_routes[params[:name]]
72+
routes_array = routes.map do |route|
73+
{
74+
:path => parse_path(route.route_path),
75+
:operations => [{
76+
:notes => route.route_notes,
77+
:summary => route.route_description || '',
78+
:nickname => Random.rand(1000000),
79+
:httpMethod => route.route_method,
80+
:parameters => parse_params(route.route_params)
81+
}]
82+
}
83+
end
84+
85+
{
86+
apiVersion: @@api_version,
87+
swaggerVersion: "1.1",
88+
basePath: @@base_path || "http://#{env['HTTP_HOST']}",
89+
resourcePath: "",
90+
apis: routes_array
91+
}
92+
end
3393
end
3494

3595

@@ -57,54 +117,6 @@ def parse_path(path)
57117
parsed_path.gsub(/:([a-z]+)/, '{\1}')
58118
end
59119
end
60-
61-
desc 'Swagger compatible API description'
62-
get '/swagger_doc' do
63-
header['Access-Control-Allow-Origin'] = '*'
64-
header['Access-Control-Request-Method'] = '*'
65-
routes = @@target_class::combined_routes
66-
67-
routes_array = routes.keys.map do |route|
68-
{ :path => "/swagger_doc/#{route}.{format}" }
69-
end
70-
{
71-
apiVersion: "0.2",
72-
swaggerVersion: "1.1",
73-
basePath: "http://#{env['HTTP_HOST']}",
74-
operations:[],
75-
apis: routes_array
76-
}
77-
end
78-
79-
desc 'Swagger compatible API description for specific API', :params =>
80-
{
81-
"name" => { :desc => "Class name of mounted API", :type => "string", :required => true },
82-
}
83-
get '/swagger_doc/:name' do
84-
header['Access-Control-Allow-Origin'] = '*'
85-
header['Access-Control-Request-Method'] = '*'
86-
routes = @@target_class::combined_routes[params[:name]]
87-
routes_array = routes.map do |route|
88-
{
89-
:path => parse_path(route.route_path),
90-
:operations => [{
91-
:notes => route.route_notes,
92-
:summary => route.route_description || '',
93-
:nickname => Random.rand(1000000),
94-
:httpMethod => route.route_method,
95-
:parameters => parse_params(route.route_params)
96-
}]
97-
}
98-
end
99-
100-
{
101-
apiVersion: "0.2",
102-
swaggerVersion: "1.1",
103-
basePath: "http://#{env['HTTP_HOST']}",
104-
resourcePath: "",
105-
apis: routes_array
106-
}
107-
end
108120
end
109121
end
110122
end

0 commit comments

Comments
 (0)