class Addressable::URI

def self.heuristic_parse(uri, hints={})

Returns:
  • (Addressable::URI) - The parsed URI.

Parameters:
  • hints (Hash) --
  • uri (String, Addressable::URI, #to_str) --
def self.heuristic_parse(uri, hints={})
  # If we were given nil, return nil.
  return nil unless uri
  # If a URI object is passed, just return itself.
  return uri.dup if uri.kind_of?(self)
  if !uri.respond_to?(:to_str)
    raise TypeError, "Can't convert #{uri.class} into String."
  end
  # Otherwise, convert to a String
  uri = uri.to_str.dup
  hints = {
    :scheme => "http"
  }.merge(hints)
  case uri
  when /^http:\/+/
    uri.gsub!(/^http:\/+/, "http://")
  when /^https:\/+/
    uri.gsub!(/^https:\/+/, "https://")
  when /^feed:\/+http:\/+/
    uri.gsub!(/^feed:\/+http:\/+/, "feed:http://")
  when /^feed:\/+/
    uri.gsub!(/^feed:\/+/, "feed://")
  when /^file:\/+/
    uri.gsub!(/^file:\/+/, "file:///")
  end
  parsed = self.parse(uri)
  if parsed.scheme =~ /^[^\/?#\.]+\.[^\/?#]+$/
    parsed = self.parse(hints[:scheme] + "://" + uri)
  end
  if parsed.path.include?(".")
    new_host = parsed.path[/^([^\/]+\.[^\/]*)/, 1]
    if new_host
      parsed.defer_validation do
        new_path = parsed.path.gsub(
          Regexp.new("^" + Regexp.escape(new_host)), EMPTYSTR)
        parsed.host = new_host
        parsed.path = new_path
        parsed.scheme = hints[:scheme] unless parsed.scheme
      end
    end
  end
  return parsed
end