class Yawast::Shared::Uri

Public Class Methods

extract_uri(url) click to toggle source
# File lib/shared/uri.rb, line 8
def self.extract_uri(url)
  # this might be buggy - actually, I know it is...
  url = 'http://' + url unless url.include?('http://') || url.include?('https://')

  # make sure the path is at least a slash
  uri = URI.parse(url)
  uri.path = '/' if uri.path == ''

  # this is buggy, but we don't handle files anyhow...
  # if the path doesn't end in a slash, add one.
  uri.path.concat '/' if uri.path[-1, 1] != '/'

  # see if we can resolve the host
  # we don't really need it, it just serves as validation
  begin
    dns = Resolv::DNS.new
    dns.getaddress(uri.host)
  rescue => e # rubocop:disable Style/RescueStandardError
    if uri.host == 'localhost'
      # do nothing, in this case, we just don't care.
    elsif IPAddress.valid? uri.host
      # in this case the host name is actually a IP, let it go through.
    else
      # we've passed all the exceptions, if we are here, it's a problem
      raise ArgumentError, "Invalid URL (#{e.message})"
    end
  end

  uri
end