trenni 3.1.0 → 3.1.1
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.
- checksums.yaml +4 -4
- data/lib/trenni/uri.rb +30 -19
- data/lib/trenni/version.rb +1 -1
- data/spec/trenni/uri_spec.rb +13 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '081ca65498a2ec0527b25ce54a105bc8e19341a0'
|
4
|
+
data.tar.gz: 224372fa6f3c9715858d8b1c700a050c33cf226c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3235d940fbf6eff830ab88623dc46fae3f92c6a31b20f2f37f28cac9f9f304cfdc7eb3a1d8eb0692bc5578aac09ffeb72763b3d594e51b08c4d8cb51b2aca300
|
7
|
+
data.tar.gz: 9d47d3e2feb3e03b42a9abf43430113c0aef7e1e4d25bc23aaafd2c2119d620810cc1bb10a50cc92f545a605abcfce918544f91d48d05a6a3641b675e4d8a8f2
|
data/lib/trenni/uri.rb
CHANGED
@@ -20,21 +20,32 @@
|
|
20
20
|
|
21
21
|
module Trenni
|
22
22
|
class URI
|
23
|
-
def initialize(
|
24
|
-
@
|
23
|
+
def initialize(path, query_string, fragment, parameters)
|
24
|
+
@path = path
|
25
|
+
@query_string = query_string
|
25
26
|
@fragment = fragment
|
26
|
-
@
|
27
|
+
@parameters = parameters
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
+
# The path component of the URI, e.g. /foo/bar/index.html
|
31
|
+
attr :path
|
32
|
+
|
33
|
+
# The un-parsed query string of the URI, e.g. 'x=10&y=20'
|
34
|
+
attr :query_string
|
35
|
+
|
36
|
+
# A fragment identifier, the part after the '#'
|
30
37
|
attr :fragment
|
31
|
-
|
38
|
+
|
39
|
+
# User supplied parameters that will be appended to the query part.
|
40
|
+
attr :parameters
|
32
41
|
|
33
42
|
def append(buffer)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
43
|
+
if @query_string
|
44
|
+
buffer << escape_path(@path) << '?' << query_string
|
45
|
+
buffer << '&' << query_parameters if @parameters
|
46
|
+
else
|
47
|
+
buffer << escape_path(@path)
|
48
|
+
buffer << '?' << query_parameters if @parameters
|
38
49
|
end
|
39
50
|
|
40
51
|
if @fragment
|
@@ -52,10 +63,12 @@ module Trenni
|
|
52
63
|
|
53
64
|
private
|
54
65
|
|
55
|
-
#
|
66
|
+
# According to https://tools.ietf.org/html/rfc3986#section-3.3, we escape non-pchar.
|
67
|
+
NON_PCHAR = /([^ a-zA-Z0-9\-\.~!$&'()*+,;=:@\/]+)/.freeze
|
68
|
+
|
56
69
|
def escape_path(path)
|
57
70
|
encoding = path.encoding
|
58
|
-
path.b.gsub(
|
71
|
+
path.b.gsub(NON_PCHAR) do |m|
|
59
72
|
'%' + m.unpack('H2' * m.bytesize).join('%').upcase
|
60
73
|
end.tr(' ', '+').force_encoding(encoding)
|
61
74
|
end
|
@@ -68,18 +81,16 @@ module Trenni
|
|
68
81
|
end.force_encoding(encoding)
|
69
82
|
end
|
70
83
|
|
71
|
-
def
|
72
|
-
@
|
73
|
-
end
|
74
|
-
|
75
|
-
def query_separator
|
76
|
-
@base.include?('?') ? '&' : '?'
|
84
|
+
def query_parameters
|
85
|
+
@parameters.map{|k,v| "#{escape(k.to_s)}=#{escape(v.to_s)}"}.join('&')
|
77
86
|
end
|
78
87
|
end
|
79
88
|
|
80
|
-
|
89
|
+
# Generate a URI from a path and user parameters. The path may contain a `#fragment` or `?query=parameters`.
|
90
|
+
def self.URI(path = '', parameters = nil)
|
81
91
|
base, fragment = path.split('#', 2)
|
92
|
+
path, query_string = base.split('?', 2)
|
82
93
|
|
83
|
-
URI.new(
|
94
|
+
URI.new(path, query_string, fragment, parameters)
|
84
95
|
end
|
85
96
|
end
|
data/lib/trenni/version.rb
CHANGED
data/spec/trenni/uri_spec.rb
CHANGED
@@ -52,4 +52,17 @@ RSpec.describe Trenni::URI do
|
|
52
52
|
|
53
53
|
expect(tag.to_s).to be == '<img src="image.jpg?x=10"/>'
|
54
54
|
end
|
55
|
+
|
56
|
+
describe Trenni::URI("foo?bar=10&baz=20", yes: 'no') do
|
57
|
+
it "can use existing query parameters" do
|
58
|
+
expect(subject.to_s).to be == "foo?bar=10&baz=20&yes=no"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe Trenni::URI('foo#frag') do
|
63
|
+
it "can use existing fragment" do
|
64
|
+
expect(subject.fragment).to be == "frag"
|
65
|
+
expect(subject.to_s).to be == 'foo#frag'
|
66
|
+
end
|
67
|
+
end
|
55
68
|
end
|