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.

Defined Under Namespace

Classes: FormEncodedPairParser

Class Method Summary collapse

Class Method Details

.parse_formatted_request_parameters(mime_type, raw_post_data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/action_controller/cgi_ext/cgi_methods.rb', line 48

def parse_formatted_request_parameters(mime_type, raw_post_data)
  case strategy = ActionController::Base.param_parsers[mime_type]
    when Proc
      strategy.call(raw_post_data)
    when :xml_simple
      raw_post_data.blank? ? {} : Hash.from_xml(raw_post_data)
    when :yaml
      YAML.load(raw_post_data)
    when :xml_node
      node = XmlNode.from_xml(raw_post_data)
      { node.node_name => node }
  end
rescue Exception => e # YAML, XML or Ruby code block errors
  { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, 
    "raw_post_data" => raw_post_data, "format" => mime_type }
end

.parse_query_parameters(query_string) ⇒ Object

DEPRECATED: Use parse_form_encoded_parameters



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/action_controller/cgi_ext/cgi_methods.rb', line 10

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

  FormEncodedPairParser.new(pairs).result
end

.parse_request_parameters(params) ⇒ Object

DEPRECATED: Use parse_form_encoded_parameters



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/action_controller/cgi_ext/cgi_methods.rb', line 23

def parse_request_parameters(params)
  parser = FormEncodedPairParser.new

  params = params.dup
  until params.empty?
    for key, value in params
      if key.blank?
        params.delete key
      elsif !key.include?('[')
        # much faster to test for the most common case first (GET)
        # and avoid the call to build_deep_hash
        parser.result[key] = get_typed_value(value[0])
        params.delete key
      elsif value.is_a?(Array)
        parser.parse(key, get_typed_value(value.shift))
        params.delete key if value.empty?
      else
        raise TypeError, "Expected array, found #{value.inspect}"
      end
    end
  end

  parser.result
end