Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds API.route_param for quick declaration of route parameters. #376

Merged
merged 2 commits into from
Apr 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ module Twitter
params do
requires :id, :type => Integer, :desc => "Status id."
end
get ':id' do
Status.find(params[:id])
route_param ':id' do
get do
Status.find(params[:id])
end
end

desc "Create a status."
Expand Down
11 changes: 11 additions & 0 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,17 @@ def namespace(space = nil, options = {}, &block)
end
end

# Thie method allows you to quickly define a parameter route segment
# in your API.
#
# @param param [Symbol] The name of the parameter you wish to declare.
# @option options [Regexp] You may supply a regular expression that the declared parameter must meet.
def route_param(param, options = {}, &block)
options = options.dup
options[:requirements] = {param.to_sym => options[:requirements]} if options[:requirements].is_a? Regexp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want to be modifying the hash that is being passed in.

namespace_options = {}
namespace_options.merge! options
namespace_options[:requirements] = ... if options[:requirements].is_a? Regexp
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Did a quick dup instead but same effect.

namespace(":#{param}", options, &block)
end

alias_method :group, :namespace
alias_method :resource, :namespace
alias_method :resources, :namespace
Expand Down
30 changes: 30 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,36 @@ def app; subject end
end
end

describe '.route_param' do
it 'adds a parameterized route segment namespace' do
subject.namespace :users do
route_param :id do
get do
params[:id]
end
end
end

get '/users/23'
last_response.body.should == '23'
end

it 'should be able to define requirements with a single hash' do
subject.namespace :users do
route_param :id, :requirements => /[0-9]+/ do
get do
params[:id]
end
end
end

get '/users/michael'
last_response.status.should == 404
get '/users/23'
last_response.status.should == 200
end
end

describe '.route' do
it 'allows for no path' do
subject.namespace :votes do
Expand Down