Module: Protocol::URL::Encoding
- Defined in:
- lib/protocol/url/encoding.rb
Overview
Helpers for encoding and decoding URL components.
Defined Under Namespace
Modules: System
Constant Summary collapse
- NON_FRAGMENT_CHARACTER_PATTERN =
Matches characters that are not allowed in a URI fragment. According to RFC 3986 Section 3.5, a valid fragment consists of pchar / "/" / "?" characters.
/([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/\?]+)/.freeze
Class Method Summary collapse
-
.assign(keys, value, parent) ⇒ Object
Assign a value to a nested hash.
-
.decode(string, maximum = 8, symbolize_keys: false) ⇒ Object
Decode a URL-encoded query string into a hash.
-
.decode_www_form(string, maximum = 8, symbolize_keys: false) ⇒ Object
Decode an
application/x-www-form-urlencodedstring into a hash. -
.encode(value, prefix = nil) ⇒ Object
Encodes a hash or array into a query string.
-
.escape(string, encoding = string.encoding) ⇒ Object
Escapes a string using percent encoding, e.g.
-
.escape_fragment(fragment) ⇒ Object
Escapes non-fragment characters using percent encoding.
-
.scan(string) ⇒ Object
Scan a string for URL-encoded key/value pairs.
-
.split(name) ⇒ Object
Split a key into parts, e.g.
-
.unescape(string, encoding = string.encoding) ⇒ Object
Unescapes a percent encoded string, e.g.
Class Method Details
.assign(keys, value, parent) ⇒ Object
Assign a value to a nested hash.
This method handles building nested data structures from query string parameters, including arrays of objects. When processing array elements (empty key like []), it intelligently decides whether to add to the last array element or create a new one.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/protocol/url/encoding.rb', line 181 def self.assign(keys, value, parent) top, *middle = keys middle.each_with_index do |key, index| if key.nil? or key.empty? # Array element (e.g., items[]): parent = (parent[top] ||= Array.new) top = parent.size # Check if we should reuse the last array element or create a new one. If there's a nested key coming next, and the last array element already has that key, then we need a new array element. Otherwise, add to the existing one. if nested = middle[index+1] and last = parent.last # If the last element doesn't include the nested key, reuse it (decrement index). # If it does include the key, keep current index (creates new element). top -= 1 unless last.include?(nested) end else # Hash key (e.g., user[name]): parent = (parent[top] ||= Hash.new) top = key end end parent[top] = value end |
.decode(string, maximum = 8, symbolize_keys: false) ⇒ Object
Decode a URL-encoded query string into a hash.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/protocol/url/encoding.rb', line 220 def self.decode(string, maximum = 8, symbolize_keys: false) parameters = {} self.scan(string) do |name, value| keys = self.split(name) if keys.empty? raise ArgumentError, "Invalid key path: #{name.inspect}!" end if keys.size > maximum raise ArgumentError, "Key length exceeded limit!" end if symbolize_keys keys.collect!{|key| key.empty? ? nil : key.to_sym} end self.assign(keys, value, parameters) end return parameters end |
.decode_www_form(string, maximum = 8, symbolize_keys: false) ⇒ Object
Decode an application/x-www-form-urlencoded string into a hash.
In addition to percent encoding, this format represents spaces using +.
251 252 253 |
# File 'lib/protocol/url/encoding.rb', line 251 def self.decode_www_form(string, maximum = 8, symbolize_keys: false) return self.decode(string.gsub("+", "%20"), maximum, symbolize_keys: symbolize_keys) end |
.encode(value, prefix = nil) ⇒ Object
Encodes a hash or array into a query string. This method is used to encode query parameters in a URL. For example, {"a" => 1, "b" => 2} is encoded as a=1&b=2.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/protocol/url/encoding.rb', line 125 def self.encode(value, prefix = nil) case value when Array return value.map do |v| self.encode(v, "#{prefix}[]") end.join("&") when Hash return value.map do |k, v| self.encode(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s)) end.reject(&:empty?).join("&") when nil return prefix else raise ArgumentError, "value must be a Hash" if prefix.nil? return "#{prefix}=#{escape(value.to_s)}" end end |
.escape(string, encoding = string.encoding) ⇒ Object
Escapes a string using percent encoding, e.g. a b -> a%20b.
22 23 24 25 26 |
# File 'lib/protocol/url/encoding.rb', line 22 def self.escape(string, encoding = string.encoding) string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m| "%" + m.unpack("H2" * m.bytesize).join("%").upcase end.force_encoding(encoding) end |
.escape_fragment(fragment) ⇒ Object
Escapes non-fragment characters using percent encoding. According to RFC 3986 Section 3.5, fragments can contain pchar / "/" / "?" characters.
106 107 108 109 110 111 |
# File 'lib/protocol/url/encoding.rb', line 106 def self.escape_fragment(fragment) encoding = fragment.encoding fragment.b.gsub(NON_FRAGMENT_CHARACTER_PATTERN) do |m| "%" + m.unpack("H2" * m.bytesize).join("%").upcase end.force_encoding(encoding) end |
.scan(string) ⇒ Object
Scan a string for URL-encoded key/value pairs.
148 149 150 151 152 153 154 155 156 |
# File 'lib/protocol/url/encoding.rb', line 148 def self.scan(string) string.split("&") do |assignment| next if assignment.empty? key, value = assignment.split("=", 2) yield unescape(key), value.nil? ? value : unescape(value) end end |
.split(name) ⇒ Object
Split a key into parts, e.g. a[b][c] -> ["a", "b", "c"].
162 163 164 165 166 167 |
# File 'lib/protocol/url/encoding.rb', line 162 def self.split(name) name.scan(/([^\[]+)|(?:\[(.*?)\])/)&.tap do |parts| parts.flatten! parts.compact! end end |
.unescape(string, encoding = string.encoding) ⇒ Object
Unescapes a percent encoded string, e.g. a%20b -> a b.
41 42 43 44 45 46 47 48 49 |
# File 'lib/protocol/url/encoding.rb', line 41 def self.unescape(string, encoding = string.encoding) string.b.gsub(/%([0-9A-Fa-f]{2})?/) do unless hexadecimal = $1 raise ArgumentError, "String contains malformed percent encoding!" end Integer(hexadecimal, 16).chr end.force_encoding(encoding) end |