Class: URI::Generic
- Inherits:
-
Object
- Object
- URI::Generic
- Defined in:
- lib/uri_extensions.rb
Class Method Summary collapse
-
.query_from_hash(q_hash) ⇒ Object
Create a URI query string from a Hash.
Class Method Details
.query_from_hash(q_hash) ⇒ Object
Create a URI query string from a Hash. The keys in the returned query string are sorted for easier testing. Example: => ‘bar’, :biz => ‘b a z’ -> ‘?biz=b%20a%20z&foo=bar’
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/uri_extensions.rb', line 12 def self.query_from_hash(q_hash) return '' if q_hash.nil? # Reveal cases where the caller thought they were using HashWithIndifferentAccess # but actually they used Hash and probably got duplicate keys (:foo vs 'foo') q_hash = q_hash.rekey{|k| k.is_a?(Symbol) ? ":#{k}" : k} unless q_hash.is_a?(HashWithIndifferentAccess) pairs = [] q_hash.each_pair{|k,v| pairs << "#{k}=#{URI.escape(v.to_s)}" unless v.nil?} '?' + pairs.sort.join('&') end |