qasa-url 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +50 -0
- data/lib/qasa/url/version.rb +7 -0
- data/lib/qasa/url.rb +9 -0
- data/lib/url.rb +296 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 15c6031d5e90337cc47cfdd900ff9a6329b9e1da4b1233292383a9cc5fbe60b1
|
4
|
+
data.tar.gz: dfcf70b764fd7b28a3c1411cb8229cb588d788c266b3881118366602cbd63bd5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 213ea1a16224606b1372b1f15bf5653f0951e977bc18f844f57b0f3b94c79de95c6d66decceda0c7ddc5a9c5a4e94fa8b0ae6042fcfabc5dc26ba25006f043ca
|
7
|
+
data.tar.gz: df2d08fa816ca7ee3eb0a7ea5f9260541e99a786c64f29d8c38cf94cecc06ebcb1256e14dfc1977fb3d8ea3a7c3b4951741f26abe11ee8e04af6fd57d0e48aa8
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright 2023 Ingemar Edsborn
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# URL
|
2
|
+
URL is a simple URL parser and construction tool for Ruby. It doesn't follow any RFC, instead, it behaves as you expect.
|
3
|
+
|
4
|
+
## Background
|
5
|
+
This gem was born out of frustration with the URL handling in Ruby's standard library and other gems. I wanted to be able to parse a URL, modify it and then get the modified URL back as a string. I also wanted to be able to join paths and query strings to URLs without having to worry about trailing slashes and question marks.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
```ruby
|
9
|
+
gem "qasa-url"
|
10
|
+
```
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
```ruby
|
14
|
+
# Initialize a new URL object by parsing a URL string:
|
15
|
+
url = URL.parse("http://www.example.com:404/path")
|
16
|
+
url.to_s # => "http://www.example.com:404/path"
|
17
|
+
|
18
|
+
# Modify a URL:
|
19
|
+
url.scheme = "https"
|
20
|
+
url.port = nil
|
21
|
+
url.join("/to")
|
22
|
+
url.join("/nowhere")
|
23
|
+
url.to_s # => "https://www.example.com/path/to/nowhere"
|
24
|
+
|
25
|
+
# Add a query string:
|
26
|
+
url.merge(foo: "bar")
|
27
|
+
url.to_s # => "https://www.example.com/path/to/nowhere?foo=bar"
|
28
|
+
|
29
|
+
# Initialize a URL object with just a domain name:
|
30
|
+
url = URL["example.com"]
|
31
|
+
url.join("/path", "to", "nowhere")
|
32
|
+
|
33
|
+
# Note: If you don't provide a protocol, it'll default to "https":
|
34
|
+
url.to_s # => "https://example.com/path/to/nowhere"
|
35
|
+
```
|
36
|
+
|
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
|
+
check out [addressable](https://github.com/sporkmonger/addressable).
|
40
|
+
If you need to do domain name validation check out [public_suffix](https://github.com/weppos/publicsuffix-ruby).
|
41
|
+
|
42
|
+
## Risks using this gem
|
43
|
+
The risk should be considered low. The gem is very small, simple and well-tested. The only risk is that it doesn't follow any RFCs. This means that it might not behave as you expect. However, it's very unlikely that you'll run into any problems.
|
44
|
+
The gem is maintained by [Qasa](https://www.qasa.se), we're a small team of dedicated Ruby developers.
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
Bug reports and pull requests are always welcome!
|
48
|
+
|
49
|
+
## License
|
50
|
+
See [LICENSE](LICENSE).
|
data/lib/qasa/url.rb
ADDED
data/lib/url.rb
ADDED
@@ -0,0 +1,296 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rack/utils"
|
4
|
+
|
5
|
+
class URL
|
6
|
+
Error = Class.new(StandardError)
|
7
|
+
|
8
|
+
DOT = "."
|
9
|
+
HTTP = "http"
|
10
|
+
HTTPS = "https"
|
11
|
+
NOTHING = ""
|
12
|
+
PORT_SEPARATOR = ":"
|
13
|
+
QUERY_STRING_SEPARATOR = "?"
|
14
|
+
SEPARATOR = "://"
|
15
|
+
SLASH = "/"
|
16
|
+
SPACE = " "
|
17
|
+
|
18
|
+
# @private
|
19
|
+
ERROR_MESSAGES = {
|
20
|
+
"tld=": {
|
21
|
+
missing_tld: "Cannot change top level domain (TLD) on a host initialized without a TLD (%{url})"
|
22
|
+
},
|
23
|
+
"subdomain=": {
|
24
|
+
missing_tld: "Cannot set subdomain on a host with only a second level domain (SLD) (%{url})"
|
25
|
+
}
|
26
|
+
}
|
27
|
+
private_constant :ERROR_MESSAGES
|
28
|
+
|
29
|
+
# @!attribute [rw] host
|
30
|
+
# @return [String]
|
31
|
+
# @param [String]
|
32
|
+
attr_accessor :host
|
33
|
+
|
34
|
+
# @!attribute [rw] port
|
35
|
+
# @param [String]
|
36
|
+
attr_accessor :port
|
37
|
+
|
38
|
+
# @!attribute [rw] query
|
39
|
+
# @return [Hash]
|
40
|
+
attr_accessor :query
|
41
|
+
|
42
|
+
# @!attribute [w] protocol
|
43
|
+
# @param [String]
|
44
|
+
attr_writer :protocol
|
45
|
+
|
46
|
+
# @!method scheme=
|
47
|
+
# @see #protocol=
|
48
|
+
alias_method :scheme=, :protocol=
|
49
|
+
|
50
|
+
# @!attribute [r] path
|
51
|
+
# @return [String]
|
52
|
+
attr_reader :path
|
53
|
+
|
54
|
+
class << self
|
55
|
+
# Initializes a new URL object from a given string
|
56
|
+
# @param [String] the URL to parse
|
57
|
+
# @return [URL] A new URL object
|
58
|
+
# @example
|
59
|
+
# URL.parse("https://www.example.com:3000/path?query=string")
|
60
|
+
# # => #<URL:0x00007f9b9c0b3b20 https://www.example.com:3000/path?query=string>
|
61
|
+
def parse(string)
|
62
|
+
string
|
63
|
+
.to_str
|
64
|
+
.dup
|
65
|
+
.then { extract_protocol(_1) }
|
66
|
+
.then { extract_host_and_port(*_1) }
|
67
|
+
.then { extract_path_and_query(**_1) }
|
68
|
+
.then do
|
69
|
+
next nil if _1.compact.empty?
|
70
|
+
|
71
|
+
new(**_1)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# @!method []
|
76
|
+
# @see #parse
|
77
|
+
alias_method :[], :parse
|
78
|
+
|
79
|
+
# @!visibility private
|
80
|
+
private
|
81
|
+
|
82
|
+
def extract_protocol(string)
|
83
|
+
if string.include?(SEPARATOR)
|
84
|
+
string.split(SEPARATOR)
|
85
|
+
else
|
86
|
+
[nil, string]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def extract_host_and_port(protocol, rest)
|
91
|
+
return {} if rest.nil? || rest.empty?
|
92
|
+
|
93
|
+
host, port = rest.split("/")[0].split("?")[0].split(":")
|
94
|
+
|
95
|
+
{protocol: protocol, host: host, port: port, rest: rest}
|
96
|
+
end
|
97
|
+
|
98
|
+
def extract_path_and_query(protocol: nil, host: nil, port: nil, rest: nil)
|
99
|
+
return {} if rest.nil?
|
100
|
+
|
101
|
+
path, query =
|
102
|
+
if rest.index("/")
|
103
|
+
rest[rest.index("/")..].split("?")
|
104
|
+
else
|
105
|
+
[nil, rest.split("?")[1]]
|
106
|
+
end
|
107
|
+
|
108
|
+
{protocol: protocol, host: host, port: port, path: path, query: query}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
private_class_method :new
|
113
|
+
|
114
|
+
# @private
|
115
|
+
def initialize(host:, path: nil, protocol: nil, port: nil, query: nil)
|
116
|
+
@protocol = protocol
|
117
|
+
@host = host
|
118
|
+
@port = port&.to_i
|
119
|
+
self.path = path
|
120
|
+
@query = Rack::Utils.parse_nested_query(query)
|
121
|
+
end
|
122
|
+
|
123
|
+
def path=(path)
|
124
|
+
@path =
|
125
|
+
if path.nil?
|
126
|
+
nil
|
127
|
+
elsif path.start_with?(SLASH)
|
128
|
+
path.dup
|
129
|
+
else
|
130
|
+
SLASH.dup.concat(path)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Adds a path to the URL
|
135
|
+
# @overload join(path)
|
136
|
+
# @param path [String] a single string path
|
137
|
+
# @overload join(*paths)
|
138
|
+
# @param paths [Array<String>] an array of string paths
|
139
|
+
# @return [URL] self
|
140
|
+
# @example
|
141
|
+
# url = URL.parse("https://www.example.com")
|
142
|
+
# url.join("path").path("to", "nowhere")
|
143
|
+
# url.to_s # => "https://www.example.com/path/to/nowhere"
|
144
|
+
def join(*paths)
|
145
|
+
paths.map do |path|
|
146
|
+
path.start_with?(SLASH) ? path.sub(SLASH, NOTHING) : path.dup
|
147
|
+
end.then do |paths|
|
148
|
+
self.path = Array(path).concat(paths).join(SLASH)
|
149
|
+
end
|
150
|
+
|
151
|
+
self
|
152
|
+
end
|
153
|
+
|
154
|
+
# Append query parameters to the URL
|
155
|
+
# @param [Hash]
|
156
|
+
# @return [URL] self
|
157
|
+
# @example
|
158
|
+
# url = URL.parse("https://www.example.com")
|
159
|
+
# url.merge(query: "string")
|
160
|
+
# url.to_s # => "https://www.example.com?query=string"
|
161
|
+
def merge(query)
|
162
|
+
self.query = self.query.merge(deep_transform_keys(query))
|
163
|
+
|
164
|
+
self
|
165
|
+
end
|
166
|
+
|
167
|
+
# @return [String, HTTPS] the protocol, defaults to "https"
|
168
|
+
def protocol
|
169
|
+
@protocol || HTTPS
|
170
|
+
end
|
171
|
+
alias_method :scheme, :protocol
|
172
|
+
|
173
|
+
# Sets the top level domain (TLD)
|
174
|
+
# @param [String]
|
175
|
+
# @raise [URL::Error] if the host was initialized without a top level domain (TLD)
|
176
|
+
def tld=(new_tld)
|
177
|
+
domain_parts.tap do |parts|
|
178
|
+
raise(Error, ERROR_MESSAGES[:tld=][:missing_tld] % {url: to_s}) if tld.nil?
|
179
|
+
|
180
|
+
@host = [subdomain, sld, new_tld].compact.join(DOT)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
# Returns the top level domain (TLD)
|
185
|
+
# @return [String, nil]
|
186
|
+
def tld
|
187
|
+
domain_parts.last if domain_parts.size > 1
|
188
|
+
end
|
189
|
+
|
190
|
+
# Sets the second level domain (SLD)
|
191
|
+
# @param [String]
|
192
|
+
# @raise [URL::Error] if the host was initialized without a top level domain (TLD)
|
193
|
+
def sld=(new_sld)
|
194
|
+
domain_parts.tap do |parts|
|
195
|
+
@host = [subdomain, new_sld, tld].compact.join(DOT)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# @return [String, nil] the second level domain (SLD)
|
200
|
+
def sld
|
201
|
+
domain_parts.then do |parts|
|
202
|
+
if parts.size < 2
|
203
|
+
parts.last
|
204
|
+
else
|
205
|
+
parts[-2]
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
# Returns the domain name
|
211
|
+
# @return [String]
|
212
|
+
# @example
|
213
|
+
# url = URL.parse("https://www.example.com")
|
214
|
+
# url.domain # => "example.com"
|
215
|
+
def domain
|
216
|
+
[sld, tld].compact.join(DOT)
|
217
|
+
end
|
218
|
+
|
219
|
+
# @param [String]
|
220
|
+
# @return [String]
|
221
|
+
# @raise [URL::Error] if the host was initialized without a top level domain (TLD)
|
222
|
+
def subdomain=(new_subdomain)
|
223
|
+
domain_parts.tap do |parts|
|
224
|
+
raise(Error, ERROR_MESSAGES[:subdomain=][:missing_tld] % {url: to_s}) if tld.nil?
|
225
|
+
|
226
|
+
@host = [new_subdomain, sld, tld].join(DOT)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
# Returns the subdomain
|
231
|
+
# @return [String, nil]
|
232
|
+
def subdomain
|
233
|
+
(domain_parts - [sld, tld].compact).then do |parts|
|
234
|
+
next if parts.empty?
|
235
|
+
|
236
|
+
parts.join(DOT)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
# Returns the full URL as a string
|
241
|
+
# @return [String]
|
242
|
+
def to_str
|
243
|
+
[
|
244
|
+
protocol,
|
245
|
+
SEPARATOR,
|
246
|
+
host,
|
247
|
+
port_str,
|
248
|
+
path_str,
|
249
|
+
query_params
|
250
|
+
].compact.join
|
251
|
+
end
|
252
|
+
alias_method :to_s, :to_str
|
253
|
+
|
254
|
+
# @private
|
255
|
+
def inspect
|
256
|
+
super.split(" ")[0].concat(" #{to_str}>")
|
257
|
+
super.split(SPACE)[0].concat(" #{to_str}>")
|
258
|
+
end
|
259
|
+
|
260
|
+
private
|
261
|
+
|
262
|
+
# @private
|
263
|
+
def query_params
|
264
|
+
if query.any?
|
265
|
+
QUERY_STRING_SEPARATOR.dup.concat(Rack::Utils.build_nested_query(query))
|
266
|
+
else
|
267
|
+
NOTHING
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
# @private
|
272
|
+
def port_str
|
273
|
+
if port
|
274
|
+
PORT_SEPARATOR.dup.concat(port.to_s)
|
275
|
+
else
|
276
|
+
NOTHING
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
# @private
|
281
|
+
def path_str
|
282
|
+
Rack::Utils.escape_path(path) if path && !path.empty?
|
283
|
+
end
|
284
|
+
|
285
|
+
# @private
|
286
|
+
def deep_transform_keys(hash)
|
287
|
+
hash.each_with_object({}) do |(key, value), result|
|
288
|
+
result[key.to_s] =
|
289
|
+
value.is_a?(Hash) ? deep_transform_keys(value) : value
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
def domain_parts
|
294
|
+
host.split(DOT)
|
295
|
+
end
|
296
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qasa-url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ingemar
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '4.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '4.0'
|
33
|
+
description: 'URL is a simple URL parser and construction tool for Ruby. It doesn''t
|
34
|
+
follow any RFC, instead, it behaves as you expect.
|
35
|
+
|
36
|
+
'
|
37
|
+
email:
|
38
|
+
- ingemar@xox.se
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- LICENSE
|
44
|
+
- README.md
|
45
|
+
- lib/qasa/url.rb
|
46
|
+
- lib/qasa/url/version.rb
|
47
|
+
- lib/url.rb
|
48
|
+
homepage: https://github.com/qasase/qasa-url
|
49
|
+
licenses:
|
50
|
+
- Apache-2.0
|
51
|
+
metadata:
|
52
|
+
allowed_push_host: https://rubygems.org
|
53
|
+
homepage_uri: https://github.com/qasase/qasa-url
|
54
|
+
source_code_uri: https://github.com/qasase/qasa-url
|
55
|
+
changelog_uri: https://github.com/qasase/qasa-url/blob/main/CHANGELOG.md
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 3.0.0
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubygems_version: 3.5.2
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: A simple URL parser and construction tool
|
75
|
+
test_files: []
|