Module: WebStruct::Http::Url

Defined in:
lib/webstruct/http/url.rb

Overview

Validates and normalizes URLs for get.

Class Method Summary collapse

Class Method Details

.verify!(url) ⇒ String

Validates the input and returns a normalized absolute HTTP(S) URL string.

Parameters:

  • url (#to_s)

    user-provided URL string or URI-like value

Returns:

  • (String)

Raises:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/webstruct/http/url.rb', line 17

def verify!(url)
  uri = URI.parse(url.strip)
  raise(InvalidUrlError, "URL must be absolute") unless uri.absolute?

  scheme = uri.scheme&.downcase
  raise(InvalidUrlError, "URL scheme must be http or https") unless %w[http https].include?(scheme)

  uri.to_s
rescue URI::InvalidURIError => error
  raise(InvalidUrlError, error.message)
end