Class: URL::ParamsHash

Inherits:
Mash
  • Object
show all
Defined in:
lib/url/helper_classes.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mash

#[], #[]=

Class Method Details

.from_string(str) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/url/helper_classes.rb', line 56

def from_string str
  params = URL::ParamsHash.new
  str.split('&').each do |myp|
    key,value = myp.split('=')
    value = CGI.unescape(value) if value
    params[key.to_sym] = value if key
  end
  params
end

Instance Method Details

#to_sObject

Merges the array into a parameter string of the form ?key=value&foo=bar



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/url/helper_classes.rb', line 21

def to_s
  return '' if empty?
  '?' + to_a.inject(Array.new) do |ret,param|
    key = param[0].to_s
    val = param[1]
    
    if param && val
      if val.is_a?(Hash)
        # TODO: Make this recusrive
        val.each do |param_key,param_val|
          param_key = CGI.escape("#{key}[#{param_key}]")
          param_val = CGI.escape(param_val.to_s)
          ret << %Q{#{param_key}=#{param_val}}
        end
      elsif val.is_a?(Array)
        # TODO: Make this recusrive
        val.each_with_index do |param_val,i|
          param_key = CGI.escape("#{key}[]")
          param_val = CGI.escape(param_val.to_s)
          ret << %Q{#{param_key}=#{param_val}}
        end
      else
        val = val.to_s

        val = CGI.escape(val)# if val =~ /(\/|\?|\s)/
        ret << %{#{param[0].to_s}=#{val}}
      end
    elsif param
      ret << param[0].to_s
    end
    ret
  end.join('&')
end