Skip to content

Commit 16cbc3c

Browse files
author
Michael Bleigh
committed
Use a Mash for params. Closes ruby-grape#50, Closes ruby-grape#47, Closes ruby-grape#27
1 parent 70eb5e2 commit 16cbc3c

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

Gemfile.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PATH
22
remote: .
33
specs:
44
grape (0.1.5)
5+
hashie (~> 1.0)
56
multi_json
67
multi_xml
78
rack
@@ -19,15 +20,16 @@ GEM
1920
guard (>= 0.2.2)
2021
guard-rspec (0.4.2)
2122
guard (>= 0.4.0)
23+
hashie (1.1.0)
2224
json_pure (1.5.2)
2325
maruku (0.6.0)
2426
syntax (>= 1.0.0)
2527
multi_json (1.0.3)
26-
multi_xml (0.2.2)
28+
multi_xml (0.3.0)
2729
rack (1.3.0)
2830
rack-jsonp (1.2.0)
2931
rack
30-
rack-mount (0.8.1)
32+
rack-mount (0.8.3)
3133
rack (>= 1.0.0)
3234
rack-test (0.6.0)
3335
rack (>= 1.0)

grape.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
1818
s.add_runtime_dependency 'rack-jsonp'
1919
s.add_runtime_dependency 'multi_json'
2020
s.add_runtime_dependency 'multi_xml'
21+
s.add_runtime_dependency 'hashie', '~> 1.0'
2122

2223
s.add_development_dependency 'rake'
2324
s.add_development_dependency 'maruku'

lib/grape/endpoint.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'rack'
22
require 'grape'
3+
require 'hashie'
34

45
module Grape
56
# An Endpoint is the proxy scope in which all routing
@@ -29,11 +30,7 @@ def self.call(env)
2930
# The parameters passed into the request as
3031
# well as parsed from URL segments.
3132
def params
32-
@params ||= request.params.merge(env['rack.routing_args'] || {}).inject({}) do |h,(k,v)|
33-
h[k.to_s] = v
34-
h[k.to_sym] = v
35-
h
36-
end
33+
@params ||= Hashie::Mash.new.deep_merge(request.params).deep_merge(env['rack.routing_args'] || {})
3734
end
3835

3936
# The API version as specified in the URL.

spec/grape/endpoint_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ def app; subject end
4848
get '/hey/12'
4949
last_response.body.should == '12'
5050
end
51+
52+
it 'should deeply convert nested params' do
53+
subject.get '/location' do
54+
params[:location][:city]
55+
end
56+
get '/location?location[city]=Dallas'
57+
last_response.body.should == 'Dallas'
58+
end
5159
end
5260

5361
describe '#error!' do

0 commit comments

Comments
 (0)