Module: Philiprehberger::UriKit
- Defined in:
- lib/philiprehberger/uri_kit.rb,
lib/philiprehberger/uri_kit/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
'0.5.0'
Class Method Summary collapse
-
.build(host:, path: '/', params: {}, scheme: 'https') ⇒ Url
Build a URL from components.
-
.join(base, relative) ⇒ Url
Join a base URL with a relative path.
-
.parse(url) ⇒ Url
Parse a URL string.
Class Method Details
.build(host:, path: '/', params: {}, scheme: 'https') ⇒ Url
Build a URL from components
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/philiprehberger/uri_kit.rb', line 289 def self.build(host:, path: '/', params: {}, scheme: 'https') query = if params.empty? nil else params.map do |k, v| "#{::URI.encode_www_form_component(k)}=#{::URI.encode_www_form_component(v)}" end.join('&') end uri = ::URI::Generic.build( scheme: scheme, host: host, path: path.start_with?('/') ? path : "/#{path}", query: query ) Url.new(uri.to_s) end |
.join(base, relative) ⇒ Url
Join a base URL with a relative path
313 314 315 316 317 318 319 |
# File 'lib/philiprehberger/uri_kit.rb', line 313 def self.join(base, relative) base_uri = ::URI.parse(base.to_s) joined = base_uri.merge(relative.to_s) Url.new(joined.to_s) rescue ::URI::InvalidURIError => e raise Error, "Invalid URL: #{e.}" end |