Skip to content

Commit d996563

Browse files
author
Michael Bleigh
committed
Add custom route segments.
1 parent 1bf41db commit d996563

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

lib/grape/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def compile_path(path)
7575

7676
def route(method, path_info, &block)
7777
route_set.add_route(build_endpoint(&block),
78-
:path_info => compile_path(path_info),
78+
:path_info => Rack::Mount::Strexp.compile(compile_path(path_info)),
7979
:request_method => method
8080
)
8181
end

lib/grape/endpoint.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22
require 'grape'
33

44
module Grape
5+
# An Endpoint is the proxy scope in which all routing
6+
# blocks are executed. In other words, any methods
7+
# on the instance level of this class may be called
8+
# from inside a `get`, `post`, etc. block.
59
class Endpoint
610
def initialize(&block)
711
@block = block
812
end
913

1014
attr_reader :env, :request
1115

16+
def params
17+
@params ||= request.params.merge(env['rack.routing_args'] || {}).inject({}) do |h,(k,v)|
18+
h[k.to_s] = v
19+
h[k.to_sym] = v
20+
h
21+
end
22+
end
23+
24+
# Set or retrieve the HTTP status code.
1225
def status(status = nil)
1326
if status
1427
@status = status
@@ -23,6 +36,8 @@ def status(status = nil)
2336
end
2437
end
2538

39+
# Set an individual header or retrieve
40+
# all headers that have been set.
2641
def header(key = nil, val = nil)
2742
if key
2843
val ? @header[key.to_s] = val : @header.delete(key.to_s)

spec/grape/endpoint_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,24 @@ def app; subject end
2929
last_response.headers['X-Awesome'].should == 'true'
3030
end
3131
end
32+
33+
describe '#params' do
34+
it 'should be available to the caller' do
35+
subject.get('/hey') do
36+
params[:howdy]
37+
end
38+
39+
get '/hey?howdy=hey'
40+
last_response.body.should == 'hey'
41+
end
42+
43+
it 'should parse from path segments' do
44+
subject.get('/hey/:id') do
45+
params[:id]
46+
end
47+
48+
get '/hey/12'
49+
last_response.body.should == '12'
50+
end
51+
end
3252
end

0 commit comments

Comments
 (0)