Class: URL
- Inherits:
-
Object
- Object
- URL
- Defined in:
- lib/url.rb
Constant Summary collapse
- Error =
Class.new(StandardError)
- DOT =
"."
- HTTP =
"http"
- HTTPS =
"https"
- NOTHING =
""
- PORT_SEPARATOR =
":"
- QUERY_STRING_SEPARATOR =
"?"
- SEPARATOR =
"://"
- PROTOCOL_MATCHER =
/\A[a-z0-9.+-]*#{Regexp.escape(SEPARATOR)}/io
- SLASH =
"/"
- SPACE =
" "
Instance Attribute Summary collapse
- #host ⇒ String
-
#path ⇒ Object
Returns the value of attribute path.
- #port ⇒ Object
-
#protocol ⇒ String, HTTPS
(also: #scheme)
The protocol, defaults to “https”.
- #query ⇒ Hash
Class Method Summary collapse
-
.parse(string) ⇒ URL
(also: [])
Initializes a new URL object from a given string.
Instance Method Summary collapse
-
#domain ⇒ String
Returns the domain name.
-
#initialize(host:, path: nil, protocol: nil, port: nil, query: nil) ⇒ URL
constructor
A new instance of URL.
- #inspect ⇒ Object
-
#join(*paths) ⇒ URL
Adds a path to the URL.
-
#join!(*paths) ⇒ URL
Adds a path to the URL.
-
#merge(query) ⇒ URL
Append query parameters to the URL.
-
#merge!(query) ⇒ URL
Append query parameters to the URL.
-
#sld ⇒ String?
The second level domain (SLD).
-
#sld=(new_sld) ⇒ Object
Sets the second level domain (SLD).
-
#subdomain ⇒ String?
Returns the subdomain.
- #subdomain=(new_subdomain) ⇒ String
-
#tld ⇒ String?
Returns the top level domain (TLD).
-
#tld=(new_tld) ⇒ Object
Sets the top level domain (TLD).
-
#to_str ⇒ String
(also: #to_s)
Returns the full URL as a string.
Constructor Details
#initialize(host:, path: nil, protocol: nil, port: nil, query: nil) ⇒ URL
Returns a new instance of URL.
118 119 120 121 122 123 124 |
# File 'lib/url.rb', line 118 def initialize(host:, path: nil, protocol: nil, port: nil, query: nil) @protocol = protocol @host = host @port = port&.to_i self.path = path @query = Rack::Utils.parse_nested_query(query) end |
Instance Attribute Details
#host ⇒ String
33 34 35 |
# File 'lib/url.rb', line 33 def host @host end |
#path ⇒ Object
Returns the value of attribute path.
53 54 55 |
# File 'lib/url.rb', line 53 def path @path end |
#port ⇒ Object
37 38 39 |
# File 'lib/url.rb', line 37 def port @port end |
#protocol ⇒ String, HTTPS Also known as: scheme
Returns the protocol, defaults to “https”.
205 206 207 |
# File 'lib/url.rb', line 205 def protocol @protocol || HTTPS end |
#query ⇒ Hash
41 42 43 |
# File 'lib/url.rb', line 41 def query @query end |
Class Method Details
.parse(string) ⇒ URL Also known as: []
Initializes a new URL object from a given string
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/url.rb', line 62 def parse(string) return nil if string.nil? string .to_str .dup .then { extract_protocol(_1) } .then { extract_host_and_port(*_1) } .then { extract_path_and_query(**_1) } .then do next nil if _1.compact.empty? new(**_1) end end |
Instance Method Details
#domain ⇒ String
Returns the domain name
252 253 254 |
# File 'lib/url.rb', line 252 def domain [sld, tld].compact.join(DOT) end |
#inspect ⇒ Object
292 293 294 295 |
# File 'lib/url.rb', line 292 def inspect super.split(" ")[0].concat(" #{to_str}>") super.split(SPACE)[0].concat(" #{to_str}>") end |
#join(path) ⇒ URL #join(*paths) ⇒ URL
Adds a path to the URL
151 152 153 154 155 |
# File 'lib/url.rb', line 151 def join(*paths) dup = self.dup dup.join!(*paths) end |
#join(path) ⇒ URL #join(*paths) ⇒ URL
Adds a path to the URL
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/url.rb', line 163 def join!(*paths) parts = Array(path).concat(paths) size = parts.size parts .map .with_index(1) { |part, index| sanitize_path(part, last: index == size) } .compact .then do |parts| self.path = Array(NOTHING).concat(parts).join(SLASH) end self end |
#merge(query) ⇒ URL
Append query parameters to the URL
185 186 187 188 189 |
# File 'lib/url.rb', line 185 def merge(query) dup = self.dup dup.merge!(query) end |
#merge!(query) ⇒ URL
Append query parameters to the URL
198 199 200 201 202 |
# File 'lib/url.rb', line 198 def merge!(query) self.query = self.query.merge(deep_transform_keys(query)) self end |
#sld ⇒ String?
Returns the second level domain (SLD).
237 238 239 240 241 242 243 244 245 |
# File 'lib/url.rb', line 237 def sld domain_parts.then do |parts| if parts.size < 2 parts.last else parts[-2] end end end |
#sld=(new_sld) ⇒ Object
Sets the second level domain (SLD)
230 231 232 233 234 |
# File 'lib/url.rb', line 230 def sld=(new_sld) domain_parts.tap do |parts| @host = [subdomain, new_sld, tld].compact.join(DOT) end end |
#subdomain ⇒ String?
Returns the subdomain
269 270 271 272 273 274 275 |
# File 'lib/url.rb', line 269 def subdomain (domain_parts - [sld, tld].compact).then do |parts| next if parts.empty? parts.join(DOT) end end |
#subdomain=(new_subdomain) ⇒ String
259 260 261 262 263 264 265 |
# File 'lib/url.rb', line 259 def subdomain=(new_subdomain) domain_parts.tap do |parts| raise(Error, ERROR_MESSAGES[:subdomain=][:missing_tld] % {url: to_s}) if tld.nil? @host = [new_subdomain, sld, tld].join(DOT) end end |
#tld ⇒ String?
Returns the top level domain (TLD)
223 224 225 |
# File 'lib/url.rb', line 223 def tld domain_parts.last if domain_parts.size > 1 end |
#tld=(new_tld) ⇒ Object
Sets the top level domain (TLD)
213 214 215 216 217 218 219 |
# File 'lib/url.rb', line 213 def tld=(new_tld) domain_parts.tap do |parts| raise(Error, ERROR_MESSAGES[:tld=][:missing_tld] % {url: to_s}) if tld.nil? @host = [subdomain, sld, new_tld].compact.join(DOT) end end |
#to_str ⇒ String Also known as: to_s
Returns the full URL as a string
279 280 281 282 283 284 285 286 287 288 |
# File 'lib/url.rb', line 279 def to_str [ protocol, SEPARATOR, host, port_str, path_str, query_params ].compact.join end |