Skip to content

Commit

Permalink
Merge pull request #392 from niedhui/master
Browse files Browse the repository at this point in the history
extract headers and params from Endpoint to Grape::Request
  • Loading branch information
dblock committed Apr 24, 2013
2 parents 2b4ef1c + ed905be commit d4a3cb3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Grape
autoload :Namespace, 'grape/namespace'
autoload :Cookies, 'grape/cookies'
autoload :Validations, 'grape/validations'
autoload :Request, 'grape/http/request'

module Exceptions
autoload :Base, 'grape/exceptions/base'
Expand Down
14 changes: 3 additions & 11 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ def call!(env)
# The parameters passed into the request as
# well as parsed from URL segments.
def params
@params ||= Hashie::Mash.new.
deep_merge(request.params).
deep_merge(env['rack.routing_args'] || {})
@params ||= @request.params
end

# A filtering method that will return a hash
Expand Down Expand Up @@ -257,13 +255,7 @@ def header(key = nil, val = nil)

# Retrieves all available request headers.
def headers
@headers ||= @env.dup.inject({}) { |h, (k, v)|
if k.start_with? 'HTTP_'
k = k[5..-1].gsub('_', '-').downcase.gsub(/^.|[-_\s]./) { |x| x.upcase }
h[k] = v
end
h
}
@headers ||= @request.headers
end

# Set response content-type
Expand Down Expand Up @@ -369,7 +361,7 @@ def endpoints
def run(env)
@env = env
@header = {}
@request = Rack::Request.new(@env)
@request = Grape::Request.new(@env)

self.extend helpers
cookies.read(@request)
Expand Down
21 changes: 21 additions & 0 deletions lib/grape/http/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Grape
class Request < Rack::Request

def params
@env['grape.request.params'] ||= Hashie::Mash.new.
deep_merge(super).
deep_merge(env['rack.routing_args'] || {})
end

def headers
@env['grape.request.headers'] ||= @env.dup.inject({}) { |h, (k, v)|
if k.start_with? 'HTTP_'
k = k[5..-1].gsub('_', '-').downcase.gsub(/^.|[-_\s]./) { |x| x.upcase }
h[k] = v
end
h
}
end

end
end
2 changes: 1 addition & 1 deletion lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def before; end
def after; end

def request
Rack::Request.new(self.env)
Grape::Request.new(self.env)
end

def response
Expand Down

2 comments on commit d4a3cb3

@aiwilliams
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a great commit, however, the message does not indicate a change in behavior which has been introduced. The headers and params are now stored in the env. This is not a Bad Thing, but perhaps it warrants a message like 'Store headers and params in Rack env to (accommodate | repair | what?)...'

@dblock
Copy link
Member Author

@dblock dblock commented on d4a3cb3 Apr 24, 2013

Choose a reason for hiding this comment

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

Totally, @niedhui, could you please update the CHANGELOG? Thx.

Please sign in to comment.