Skip to content

Commit 52f8398

Browse files
committed
Added route that retrieves the information about the current route.
1 parent 6d8793f commit 52f8398

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

README.markdown

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class TwitterAPI < Grape::API
266266
api.version
267267
end
268268

269-
desc "Reverses a string.", { :params =>
269+
desc "Reverses a string.", { :params =>
270270
{ "s" => { :desc => "string to reverse", :type => "string" }}
271271
}
272272
get "reverse" do
@@ -301,6 +301,17 @@ StringAPI::routes[0].route_params # yields an array [ "string", "token" ]
301301
StringAPI::routes[0].route_optional_params # yields an array [ "limit" ]
302302
```
303303

304+
It's possible to retrieve the information about the current route from within an API call with `route`.
305+
306+
```ruby
307+
class MyAPI < Grape::API
308+
desc "Returns a description of a parameter.", { :params => { "id" => "a required id" } }
309+
get "params/:id" do
310+
route.route_params[params[:id]] # returns "a required id"
311+
end
312+
end
313+
```
314+
304315
## Anchoring
305316

306317
Grape by default anchors all request paths, which means that the request URL

lib/grape/endpoint.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def mount_in(route_set)
3535
options[:app].endpoints.each{|e| e.mount_in(route_set)}
3636
else
3737
routes.each do |route|
38-
route_set.add_route(self,
38+
route_set.add_route(self, {
3939
:path_info => route.route_compiled,
40-
:request_method => route.route_method
41-
)
40+
:request_method => route.route_method,
41+
}, { :route_info => route })
4242
end
4343
end
4444
end
@@ -210,6 +210,17 @@ def present(object, options = {})
210210
representation = { root => representation } if root
211211
body representation
212212
end
213+
214+
# Returns route information for the current request.
215+
#
216+
# @example
217+
#
218+
# get '/users/:id' do
219+
#
220+
# end
221+
def route
222+
env["rack.routing_args"][:route_info]
223+
end
213224

214225
protected
215226

spec/grape/api_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,4 +1017,43 @@ class CommunicationError < RuntimeError; end
10171017
subject.instance.should be_nil
10181018
end
10191019
end
1020+
1021+
describe ".route" do
1022+
context "plain" do
1023+
before(:each) do
1024+
subject.get '/' do
1025+
route.route_path
1026+
end
1027+
subject.get '/path' do
1028+
route.route_path
1029+
end
1030+
end
1031+
it 'should provide access to route info' do
1032+
get '/'
1033+
last_response.body.should == "/(.:format)"
1034+
get '/path'
1035+
last_response.body.should == "/path(.:format)"
1036+
end
1037+
end
1038+
context "with desc" do
1039+
before(:each) do
1040+
subject.desc 'returns description'
1041+
subject.get '/description' do
1042+
route.route_description
1043+
end
1044+
subject.desc 'returns parameters', { :params => { "x" => "y" }}
1045+
subject.get '/params/:id' do
1046+
route.route_params[params[:id]]
1047+
end
1048+
end
1049+
it 'should return route description' do
1050+
get '/description'
1051+
last_response.body.should == "returns description"
1052+
end
1053+
it 'should return route parameters' do
1054+
get '/params/x'
1055+
last_response.body.should == "y"
1056+
end
1057+
end
1058+
end
10201059
end

0 commit comments

Comments
 (0)