qasa-url 0.1.2 → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +13 -11
- data/lib/qasa/url/version.rb +1 -1
- data/lib/url.rb +27 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da87a5058a88b4fd8f610e7950afb30f03453d7e9fe67d4c8682bad3699608a8
|
4
|
+
data.tar.gz: 5552a59d77879d045c20819031a2521df3eb5ab1c1a767b53b71a656116eaf82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d96e2f786ca8ec0a93edf2d662e691ecf7e996d73519e5cdfea3fade2ae8bf339d56b35f7732fb8b9c90477aeb2ef00e2e53c7117a5fe6a353ab334b89978c78
|
7
|
+
data.tar.gz: 46fe970aefe8cd4d4f7e6d3c300c426e4db4ce6430b93b120f35ad70149a2aa114ca321b98b3155551278fe2aecd322e9a5da82858a5f80f281d55f5bfffdbf1
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# URL
|
2
|
-
URL is a simple URL parser and construction tool for Ruby. It doesn't follow any
|
2
|
+
**URL** is a simple URL parser and construction tool for Ruby. It doesn't strictly follow any RFCs — instead, **it behaves as you’d expect**.
|
3
3
|
|
4
4
|
## Background
|
5
|
-
This gem was born out of frustration with the URL handling in Ruby
|
5
|
+
This gem was born out of frustration with the URL handling in Ruby’s standard library and other gems.
|
6
|
+
I wanted to parse a URL, modify it, and then convert it back to a string. I also wanted to join paths and query strings to URLs without worrying about trailing slashes or question marks.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
```ruby
|
@@ -11,7 +12,7 @@ gem "qasa-url"
|
|
11
12
|
|
12
13
|
## Usage
|
13
14
|
```ruby
|
14
|
-
# Initialize a new URL object
|
15
|
+
# Initialize a new URL object from a string:
|
15
16
|
url = URL.parse("http://www.example.com:404/path")
|
16
17
|
url.to_s # => "http://www.example.com:404/path"
|
17
18
|
|
@@ -30,21 +31,22 @@ url.to_s # => "https://www.example.com/path/to/nowhere?foo=bar"
|
|
30
31
|
url = URL["example.com"]
|
31
32
|
url.join("/path", "to", "nowhere")
|
32
33
|
|
33
|
-
# Note: If
|
34
|
+
# Note: If no protocol is provided, it defaults to "https":
|
34
35
|
url.to_s # => "https://example.com/path/to/nowhere"
|
35
36
|
```
|
36
37
|
|
37
|
-
## Alternatives
|
38
|
-
If you're looking for something that parses URLs in Ruby and Ruby on Rails and closely conforms to RFC 3986, RFC 3987, and RFC 6570 (level 4),
|
39
|
-
|
40
|
-
If you need to do domain name validation check out [public_suffix](https://github.com/weppos/publicsuffix-ruby).
|
38
|
+
## Alternatives
|
39
|
+
If you're looking for something that parses URLs in Ruby and Ruby on Rails and closely conforms to RFC 3986, RFC 3987, and RFC 6570 (level 4), check out [addressable](https://github.com/sporkmonger/addressable).
|
40
|
+
For domain name validation, see [public_suffix](https://github.com/weppos/publicsuffix-ruby).
|
41
41
|
|
42
42
|
## Risks using this gem
|
43
|
-
The
|
44
|
-
The
|
43
|
+
The risks of using this gem are very low. It’s small, simple, and well-tested.
|
44
|
+
The main caveat is that it doesn’t follow any RFCs, so behavior may differ in edge cases. That said, it’s unlikely you’ll encounter issues in practice.
|
45
|
+
|
46
|
+
This gem is actively maintained by [Qasa](https://www.qasa.se), a small team of dedicated Ruby developers.
|
45
47
|
|
46
48
|
## Contributing
|
47
|
-
Bug reports and pull requests are always welcome
|
49
|
+
**Bug reports and pull requests are always welcome!**
|
48
50
|
|
49
51
|
## License
|
50
52
|
See [LICENSE](LICENSE).
|
data/lib/qasa/url/version.rb
CHANGED
data/lib/url.rb
CHANGED
@@ -139,7 +139,7 @@ class URL
|
|
139
139
|
# @param path [String] a single string path
|
140
140
|
# @overload join(*paths)
|
141
141
|
# @param paths [Array<String>] an array of string paths
|
142
|
-
# @return [URL] self
|
142
|
+
# @return [URL] duplicate of self
|
143
143
|
# @example
|
144
144
|
# url = URL.parse("https://www.example.com")
|
145
145
|
# url.join("path").path("to", "nowhere")
|
@@ -149,6 +149,18 @@ class URL
|
|
149
149
|
# url.join("/path", "/to/", "nowhere/")
|
150
150
|
# url.to_s # => "https://www.example.com/path/to/nowhere/"
|
151
151
|
def join(*paths)
|
152
|
+
dup = self.dup
|
153
|
+
|
154
|
+
dup.join!(*paths)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Adds a path to the URL
|
158
|
+
# @overload join(path)
|
159
|
+
# @param path [String] a single string path
|
160
|
+
# @overload join(*paths)
|
161
|
+
# @param paths [Array<String>] an array of string paths
|
162
|
+
# @return [URL] self
|
163
|
+
def join!(*paths)
|
152
164
|
parts = Array(path).concat(paths)
|
153
165
|
size = parts.size
|
154
166
|
|
@@ -165,12 +177,25 @@ class URL
|
|
165
177
|
|
166
178
|
# Append query parameters to the URL
|
167
179
|
# @param [Hash]
|
168
|
-
# @return [URL] self
|
180
|
+
# @return [URL] duplicate of self
|
169
181
|
# @example
|
170
182
|
# url = URL.parse("https://www.example.com")
|
171
183
|
# url.merge(query: "string")
|
172
184
|
# url.to_s # => "https://www.example.com?query=string"
|
173
185
|
def merge(query)
|
186
|
+
dup = self.dup
|
187
|
+
|
188
|
+
dup.merge!(query)
|
189
|
+
end
|
190
|
+
|
191
|
+
# Append query parameters to the URL
|
192
|
+
# @param [Hash]
|
193
|
+
# @return [URL] self
|
194
|
+
# @example
|
195
|
+
# url = URL.parse("https://www.example.com")
|
196
|
+
# url.merge(query: "string")
|
197
|
+
# url.to_s # => "https://www.example.com?query=string"
|
198
|
+
def merge!(query)
|
174
199
|
self.query = self.query.merge(deep_transform_keys(query))
|
175
200
|
|
176
201
|
self
|