Skip to content

Commit 933c724

Browse files
committed
Storing additional information.
1 parent 51fb5c4 commit 933c724

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

README.markdown

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ you simply use the `rescue_from` method inside your API declaration:
8585

8686
## Inspecting an API
8787

88-
Grape exposes arrays of API versions and compiled routes. Each route contains a prefix, version, namespace, method and path.
88+
Grape exposes arrays of API versions and compiled routes. Each route contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`, `route_path` and `route_params`.
8989

9090
class TwitterAPI < Grape::API
9191

@@ -107,16 +107,16 @@ Grape exposes arrays of API versions and compiled routes. Each route contains a
107107
TwitterAPI::routes # yields an array of Grape::Route objects
108108
TwitterAPI::routes[0].route_version # yields 'v1'
109109

110-
Grape also supports storing additional options with the route information. This can be useful for generating documentation.
111-
The optional hash that follows the API path may contain any number of keys and values are accessible via `route_[name]`.
110+
Grape also supports storing additional parameters with the route information. This can be useful for generating documentation. The optional hash that follows the API path may contain any number of keys and its values are also accessible via a dynamically-generated `route_[name]` function.
112111

113-
class StringAPI < Grape::API
114-
get :split, { :params => [ :string, :token ] } do
115-
params[:string].split(params[:token])
112+
class StringAPI < Grape::API
113+
get "split/:string", { :params => [ "token" ], :optional_params => [ "limit" ] } do
114+
params[:string].split(params[:token], (params[:limit] || 0))
116115
end
117116
end
118117

119-
StringAPI::routes[0].route_params # yields an array [ :string, :token ]
118+
StringAPI::routes[0].route_params # yields an array [ "string", "token" ]
119+
StringAPI::routes[0].route_optional_params # yields an array [ "limit" ]
120120

121121
## Note on Patches/Pull Requests
122122

lib/grape/api.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,24 @@ def route(methods, paths = ['/'], route_options = {}, &block)
193193
endpoint_options = {}
194194
endpoint_options[:version] = /#{version.join('|')}/ if version
195195

196+
route_options ||= {}
197+
196198
methods.each do |method|
197199
paths.each do |path|
198200

199201
compiled_path = compile_path(path)
200202
path = Rack::Mount::Strexp.compile(compiled_path, endpoint_options, %w( / . ? ), true)
203+
path_params = path.named_captures.map { |nc| nc[0] } - [ 'version', 'format' ]
204+
path_params |= (route_options[:params] || [])
201205
request_method = (method.to_s.upcase unless method == :any)
202206

203-
routes << Route.new({
207+
routes << Route.new(route_options.merge({
204208
:prefix => prefix,
205209
:version => version ? version.join('|') : nil,
206210
:namespace => namespace,
207211
:method => request_method,
208-
:path => compiled_path}
209-
.merge(route_options || {}))
212+
:path => compiled_path,
213+
:params => path_params}))
210214

211215
route_set.add_route(endpoint,
212216
:path_info => path,

spec/grape/api_spec.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,17 +674,22 @@ class TwitterAPI < Grape::API
674674
end
675675
describe "api structure with additional parameters" do
676676
before(:each) do
677-
subject.get :split, { :params => [ :string, :token ] } do
678-
params[:string].split(params[:token])
677+
subject.get 'split/:string', { :params => [ "token" ], :optional_params => [ "limit" ] } do
678+
params[:string].split(params[:token], (params[:limit] || 0).to_i)
679679
end
680680
end
681681
it "should split a string" do
682-
get "/split", :string => "a,b,c", :token => ','
682+
get "/split/a,b,c", :token => ','
683683
last_response.body.should == '["a", "b", "c"]'
684684
end
685+
it "should split a string with limit" do
686+
get "/split/a,b,c", :token => ',', :limit => '2'
687+
last_response.body.should == '["a", "b,c"]'
688+
end
685689
it "should set route_params" do
686690
subject.routes.size.should == 1
687-
subject.routes[0].route_params.should == [ :string, :token ]
691+
subject.routes[0].route_params.should == [ "string", "token" ]
692+
subject.routes[0].route_optional_params.should == [ "limit" ]
688693
end
689694
end
690695
end

0 commit comments

Comments
 (0)