qasa-url 0.1.1 → 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 +30 -4
- metadata +3 -3
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
@@ -12,6 +12,7 @@ class URL
|
|
12
12
|
PORT_SEPARATOR = ":"
|
13
13
|
QUERY_STRING_SEPARATOR = "?"
|
14
14
|
SEPARATOR = "://"
|
15
|
+
PROTOCOL_MATCHER = /\A[a-z0-9.+-]*#{Regexp.escape(SEPARATOR)}/io
|
15
16
|
SLASH = "/"
|
16
17
|
SPACE = " "
|
17
18
|
|
@@ -82,8 +83,8 @@ class URL
|
|
82
83
|
private
|
83
84
|
|
84
85
|
def extract_protocol(string)
|
85
|
-
if string.
|
86
|
-
string.split(SEPARATOR)
|
86
|
+
if string.match?(PROTOCOL_MATCHER)
|
87
|
+
string.split(SEPARATOR, 2)
|
87
88
|
else
|
88
89
|
[nil, string]
|
89
90
|
end
|
@@ -138,7 +139,7 @@ class URL
|
|
138
139
|
# @param path [String] a single string path
|
139
140
|
# @overload join(*paths)
|
140
141
|
# @param paths [Array<String>] an array of string paths
|
141
|
-
# @return [URL] self
|
142
|
+
# @return [URL] duplicate of self
|
142
143
|
# @example
|
143
144
|
# url = URL.parse("https://www.example.com")
|
144
145
|
# url.join("path").path("to", "nowhere")
|
@@ -148,6 +149,18 @@ class URL
|
|
148
149
|
# url.join("/path", "/to/", "nowhere/")
|
149
150
|
# url.to_s # => "https://www.example.com/path/to/nowhere/"
|
150
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)
|
151
164
|
parts = Array(path).concat(paths)
|
152
165
|
size = parts.size
|
153
166
|
|
@@ -164,12 +177,25 @@ class URL
|
|
164
177
|
|
165
178
|
# Append query parameters to the URL
|
166
179
|
# @param [Hash]
|
167
|
-
# @return [URL] self
|
180
|
+
# @return [URL] duplicate of self
|
168
181
|
# @example
|
169
182
|
# url = URL.parse("https://www.example.com")
|
170
183
|
# url.merge(query: "string")
|
171
184
|
# url.to_s # => "https://www.example.com?query=string"
|
172
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)
|
173
199
|
self.query = self.query.merge(deep_transform_keys(query))
|
174
200
|
|
175
201
|
self
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qasa-url
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ingemar
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: rack
|
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
- !ruby/object:Gem::Version
|
67
67
|
version: '0'
|
68
68
|
requirements: []
|
69
|
-
rubygems_version: 3.6.
|
69
|
+
rubygems_version: 3.6.9
|
70
70
|
specification_version: 4
|
71
71
|
summary: A simple URL parser and construction tool
|
72
72
|
test_files: []
|