Module: Protocol::HTTP::URL

Defined in:
lib/protocol/http/url.rb

Overview

Helpers for working with URLs.

Constant Summary collapse

NON_PATH_CHARACTER_PATTERN =

Matches characters that are not allowed in a URI path segment. According to RFC 3986 Section 3.3 (tools.ietf.org/html/rfc3986#section-3.3), a valid path segment consists of “pchar” characters. This pattern identifies characters that must be percent-encoded when included in a URI path segment.

/([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/]+)/.freeze

Class Method Summary collapse

Class Method Details

.assign(keys, value, parent) ⇒ Object

Assign a value to a nested hash.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/protocol/http/url.rb', line 98

def self.assign(keys, value, parent)
	top, *middle = keys
	
	middle.each_with_index do |key, index|
		if key.nil? or key.empty?
			parent = (parent[top] ||= Array.new)
			top = parent.size
			
			if nested = middle[index+1] and last = parent.last
				top -= 1 unless last.include?(nested)
			end
		else
			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.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/protocol/http/url.rb', line 124

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

.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, ‘=> 1, “b” => 2` is encoded as `a=1&b=2`.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/protocol/http/url.rb', line 49

def self.encode(value, prefix = nil)
	case value
	when Array
		return value.map {|v|
			self.encode(v, "#{prefix}[]")
		}.join("&")
	when Hash
		return value.map {|k, v|
			self.encode(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
		}.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`.



15
16
17
18
19
# File 'lib/protocol/http/url.rb', line 15

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_path(path) ⇒ Object

Escapes non-path characters using percent encoding. In other words, this method escapes characters that are not allowed in a URI path segment. According to RFC 3986 Section 3.3 (tools.ietf.org/html/rfc3986#section-3.3), a valid path segment consists of “pchar” characters. This method percent-encodes characters that are not “pchar” characters.



38
39
40
41
42
43
# File 'lib/protocol/http/url.rb', line 38

def self.escape_path(path)
	encoding = path.encoding
	path.b.gsub(NON_PATH_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.



72
73
74
75
76
77
78
79
80
# File 'lib/protocol/http/url.rb', line 72

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[c]` -> `[“a”, “b”, “c”]`.



86
87
88
89
90
91
# File 'lib/protocol/http/url.rb', line 86

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`.



25
26
27
28
29
# File 'lib/protocol/http/url.rb', line 25

def self.unescape(string, encoding = string.encoding)
	string.b.gsub(/%(\h\h)/) do |hex|
		Integer($1, 16).chr
	end.force_encoding(encoding)
end