Skip to content

Commit

Permalink
Merge pull request #159 from gaiottino/master
Browse files Browse the repository at this point in the history
Adding :requirements to routes so it's possible to use periods etc in path
  • Loading branch information
Michael Bleigh committed Apr 4, 2012
2 parents 5ee0f05 + c6f4398 commit 8ee8481
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
7 changes: 5 additions & 2 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def prepare_routes
anchor = options[:route_options][:anchor]
anchor = anchor.nil? ? true : anchor

path = compile_path(prepared_path, anchor && !options[:app])
requirements = options[:route_options][:requirements] || {}

path = compile_path(prepared_path, anchor && !options[:app], requirements)
regex = Rack::Mount::RegexpWithNamedGroups.new(path)
path_params = {}
# named parameters in the api path
Expand Down Expand Up @@ -91,9 +93,10 @@ def namespace
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
end

def compile_path(prepared_path, anchor = true)
def compile_path(prepared_path, anchor = true, requirements = {})
endpoint_options = {}
endpoint_options[:version] = /#{settings[:version].join('|')}/ if settings[:version]
endpoint_options.merge!(requirements)
Rack::Mount::Strexp.compile(prepared_path, endpoint_options, %w( / . ? ), anchor)
end

Expand Down
11 changes: 5 additions & 6 deletions lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ def before

def format_from_extension
parts = request.path.split('.')
hit = parts.last.to_sym

if parts.size <= 1
nil
else
hit
extension = parts.last.to_sym

if parts.size > 1 && content_types.key?(extension)
return extension
end
nil
end

def format_from_header
Expand Down
40 changes: 38 additions & 2 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def app; subject end
]
end
end

describe '#params' do
it 'should be available to the caller' do
subject.get('/hey') do
Expand All @@ -134,6 +134,42 @@ def app; subject end
get '/location?location[city]=Dallas'
last_response.body.should == 'Dallas'
end

context 'with special requirements' do
it 'should parse email param with provided requirements for params' do
subject.get('/:person_email', :requirements => { :person_email => /.*/ }) do
params[:person_email]
end

get '/rodzyn@grape.com'
last_response.body.should == 'rodzyn@grape.com'

get 'rodzyn@grape.com.pl'
last_response.body.should == 'rodzyn@grape.com.pl'
end

it 'should parse many params with provided regexps' do
subject.get('/:person_email/test/:number',
:requirements => {
:person_email => /rodzyn@(.*).com/,
:number => /[0-9]/ }) do
params[:person_email] << params[:number]
end

get '/rodzyn@grape.com/test/1'
last_response.body.should == 'rodzyn@grape.com1'

get '/rodzyn@testing.wrong/test/1'
last_response.status.should == 404

get 'rodzyn@test.com/test/wrong_number'
last_response.status.should == 404

get 'rodzyn@test.com/wrong_middle/1'
last_response.status.should == 404

end
end
end

describe '#error!' do
Expand Down Expand Up @@ -309,4 +345,4 @@ def memoized
end
end
end
end
end
5 changes: 0 additions & 5 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ def to_xml
subject.call({'PATH_INFO' => '/info.txt', 'HTTP_ACCEPT' => 'application/json'})
subject.env['api.format'].should == :txt
end

it 'should throw an error on an unrecognized format' do
err = catch(:error){ subject.call({'PATH_INFO' => '/info.barklar'}) }
err.should == {:status => 406, :message => "The requested format is not supported."}
end
end

context 'Accept header detection' do
Expand Down

0 comments on commit 8ee8481

Please sign in to comment.