Class: CGIMethods

Inherits:
Object show all
Defined in:
lib/action_controller/cgi_ext/cgi_methods.rb

Overview

Static methods for parsing the query and request parameters that can be used in a CGI extension class or testing in isolation.

Class Method Summary collapse

Class Method Details

.parse_query_parameters(query_string) ⇒ Object

Returns a hash with the pairs from the query string. The implicit hash construction that is done in parse_request_params is not done here.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/action_controller/cgi_ext/cgi_methods.rb', line 9

def CGIMethods.parse_query_parameters(query_string)
  parsed_params = {}
  
  query_string.split(/[&;]/).each { |p| 
    k, v = p.split('=')

    k = CGI.unescape(k) unless k.nil?
    v = CGI.unescape(v) unless v.nil?

    if k =~ /(.*)\[\]$/
        if parsed_params.has_key? $1
            parsed_params[$1] << v
        else
            parsed_params[$1] = [v]
        end
    else
        parsed_params[k] = v.nil? ? nil : v
    end
  }
  
  return parsed_params
end

.parse_request_parameters(params) ⇒ Object

Returns the request (POST/GET) parameters in a parsed form where pairs such as “customer[street]” / “Somewhere cool!” are translated into a full hash hierarchy, like { “customer” => { “address” => { “street” => “Somewhere cool!” } } }



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/action_controller/cgi_ext/cgi_methods.rb', line 35

def CGIMethods.parse_request_parameters(params)
  parsed_params = {}

  for key, value in params
    value = [value] if key =~ /.*\[\]$/
    CGIMethods.build_deep_hash(
      CGIMethods.get_typed_value(value[0]),
      parsed_params, 
      CGIMethods.get_levels(key)
    )
  end

  return parsed_params
end