Skip to content

Commit 72a4a81

Browse files
committed
Improved documentation for Protocol::HTTPL::URL.
1 parent eeaaa06 commit 72a4a81

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

lib/protocol/http/url.rb

+33-8
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,44 @@
77
module Protocol
88
module HTTP
99
module URL
10-
# Escapes a string using percent encoding.
10+
# Escapes a string using percent encoding, e.g. `a b` -> `a%20b`.
11+
#
12+
# @parameter string [String] The string to escape.
13+
# @returns [String] The escaped string.
1114
def self.escape(string, encoding = string.encoding)
1215
string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
1316
"%" + m.unpack("H2" * m.bytesize).join("%").upcase
1417
end.force_encoding(encoding)
1518
end
1619

17-
# Unescapes a percent encoded string.
20+
# Unescapes a percent encoded string, e.g. `a%20b` -> `a b`.
21+
#
22+
# @parameter string [String] The string to unescape.
23+
# @returns [String] The unescaped string.
1824
def self.unescape(string, encoding = string.encoding)
1925
string.b.gsub(/%(\h\h)/) do |hex|
2026
Integer($1, 16).chr
2127
end.force_encoding(encoding)
2228
end
2329

24-
# According to https://tools.ietf.org/html/rfc3986#section-3.3, we escape non-pchar.
25-
NON_PCHAR = /([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/]+)/.freeze
30+
# Matches characters that are not allowed in a URI path segment. According to RFC 3986 Section 3.3 (https://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.
31+
NON_PATH_CHARACTER_PATTERN = /([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/]+)/.freeze
2632

27-
# Escapes non-path characters using percent encoding.
33+
# 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 (https://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.
34+
#
35+
# @parameter path [String] The path to escape.
36+
# @returns [String] The escaped path.
2837
def self.escape_path(path)
2938
encoding = path.encoding
30-
path.b.gsub(NON_PCHAR) do |m|
39+
path.b.gsub(NON_PATH_CHARACTER_PATTERN) do |m|
3140
"%" + m.unpack("H2" * m.bytesize).join("%").upcase
3241
end.force_encoding(encoding)
3342
end
3443

35-
# Encodes a hash or array into a query string.
44+
# 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`.
45+
#
46+
# @parameter value [Hash, Array] The value to encode.
47+
# @parameter prefix [String] The prefix to use for keys.
3648
def self.encode(value, prefix = nil)
3749
case value
3850
when Array
@@ -66,6 +78,10 @@ def self.scan(string)
6678
end
6779
end
6880

81+
# Split a key into parts, e.g. `a[b][c]` -> `["a", "b", "c"]`.
82+
#
83+
# @parameter name [String] The key to split.
84+
# @returns [Array(String)] The parts of the key.
6985
def self.split(name)
7086
name.scan(/([^\[]+)|(?:\[(.*?)\])/)&.tap do |parts|
7187
parts.flatten!
@@ -74,6 +90,10 @@ def self.split(name)
7490
end
7591

7692
# Assign a value to a nested hash.
93+
#
94+
# @parameter keys [Array(String)] The parts of the key.
95+
# @parameter value [Object] The value to assign.
96+
# @parameter parent [Hash] The parent hash.
7797
def self.assign(keys, value, parent)
7898
top, *middle = keys
7999

@@ -94,7 +114,12 @@ def self.assign(keys, value, parent)
94114
parent[top] = value
95115
end
96116

97-
# TODO use native C extension from `Trenni::Reference`.
117+
# Decode a URL-encoded query string into a hash.
118+
#
119+
# @parameter string [String] The query string to decode.
120+
# @parameter maximum [Integer] The maximum number of keys in a path.
121+
# @parameter symbolize_keys [Boolean] Whether to symbolize keys.
122+
# @returns [Hash] The decoded query string.
98123
def self.decode(string, maximum = 8, symbolize_keys: false)
99124
parameters = {}
100125

0 commit comments

Comments
 (0)