Class: URI::Component::QueryParamsHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/uri/component/query.rb

Overview

Handle query parameters for the URI as a hash

Example

require "uri/component/query"

query = URI::Component::Query.new('foo=123&bar=x+y&bar=%40example')
params = query.params
#=> #<URI::Component::QueryParamsHash: {"foo"=>["123"], "bar"=>["x y", "@example"]}>

p params['foo']
#=> ["123"]
p params[:foo]
#=> ["123"]
p params.values(:foo)
#=> ["123"]
p params.value(:foo)
#=> "123"

p params['bar']
#=> ["x y", "@example"]
p params[:bar]
#=> ["x y", "@example"]
p params.values(:bar)
#=> ["x y", "@example"]
p params.value(:bar)
#=> "x y"

params[:foo] = [1, 2, 3]
#=> [1, 2, 3]
params[:bar] = '[email protected]'
#=> ["[email protected]"]
p query.to_uri
#=> "foo=1&foo=2&foo=3&bar=baz%40example.jp"

Instance Method Summary collapse

Constructor Details

#initializeQueryParamsHash

Returns a new instance of QueryParamsHash.



48
49
50
51
# File 'lib/uri/component/query.rb', line 48

def initialize
	super
	@nil = true
end

Instance Method Details

#[](key) ⇒ Object

:nodoc:



69
70
71
# File 'lib/uri/component/query.rb', line 69

def [](key) #:nodoc:
  super(self.convert_key(key))
end

#[]=(key, values) ⇒ Object

:nodoc:



90
91
92
93
94
# File 'lib/uri/component/query.rb', line 90

def []=(key, values) #:nodoc:
	@nil = false
  values = [values] unless values.kind_of?(Array)
  super(self.convert_key(key), values)
end

#clearObject



53
54
55
56
# File 'lib/uri/component/query.rb', line 53

def clear
	super
	@nil = true
end

#convert_key(key) ⇒ Object

:nodoc:



66
67
68
# File 'lib/uri/component/query.rb', line 66

def convert_key(key) #:nodoc:
  return key.kind_of?(String) ? key : key.to_s
end

#delete(key) ⇒ Object

:nodoc:



100
101
102
# File 'lib/uri/component/query.rb', line 100

def delete(key) #:nodoc:
  super(self.convert_key(key))
end

#fetch(key, default = nil) ⇒ Object

:nodoc:



72
73
74
# File 'lib/uri/component/query.rb', line 72

def fetch(key, default = nil) #:nodoc:
  super(self.convert_key(key), default)
end

#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?

:nodoc:

Returns:

  • (Boolean)


104
105
106
# File 'lib/uri/component/query.rb', line 104

def has_key?(key) #:nodoc:
  super(self.convert_key(key))
end

#merge(hash) ⇒ Object

:nodoc:



111
112
113
114
115
116
117
# File 'lib/uri/component/query.rb', line 111

def merge(hash) #:nodoc:
  hash_new = self.class.new
  hash.each do |key, value|
    hash_new[key] = value
  end
  return hash_new
end

#merge!(hash) ⇒ Object Also known as: update

:nodoc:



119
120
121
122
123
124
# File 'lib/uri/component/query.rb', line 119

def merge!(hash) #:nodoc:
  hash.each do |key, value|
    self[key] = value
  end
  return self
end

#nil=(flag) ⇒ Object



58
59
60
# File 'lib/uri/component/query.rb', line 58

def nil=(flag)
	@nil = flag
end

#nil?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/uri/component/query.rb', line 62

def nil?
	return !@nil
end

#replace(hash) ⇒ Object

:nodoc:



127
128
129
130
131
132
133
# File 'lib/uri/component/query.rb', line 127

def replace(hash) #:nodoc:
  self.clear
  hash.each do |key, value|
    self[key] = value
  end
  return self
end

#store(key, values) ⇒ Object

:nodoc:



95
96
97
98
# File 'lib/uri/component/query.rb', line 95

def store(key, values) #:nodoc:
	@nil = false
  self[key] = values
end

#value(key) ⇒ Object

Returns a value from the hash for the given key.



82
83
84
# File 'lib/uri/component/query.rb', line 82

def value(key)
  return self[key][0]
end

#values(key) ⇒ Object

Returns an array of values from the hash for the given key.



77
78
79
# File 'lib/uri/component/query.rb', line 77

def values(key)
  return self[key]
end

#values_at(*keys) ⇒ Object

:nodoc:



86
87
88
# File 'lib/uri/component/query.rb', line 86

def values_at(*keys) #:nodoc:
  super(*keys.map {|key| self.convert_key(key)})
end