Module: Nucleus::UrlConverter

Included in:
AdapterResolver
Defined in:
lib/nucleus/core/common/url_converter.rb

Instance Method Summary collapse

Instance Method Details

#secure_url(url_to_secure) ⇒ String

Convert the URL to the secure ‘HTTPS’ scheme. Passed URLs must be in one of the following forms:

{scheme}://{prefix.}host.domain
{prefix.}host.domain

An url that would raise an ArgumentError is

/path/to/somewhere

Raises:

  • ArgumentError if url is not absolute, starts with a ‘/’



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nucleus/core/common/url_converter.rb', line 13

def secure_url(url_to_secure)
  # return if URL already is secure
  return url_to_secure if url_to_secure =~ /\A#{URI.regexp(['https'])}\z/
  throw ArgumentError, "Invalid URL '#{url_to_secure}', can't secure relative URL" if url_to_secure.start_with?('/')
  uri = URI.parse(url_to_secure)
  if uri.scheme.nil?
    uri = "https://#{url_to_secure}"
    secured_url = uri.to_s
  elsif uri.scheme != 'https'
    uri.scheme = 'https'
    secured_url = uri.to_s
  end
  secured_url
end