Module: URI::QueryParams
- Defined in:
- lib/uri/query_params/mixin.rb,
lib/uri/query_params/version.rb,
lib/uri/query_params/query_params.rb
Defined Under Namespace
Modules: Mixin
Constant Summary collapse
- VERSION =
uri-query_params version
'0.8.2'
- UNSAFE =
RFC 3986 unsafe characters (including ' ')
[ '!', '*', "'", '(', ')', ';', ':', '@', '&', '=', '+', '$', ',', '/', '?', '%', '#', '[', ']', ' ', "\f", "\n", "\r", "\t", "\v", "\x7f", *("\x00".."\x1f") ].join
Class Method Summary collapse
-
.dump(query_params) ⇒ String
Dumps the URI query params.
-
.escape(value) ⇒ String
Escapes a URI query param value.
-
.parse(query_string) {|name, value| ... } ⇒ Hash{String => String}
Parses a URI query string.
Class Method Details
.dump(query_params) ⇒ String
Dumps the URI query params.
114 115 116 117 118 |
# File 'lib/uri/query_params/query_params.rb', line 114 def self.dump(query_params) query_params.map { |name,value| "#{name}=#{escape(value)}" }.join('&') end |
.escape(value) ⇒ String
Escapes a URI query param value.
80 81 82 83 84 85 86 87 |
# File 'lib/uri/query_params/query_params.rb', line 80 def self.escape(value) case value when Array then URI::DEFAULT_PARSER.escape(value.join(' '),UNSAFE) when true then String.new('active') when false, nil then String.new('') else URI::DEFAULT_PARSER.escape(value.to_s,UNSAFE) end end |
.parse(query_string) {|name, value| ... } ⇒ Hash{String => String}
Note:
Version 0.6.0 allows parse to yield the query params, in the order they are parsed.
Parses a URI query string.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/uri/query_params/query_params.rb', line 46 def self.parse(query_string) query_params = {} if query_string query_string.split('&').each do |param| # skip empty params next if param.empty? name, value = param.split('=',2) value = if value then URI::DEFAULT_PARSER.unescape(value) else '' end yield(name,value) if block_given? query_params[name] = value end end return query_params end |