Skip to Content Skip to Search
Methods
F
P
Q
R
T
V

Constants

LOCALHOST_URL_REGEXP = /\A(http|https):\/\/localhost(:[0-9]{1,5})?(\/.*)?\z/ix
 
QUALIFIED_URL_REGEXP = /\A(http|https):\/\/[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,9}(:[0-9]{1,5})?(\/.*)?\z/ix
 
RELATIVE_URL_REGEXP = /^\/[a-z0-9\-._~%!$&'()*+,;=:@\/?]*$/i
 

Regex Breakdown ^/ = starts with /

a-z0-9-._~%!$&‘()*+,;=:@/?

only allow these chars (RFC 3986)

*$ = to the end of the line /i = case insensitive

Class Public methods

fully_qualified_url(location, site)

# File app/utils/util/url.rb, line 42
def fully_qualified_url(location, site)
  qualified_url?(location) ? location : to_site_url(location, site)
end

path_matches_pathname(path, pathname)

# File app/utils/util/url.rb, line 15
def path_matches_pathname(path, pathname)
  pathname.chop! if pathname&.ends_with?("/")
  path.chop! if path.ends_with?("/")
  if path.ends_with?("*")
    Regexp.new(/\A#{Regexp.escape(path.chop)}\S+/).match("#{pathname}/").present?
  else
    path == pathname
  end
end

qualified_url?(url)

# File app/utils/util/url.rb, line 50
def qualified_url?(url)
  url&.match?(QUALIFIED_URL_REGEXP)
end

relative_url?(url)

# File app/utils/util/url.rb, line 54
def relative_url?(url)
  url.match?(RELATIVE_URL_REGEXP)
end

to_site_url(location, site)

# File app/utils/util/url.rb, line 46
def to_site_url(location, site)
  site.website + location
end

to_url(str)

# File app/utils/util/url.rb, line 25
def to_url(str)
  uri = URI(str&.squish)
  uri.scheme = "https" if schemeless_uri?(uri)

  if generic_uri_with_path?(uri)
    root, *paths = uri.path.split("/")
    uri.host = root
    uri.path = "/#{paths&.join("/")}"
  end

  add_trailing_slash(uri)

  uri.to_s
rescue URI::InvalidURIError
  url
end

valid_url?(url)

# File app/utils/util/url.rb, line 58
def valid_url?(url)
  relative_url?(url) || localhost?(url) || qualified_url?(url)
end