uri-builder 0.1.12 → 0.1.13

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
  SHA256:
3
- metadata.gz: 2c35a6790d4fa14b69e8dbe73f2ce8f5b7814fd4f125aff996cdbb228d417375
4
- data.tar.gz: 20e34111ccfc4a4440f4a4c0ffb126e4a9109aeb24decc9fe825af0942ad6195
3
+ metadata.gz: 2a0af25288f9f2c4f6385d2a7ad29016951a1c9048c40722d893b1b49151c5e5
4
+ data.tar.gz: 370b0a62178447bcd9c313c6fa522ac77926a793af19765a0c1bab9ff167fcb9
5
5
  SHA512:
6
- metadata.gz: f9c635ee513fe5f7e9854185e2d8e8d346560a9165fbbbda30f9b465c8bb6c6d7939137632ed2a7546ee1876736bc36f42299c4e84451bed44b57fae0651c061
7
- data.tar.gz: 5263fa6e147a0e8c23a57fc3401b7ba95ba47d8aa65b280c8145bd55b8d6d69208b2f5d5c07668cea90a10a5562352c57a9939378f5c64b22fe7d329a722d610
6
+ metadata.gz: f92b5696f4694868faafd3a0ebec001105431e60fcd656c0eb4d547251903aaefe09cb349a139d65004c84bbd8c46401ad3778e42b491d2337678e67df2a55d6
7
+ data.tar.gz: 4da7c5653b3fc86f37eab3ff04a50f352957e91716dcc011f452fa2669a7ea9e522f29cebd12ab833947202c298454c0c1322fcfed5421a5c9c1f846f84b549b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- uri-builder (0.1.12)
4
+ uri-builder (0.1.13)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -9,7 +9,7 @@ URI.build("https://www.example.com/api/v1").path("/api/v2").query(search: "great
9
9
  Or if you prefer a block format that automatically converts back to an URI object after the transformation.
10
10
 
11
11
  ```ruby
12
- URI.build("https://www.example.com/api/v1") { _1.path("/api/v2").query search: "great books" }
12
+ URI.build("https://www.example.com/api/v1") { it.path("/api/v2").query search: "great books" }
13
13
  ```
14
14
 
15
15
  Compare that to:
@@ -36,6 +36,25 @@ uri.query = URI.encode_www_form(first_name: "Brad")
36
36
  uri
37
37
  ```
38
38
 
39
+ Paths may be traversed with various methods:
40
+
41
+ ```ruby
42
+ URI.build("https://www.example.com/api/v1") { it.join("books/search").query search: "great books" } # => "https://www.example.com/api/v1/books/search?search=great+books"
43
+
44
+ URI.build("https://www.example.com/api/v1") { it.parent.join("v2/articles/search").query search: "great books" }.uri # => #<URI::HTTPS https://www.example.com/api/v2/articles/search?search=great+books>
45
+
46
+ URI.build("https://www.example.com/api/v1") { it.root.join("about").uri # => #<URI::HTTPS https://www.example.com/about>
47
+ ```
48
+
49
+ Compare that to:
50
+
51
+ ```ruby
52
+ uri = URI("https://www.example.com/api/v1")
53
+ uri.path = uri.path + "/books/search"
54
+ uri.query = URI.encode_www_form(search: "great books")
55
+ uri
56
+ ```
57
+
39
58
  Each chain creates a duplicate of the original URL, so you can transform away without worrying about thrashing the original URL object.
40
59
 
41
60
  ## Installation
@@ -2,6 +2,6 @@
2
2
 
3
3
  module URI
4
4
  module Build
5
- VERSION = "0.1.12"
5
+ VERSION = "0.1.13"
6
6
  end
7
7
  end
data/lib/uri/builder.rb CHANGED
@@ -7,6 +7,32 @@ module URI
7
7
  module Builder
8
8
  class Error < StandardError; end
9
9
 
10
+ class Path
11
+ File = ::File
12
+
13
+ def initialize(*segments)
14
+ @trailing_slash = segments.last.to_s.end_with?("/")
15
+ @segments = segments.compact.flat_map { _1.to_s.split("/") }
16
+ end
17
+
18
+ def join(*segments)
19
+ self.class.new(*@segments, *segments)
20
+ end
21
+
22
+ def parent
23
+ self.class.new(*@segments[0...-1])
24
+ end
25
+
26
+ def to_s
27
+ File.join("/", *@segments.map(&:to_s).tap { _1.append "/" if @trailing_slash })
28
+ end
29
+
30
+ def trailing_slash(value = true)
31
+ @trailing_slash = value
32
+ self
33
+ end
34
+ end
35
+
10
36
  class DSL
11
37
  attr_reader :uri
12
38
 
@@ -58,19 +84,28 @@ module URI
58
84
 
59
85
  def path(*segments)
60
86
  # Make sure there's a leading / if a non leading / is given.
61
- wrap :path, ::File.join(*segments.compact.map(&:to_s).prepend("/"))
87
+ wrap :path, Path.new(*segments).to_s
62
88
  end
63
89
 
64
90
  def clear_path
65
91
  path "/"
66
92
  end
93
+ alias :root :clear_path
67
94
 
68
95
  def trailing_slash
69
- wrap :path, @uri.path + "/" unless @uri.path.end_with?("/")
96
+ wrap :path, Path.new(@uri.path).trailing_slash(true).to_s
70
97
  end
71
98
 
72
99
  def clear_trailing_slash
73
- wrap :path, @uri.path.chomp("/") if @uri.path.end_with?("/") and @uri.path != "/"
100
+ wrap :path, Path.new(@uri.path).trailing_slash(false).to_s
101
+ end
102
+
103
+ def join(...)
104
+ wrap :path, Path.new(@uri.path).join(...).to_s
105
+ end
106
+
107
+ def parent(...)
108
+ wrap :path, Path.new(@uri.path).parent(...).to_s
74
109
  end
75
110
 
76
111
  def to_s
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uri-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-11-12 00:00:00.000000000 Z
10
+ date: 2025-02-07 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Build URIs via chains
14
13
  email:
@@ -36,7 +35,6 @@ metadata:
36
35
  homepage_uri: https://github.com/rubymonolith/uri-builder
37
36
  source_code_uri: https://github.com/rubymonolith/uri-builder
38
37
  changelog_uri: https://github.com/rubymonolith/uri-builder
39
- post_install_message:
40
38
  rdoc_options: []
41
39
  require_paths:
42
40
  - lib
@@ -51,8 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
49
  - !ruby/object:Gem::Version
52
50
  version: '0'
53
51
  requirements: []
54
- rubygems_version: 3.5.16
55
- signing_key:
52
+ rubygems_version: 3.6.2
56
53
  specification_version: 4
57
54
  summary: Build URIs via chains
58
55
  test_files: []