class Fuzzyurl::Strings

Constants

REGEX

Public Class Methods

from_string(str, opts={}) click to toggle source
# File lib/fuzzyurl/strings.rb, line 21
def from_string(str, opts={})
  return nil unless str.kind_of?(String)

  default = opts[:default]
  if m = REGEX.match(str)
    fu = Fuzzyurl.new
    Fuzzyurl::FIELDS.each do |f|
      fu.send("#{f}=", m[f] || default)
    end
    fu
  else
    raise ArgumentError, "Couldn't parse url string: #{str}"
  end
end
to_string(fuzzyurl) click to toggle source
# File lib/fuzzyurl/strings.rb, line 36
def to_string(fuzzyurl)
  if !fuzzyurl.kind_of?(Fuzzyurl)
    raise ArgumentError, "`fuzzyurl` must be a Fuzzyurl"
  end

  fu = fuzzyurl
  str = ""
  str << "#{fu.protocol}://" if fu.protocol
  str << "#{fu.username}" if fu.username
  str << ":#{fu.password}" if fu.password
  str << "@" if fu.username
  str << "#{fu.hostname}" if fu.hostname
  str << ":#{fu.port}" if fu.port
  str << "#{fu.path}" if fu.path
  str << "?#{fu.query}" if fu.query
  str << "##{fu.fragment}" if fu.fragment
  str
end