xrb 0.11.2 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/xrb/uri.rb DELETED
@@ -1,100 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2017-2024, by Samuel Williams.
5
-
6
- module XRB
7
- # This class is superceeded by `XRB::Reference`.
8
- class URI
9
- def initialize(path, query_string, fragment, parameters)
10
- @path = path
11
- @query_string = query_string
12
- @fragment = fragment
13
- @parameters = parameters
14
- end
15
-
16
- # The path component of the URI, e.g. /foo/bar/index.html
17
- attr :path
18
-
19
- # The un-parsed query string of the URI, e.g. 'x=10&y=20'
20
- attr :query_string
21
-
22
- # A fragment identifier, the part after the '#'
23
- attr :fragment
24
-
25
- # User supplied parameters that will be appended to the query part.
26
- attr :parameters
27
-
28
- def append(buffer)
29
- if @query_string
30
- buffer << escape_path(@path) << "?" << query_string
31
- buffer << "&" << query_parameters if @parameters
32
- else
33
- buffer << escape_path(@path)
34
- buffer << "?" << query_parameters if @parameters
35
- end
36
-
37
- if @fragment
38
- buffer << "#" << escape(@fragment)
39
- end
40
-
41
- return buffer
42
- end
43
-
44
- def to_str
45
- append(String.new)
46
- end
47
-
48
- alias to_s to_str
49
-
50
- private
51
-
52
- # According to https://tools.ietf.org/html/rfc3986#section-3.3, we escape non-pchar.
53
- NON_PCHAR = /([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/]+)/.freeze
54
-
55
- def escape_path(path)
56
- encoding = path.encoding
57
- path.b.gsub(NON_PCHAR) do |m|
58
- "%" + m.unpack("H2" * m.bytesize).join("%").upcase
59
- end.force_encoding(encoding)
60
- end
61
-
62
- # Escapes a generic string, using percent encoding.
63
- def escape(string)
64
- encoding = string.encoding
65
- string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
66
- "%" + m.unpack("H2" * m.bytesize).join("%").upcase
67
- end.force_encoding(encoding)
68
- end
69
-
70
- def query_parameters
71
- build_nested_query(@parameters)
72
- end
73
-
74
- def build_nested_query(value, prefix = nil)
75
- case value
76
- when Array
77
- value.map { |v|
78
- build_nested_query(v, "#{prefix}[]")
79
- }.join("&")
80
- when Hash
81
- value.map { |k, v|
82
- build_nested_query(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
83
- }.reject(&:empty?).join("&")
84
- when nil
85
- prefix
86
- else
87
- raise ArgumentError, "value must be a Hash" if prefix.nil?
88
- "#{prefix}=#{escape(value.to_s)}"
89
- end
90
- end
91
- end
92
-
93
- # Generate a URI from a path and user parameters. The path may contain a `#fragment` or `?query=parameters`.
94
- def self.URI(path = "", parameters = nil)
95
- base, fragment = path.split("#", 2)
96
- path, query_string = base.split("?", 2)
97
-
98
- URI.new(path, query_string, fragment, parameters)
99
- end
100
- end