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

extract headers and params from Endpoint to Grape::Request #392

Merged
merged 1 commit into from
Apr 24, 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
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