class Rack::NestedParams

Rack middleware for parsing POST/PUT body data into nested parameters

Constants

CONTENT_TYPE
FORM_HASH
FORM_INPUT
FORM_VARS
POST_BODY
URL_ENCODED

supported content type

Public Class Methods

new(app) click to toggle source
# File lib/rack/contrib/nested_params.rb, line 17
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/contrib/nested_params.rb, line 21
def call(env)
  if form_vars = env[FORM_VARS]
    env[FORM_HASH] = parse_query_parameters(form_vars)
  elsif env[CONTENT_TYPE] == URL_ENCODED
    post_body = env[POST_BODY]
    env[FORM_INPUT] = post_body
    env[FORM_HASH] = parse_query_parameters(post_body.read)
    post_body.rewind if post_body.respond_to?(:rewind)
  end
  @app.call(env)
end
parse_query_parameters(query_string) click to toggle source

the rest is nabbed from Rails ##

# File lib/rack/contrib/nested_params.rb, line 35
def parse_query_parameters(query_string)
  return {} if query_string.nil? or query_string.empty?

  pairs = query_string.split('&').collect do |chunk|
    next if chunk.empty?
    key, value = chunk.split('=', 2)
    next if key.empty?
    value = value.nil? ? nil : CGI.unescape(value)
    [ CGI.unescape(key), value ]
  end.compact

  UrlEncodedPairParser.new(pairs).result
end