Skip to content

Commit 8ee8481

Browse files
author
Michael Bleigh
committed
Merge pull request #159 from gaiottino/master
Adding :requirements to routes so it's possible to use periods etc in path
2 parents 5ee0f05 + c6f4398 commit 8ee8481

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

lib/grape/endpoint.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ def prepare_routes
5252
anchor = options[:route_options][:anchor]
5353
anchor = anchor.nil? ? true : anchor
5454

55-
path = compile_path(prepared_path, anchor && !options[:app])
55+
requirements = options[:route_options][:requirements] || {}
56+
57+
path = compile_path(prepared_path, anchor && !options[:app], requirements)
5658
regex = Rack::Mount::RegexpWithNamedGroups.new(path)
5759
path_params = {}
5860
# named parameters in the api path
@@ -91,9 +93,10 @@ def namespace
9193
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
9294
end
9395

94-
def compile_path(prepared_path, anchor = true)
96+
def compile_path(prepared_path, anchor = true, requirements = {})
9597
endpoint_options = {}
9698
endpoint_options[:version] = /#{settings[:version].join('|')}/ if settings[:version]
99+
endpoint_options.merge!(requirements)
97100
Rack::Mount::Strexp.compile(prepared_path, endpoint_options, %w( / . ? ), anchor)
98101
end
99102

lib/grape/middleware/formatter.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ def before
4242

4343
def format_from_extension
4444
parts = request.path.split('.')
45-
hit = parts.last.to_sym
46-
47-
if parts.size <= 1
48-
nil
49-
else
50-
hit
45+
extension = parts.last.to_sym
46+
47+
if parts.size > 1 && content_types.key?(extension)
48+
return extension
5149
end
50+
nil
5251
end
5352

5453
def format_from_header

spec/grape/endpoint_spec.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def app; subject end
107107
]
108108
end
109109
end
110-
110+
111111
describe '#params' do
112112
it 'should be available to the caller' do
113113
subject.get('/hey') do
@@ -134,6 +134,42 @@ def app; subject end
134134
get '/location?location[city]=Dallas'
135135
last_response.body.should == 'Dallas'
136136
end
137+
138+
context 'with special requirements' do
139+
it 'should parse email param with provided requirements for params' do
140+
subject.get('/:person_email', :requirements => { :person_email => /.*/ }) do
141+
params[:person_email]
142+
end
143+
144+
145+
last_response.body.should == '[email protected]'
146+
147+
148+
last_response.body.should == '[email protected]'
149+
end
150+
151+
it 'should parse many params with provided regexps' do
152+
subject.get('/:person_email/test/:number',
153+
:requirements => {
154+
:person_email => /rodzyn@(.*).com/,
155+
:number => /[0-9]/ }) do
156+
params[:person_email] << params[:number]
157+
end
158+
159+
get '/[email protected]/test/1'
160+
last_response.body.should == '[email protected]'
161+
162+
get '/[email protected]/test/1'
163+
last_response.status.should == 404
164+
165+
get '[email protected]/test/wrong_number'
166+
last_response.status.should == 404
167+
168+
get '[email protected]/wrong_middle/1'
169+
last_response.status.should == 404
170+
171+
end
172+
end
137173
end
138174

139175
describe '#error!' do
@@ -309,4 +345,4 @@ def memoized
309345
end
310346
end
311347
end
312-
end
348+
end

spec/grape/middleware/formatter_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ def to_xml
8282
subject.call({'PATH_INFO' => '/info.txt', 'HTTP_ACCEPT' => 'application/json'})
8383
subject.env['api.format'].should == :txt
8484
end
85-
86-
it 'should throw an error on an unrecognized format' do
87-
err = catch(:error){ subject.call({'PATH_INFO' => '/info.barklar'}) }
88-
err.should == {:status => 406, :message => "The requested format is not supported."}
89-
end
9085
end
9186

9287
context 'Accept header detection' do

0 commit comments

Comments
 (0)