Module: Webbed::Helpers::RackRequestHelper::ClassMethods

Defined in:
lib/webbed/helpers/rack_request_helper.rb

Instance Method Summary collapse

Instance Method Details

#from_rack(rack_env) ⇒ Request

Note:

The Rack environment will never be modified.

Create a new Request from a Rack environment

This method will take the HTTP-Version, Method, Request-URI, Headers, Entity Body, and scheme from the Rack environment and create a new Request using that data. The Rack environment can be accessed through #rack_env.

Parameters:

  • rack_env (Hash{String => String})

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/webbed/helpers/rack_request_helper.rb', line 16

def from_rack(rack_env)
  env          = rack_env.dup
  method       = env.delete('REQUEST_METHOD')
  request_uri  = "#{env['PATH_INFO']}?#{env['QUERY_STRING']}"
  http_version = env.delete('HTTP_VERSION') || 'HTTP/1.1'
  scheme       = env.delete('rack.url_scheme')
  entity_body  = env.delete('rack.input')
  
  headers = env.inject({}) do |memo, h|
    memo[$1.gsub('_', '-')] = h[1] if h[0] =~ /^HTTP_(.*)/
    memo
  end
  
  headers['Content-Type']   = env.delete('CONTENT_TYPE')
  headers['Content-Length'] = env.delete('CONTENT_LENGTH')
  
  request = new(method, request_uri, headers, entity_body, {
    :http_version => http_version,
    :scheme       => scheme
  })
  
  request.rack_env = rack_env
  
  request
end