Class: Rack::NestedParams
- Inherits:
-
Object
- Object
- Rack::NestedParams
- Defined in:
- lib/rack/contrib/nested_params.rb
Overview
Rack middleware for parsing POST/PUT body data into nested parameters
Defined Under Namespace
Classes: UrlEncodedPairParser
Constant Summary collapse
- CONTENT_TYPE =
'CONTENT_TYPE'.freeze
- POST_BODY =
'rack.input'.freeze
- FORM_INPUT =
'rack.request.form_input'.freeze
- FORM_HASH =
'rack.request.form_hash'.freeze
- FORM_VARS =
'rack.request.form_vars'.freeze
- URL_ENCODED =
supported content type
'application/x-www-form-urlencoded'.freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ NestedParams
constructor
A new instance of NestedParams.
-
#parse_query_parameters(query_string) ⇒ Object
the rest is nabbed from Rails ##.
Constructor Details
#initialize(app) ⇒ NestedParams
Returns a new instance of NestedParams.
17 18 19 |
# File 'lib/rack/contrib/nested_params.rb', line 17 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# 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) ⇒ Object
the rest is nabbed from Rails ##
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 |