Module: QueryString

Defined in:
lib/querystring.rb

Constant Summary collapse

VERSION =
File.read(File.expand_path('../../VERSION', __FILE__)).strip

Class Method Summary collapse

Class Method Details

.parse(q, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/querystring.rb', line 45

def self.parse q, options={}
  o = {
    :seperator => /&|;/,           # between params
    :equals => '=',                # between key/value pairs
    :array_seperator => ','        # used with :combine array style
  }.merge(options)

  q.split(o[:seperator]).inject({}) do |ret, pair|
    key, value = pair.split(o[:equals])
    value.split(o[:array_seperator]).inject(ret) do |ret, part|
      key =~ /([^\[]+)(\[(\d)?\])?/
      ret[$1] ||= []
      if $3
        ret[$1][$3.to_i] = CGI.unescape(part)
      else
        ret[$1] << CGI.unescape(part)
      end
      ret
    end
  end
end

.stringify(obj, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/querystring.rb', line 6

def self.stringify obj, options={}
  o = {
    :seperator => '&',             # between params
    :equals => '=',                # between key/value pairs
    :array_style => :seperate,     # or :combine
    :array_brackets => false,      # use [] with array_style :seperate
    :array_index => false,         # use indexes with array style :seperate
    :array_seperator => ','        # used with :combine array style
  }.merge(options)

  # used for seperate style arrays
  ar_tmpl = "%s#{'[' if o[:array_brackets]}%s#{']' if o[:array_brackets]}#{o[:equals]}%s"

  obj.map do |key,value|
    key = CGI.escape(key.to_s)

    if value.is_a?Array
      case o[:array_style]
      when :combine
        list = value.map do |inner_value|
          CGI.escape(inner_value.to_s)
        end.join o[:array_seperator]
        "#{key}#{o[:equals]}#{list}"
      else  # when :seperate
        value.each_with_index.map do |inner_value, index|
          index = o[:array_index] ? index.to_s : ''
          inner_value = CGI.escape(inner_value.to_s)

          ar_tmpl%[key, index, inner_value]
        end.join o[:seperator]
      end
    else
      value = CGI.escape(value.to_s)

      "#{key}#{o[:equals]}#{value}"
    end
  end.join o[:seperator]
end