Skip to content

Commit 8a052ed

Browse files
author
Tim Vandecasteele
committed
Allow procs in basepath
1 parent 6bc27be commit 8a052ed

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Added Rails 4 support - [@jrhe](https://github.com/jrhe).
44
* Fix: document APIs at root level - [@dblock](https://github.com/dblock).
5+
* Added support for procs in basepath - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
56

67
### 0.5.0 (March 28, 2013)
78

lib/grape-swagger.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def self.setup(options)
7070
{
7171
apiVersion: api_version,
7272
swaggerVersion: "1.1",
73-
basePath: base_path || request.base_url,
73+
basePath: parse_base_path(base_path, request),
7474
operations:[],
7575
apis: routes_array
7676
}
@@ -105,7 +105,7 @@ def self.setup(options)
105105
{
106106
apiVersion: api_version,
107107
swaggerVersion: "1.1",
108-
basePath: base_path || request.base_url,
108+
basePath: parse_base_path(base_path, request),
109109
resourcePath: "",
110110
apis: routes_array
111111
}
@@ -190,6 +190,10 @@ def strip_heredoc(string)
190190
indent = string.scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
191191
string.gsub(/^[ \t]{#{indent}}/, '')
192192
end
193+
194+
def parse_base_path(base_path, request)
195+
(base_path.is_a?(Proc) ? base_path.call(request) : base_path) || request.base_url
196+
end
193197
end
194198
end
195199
end

spec/non_default_api_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,34 @@ def app; SimpleApiWithBasePath end
3131
end
3232
end
3333

34+
context "overruling the basepath with a proc" do
35+
before(:all) do
36+
class ProcBasePathMountedApi < Grape::API
37+
desc 'this gets something'
38+
get '/something' do
39+
{:bla => 'something'}
40+
end
41+
end
42+
43+
class SimpleApiWithProcBasePath < Grape::API
44+
mount ProcBasePathMountedApi
45+
add_swagger_documentation :base_path => lambda { |request| "#{request.base_url}/some_value" }
46+
end
47+
end
48+
49+
def app; SimpleApiWithProcBasePath end
50+
51+
it "retrieves the given base-path on /swagger_doc" do
52+
get '/swagger_doc'
53+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org/some_value\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/something.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
54+
end
55+
56+
it "retrieves the same given base-path for mounted-api" do
57+
get '/swagger_doc/something'
58+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org/some_value\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>\"GET-something---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"
59+
end
60+
end
61+
3462
context "overruling the version" do
3563
before(:all) do
3664
class ApiVersionMountedApi < Grape::API

0 commit comments

Comments
 (0)