Skip to content

Commit 34de917

Browse files
author
Michael Bleigh
committed
Merge pull request ruby-grape#126 from dblock/frontier-route
Frontier: added access to `route` from within an API
2 parents a2e36d1 + 59c8474 commit 34de917

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

README.markdown

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class TwitterAPI < Grape::API
278278
api.version
279279
end
280280

281-
desc "Reverses a string.", { :params =>
281+
desc "Reverses a string.", { :params =>
282282
{ "s" => { :desc => "string to reverse", :type => "string" }}
283283
}
284284
get "reverse" do
@@ -313,6 +313,17 @@ StringAPI::routes[0].route_params # yields an array [ "string", "token" ]
313313
StringAPI::routes[0].route_optional_params # yields an array [ "limit" ]
314314
```
315315

316+
It's possible to retrieve the information about the current route from within an API call with `route`.
317+
318+
```ruby
319+
class MyAPI < Grape::API
320+
desc "Returns a description of a parameter.", { :params => { "id" => "a required id" } }
321+
get "params/:id" do
322+
route.route_params[params[:id]] # returns "a required id"
323+
end
324+
end
325+
```
326+
316327
## Anchoring
317328

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

lib/grape/endpoint.rb

Lines changed: 15 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,18 @@ 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+
# desc "Returns the route description."
219+
# get '/' do
220+
# route.route_description
221+
# end
222+
def route
223+
env["rack.routing_args"][:route_info]
224+
end
213225

214226
protected
215227

lib/grape/route.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def method_missing(method_id, *arguments)
1616
end
1717

1818
def to_s
19-
"Version: #{route_version} #{route_method} #{route_path}"
19+
"version=#{route_version}, method=#{route_method}, path=#{route_path}"
2020
end
2121

2222
end

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)