Class: Rack::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/params.rb

Constant Summary collapse

URL_ENCODED =
%r{^application/x-www-form-urlencoded}
JSON_ENCODED =
%r{^application/json}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Params

A middle ware to parse params. This will parse both the query string parameters and the body and place them into the params hash of the Goliath::Env for the request.

– Borrowed from Goliath – goliath.io

Examples:

use Rack::Params


15
16
17
# File 'lib/rack/params.rb', line 15

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
# File 'lib/rack/params.rb', line 19

def call(env)
  env['params'] = retrieve_params(env)
  
  @app.call(env)
end

#retrieve_params(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rack/params.rb', line 25

def retrieve_params(env)
  params = {}
  params.merge!(::Rack::Utils.parse_nested_query(env['QUERY_STRING']))

  if env['rack.input']
    post_params = ::Rack::Utils::Multipart.parse_multipart(env)
    unless post_params
      body = env['rack.input'].read
      env['rack.input'].rewind
      
      post_params = case(env['CONTENT_TYPE'])
      when URL_ENCODED then
        ::Rack::Utils.parse_nested_query(body)
      when JSON_ENCODED then
        JSON.parse(body)
      else
        {}
      end
    end
    params.merge!(post_params)
  end

  params
end