trenni 3.1.0 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ca1689a6184db9df2773f6d3b4b6211ed558f1a
4
- data.tar.gz: 97d4dc5f0e2fa4f7f165519bc8f3c86c8e36c822
3
+ metadata.gz: '081ca65498a2ec0527b25ce54a105bc8e19341a0'
4
+ data.tar.gz: 224372fa6f3c9715858d8b1c700a050c33cf226c
5
5
  SHA512:
6
- metadata.gz: c57c4f0c3463d35c0d73142be0451ea2243901fc67801d36f9dded3acc1118a85104be7138da3a442028e267333099772d2f867e6517ab0c6abb47acf731dc2a
7
- data.tar.gz: 1b84a6d7d545ac7fc8de48c203f8d8cd1ab8652ce422f6c8da1e754d2d67023db807e96d2c236926ad34d88b4cf1f938c5a60ca498daf3a3f1c27b305f7d6ccc
6
+ metadata.gz: 3235d940fbf6eff830ab88623dc46fae3f92c6a31b20f2f37f28cac9f9f304cfdc7eb3a1d8eb0692bc5578aac09ffeb72763b3d594e51b08c4d8cb51b2aca300
7
+ data.tar.gz: 9d47d3e2feb3e03b42a9abf43430113c0aef7e1e4d25bc23aaafd2c2119d620810cc1bb10a50cc92f545a605abcfce918544f91d48d05a6a3641b675e4d8a8f2
@@ -20,21 +20,32 @@
20
20
 
21
21
  module Trenni
22
22
  class URI
23
- def initialize(base, fragment, query)
24
- @base = base
23
+ def initialize(path, query_string, fragment, parameters)
24
+ @path = path
25
+ @query_string = query_string
25
26
  @fragment = fragment
26
- @query = query
27
+ @parameters = parameters
27
28
  end
28
29
 
29
- attr :base
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
- attr :query
38
+
39
+ # User supplied parameters that will be appended to the query part.
40
+ attr :parameters
32
41
 
33
42
  def append(buffer)
34
- buffer << escape_path(@base)
35
-
36
- if @query&.any?
37
- buffer << query_separator << query_part
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
- # Escapes a path string, using percent encoding, but additionally ignoring "/" and substituting spaces with "+".
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(/([^ a-zA-Z0-9_.\-\/:]+)/) do |m|
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 query_part
72
- @query.map{|k,v| "#{escape(k.to_s)}=#{escape(v.to_s)}"}.join('&')
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
- def self.URI(path = '', query = nil)
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(base, fragment, query)
94
+ URI.new(path, query_string, fragment, parameters)
84
95
  end
85
96
  end
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Trenni
22
- VERSION = "3.1.0"
22
+ VERSION = "3.1.1"
23
23
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trenni
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams