class Elsmore::Url

Attributes

parent[RW]
raw_url[RW]
uri[RW]
valid[RW]

Public Class Methods

new(raw_url, parent) click to toggle source
# File lib/elsmore/url.rb, line 5
def initialize raw_url, parent
  self.raw_url = raw_url.strip
  self.parent = parent
  self.valid = true

  sanitize_string
  parse_uri
end

Public Instance Methods

absolute_path_or_external_url() click to toggle source
# File lib/elsmore/url.rb, line 31
def absolute_path_or_external_url
  if parent && parent.host == host
    uri.path
  else
    canonical_url
  end
end
canonical_url() click to toggle source
# File lib/elsmore/url.rb, line 27
def canonical_url
  uri.to_s
end
host() click to toggle source
# File lib/elsmore/url.rb, line 14
def host
  uri.host
end
parent_host() click to toggle source
# File lib/elsmore/url.rb, line 18
def parent_host
  return unless parent
  parent.host
end
resource_path() click to toggle source
# File lib/elsmore/url.rb, line 39
def resource_path
  if parent && parent.host == host
    uri.path
  else
    canonical_url.gsub('http:/', '').gsub('https:/', '')
  end
end
scheme() click to toggle source
# File lib/elsmore/url.rb, line 23
def scheme
  uri.scheme
end

Private Instance Methods

parse_uri() click to toggle source
# File lib/elsmore/url.rb, line 63
def parse_uri
  self.uri = URI.parse(raw_url)
  uri.path = "/" if uri.path.empty?
  uri.fragment = nil
rescue
  self.valid = false
end
sanitize_string() click to toggle source
# File lib/elsmore/url.rb, line 49
def sanitize_string
  if raw_url.start_with?('//')
    self.raw_url = "http:#{raw_url}"
  elsif raw_url.start_with?('/')
    self.raw_url = "#{parent.scheme}://#{parent.host}#{raw_url}"
  elsif !(raw_url.start_with?('http://') || raw_url.start_with?('https://'))
    if parent
      self.raw_url = URI.join(parent.canonical_url, raw_url).to_s
    else
      self.raw_url = "http://#{raw_url}"
    end
  end
end