uri-builder 0.1.11 → 0.1.13

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e06f8d4bcafa879c5e15eca5eea2465375d069f45dff718f3cb2d2639370ad64
4
- data.tar.gz: 2fb08af317f38650f039793e95a8d02ef01ec56029dc899190e79342ea500006
3
+ metadata.gz: 2a0af25288f9f2c4f6385d2a7ad29016951a1c9048c40722d893b1b49151c5e5
4
+ data.tar.gz: 370b0a62178447bcd9c313c6fa522ac77926a793af19765a0c1bab9ff167fcb9
5
5
  SHA512:
6
- metadata.gz: 5715d70198f6cc9bff67b5f51aac33b6d28966ae4c85909b0b34f890ec8102d5a960691093b554237bc6aef571812b730c93a131268844454013254313f4daad
7
- data.tar.gz: 8331306c424006462e51f310903e67c9f9f6c41987788e6e917302633ab9eda1d661978615341499c588ddc566012468ba2105878691e48c411f700cb0e8fbf7
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.11)
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.11"
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
 
@@ -14,12 +40,16 @@ module URI
14
40
  @uri = uri.clone
15
41
  end
16
42
 
17
- [:host, :query, :fragment, :port].each do |property|
43
+ [:host, :fragment, :port].each do |property|
18
44
  define_method property do |value|
19
45
  wrap property, value
20
46
  end
21
47
  end
22
48
 
49
+ def clear_fragment
50
+ wrap :fragment, nil
51
+ end
52
+
23
53
  def scheme(value)
24
54
  if @uri.scheme
25
55
  # Handles URLs without schemes, like https://example.com/foo
@@ -48,17 +78,34 @@ module URI
48
78
  wrap :query, value
49
79
  end
50
80
 
81
+ def clear_query
82
+ wrap :query, nil
83
+ end
84
+
51
85
  def path(*segments)
52
86
  # Make sure there's a leading / if a non leading / is given.
53
- wrap :path, ::File.join(*segments.compact.map(&:to_s).prepend("/"))
87
+ wrap :path, Path.new(*segments).to_s
54
88
  end
55
89
 
90
+ def clear_path
91
+ path "/"
92
+ end
93
+ alias :root :clear_path
94
+
56
95
  def trailing_slash
57
- wrap :path, @uri.path + "/" unless @uri.path.end_with?("/")
96
+ wrap :path, Path.new(@uri.path).trailing_slash(true).to_s
97
+ end
98
+
99
+ def clear_trailing_slash
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
58
105
  end
59
106
 
60
- def remove_trailing_slash
61
- wrap :path, @uri.path.chomp("/") if @uri.path.end_with?("/") and @uri.path != "/"
107
+ def parent(...)
108
+ wrap :path, Path.new(@uri.path).parent(...).to_s
62
109
  end
63
110
 
64
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.11
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-11 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: []