def self.parse(uri)
return nil unless uri
return uri.dup if uri.kind_of?(self)
if uri.class.name =~ /^URI\b/
uri = uri.to_s
end
begin
uri = uri.to_str
rescue TypeError, NoMethodError
raise TypeError, "Can't convert #{uri.class} into String."
end if not uri.is_a? String
scan = uri.scan(URIREGEX)
fragments = scan[0]
scheme = fragments[1]
authority = fragments[3]
path = fragments[4]
query = fragments[6]
fragment = fragments[8]
user = nil
password = nil
host = nil
port = nil
if authority != nil
userinfo = authority[/^([^\[\]]*)@/, 1]
if userinfo != nil
user = userinfo.strip[/^([^:]*):?/, 1]
password = userinfo.strip[/:(.*)$/, 1]
end
host = authority.gsub(
/^([^\[\]]*)@/, EMPTY_STR
).gsub(
/:([^:@\[\]]*?)$/, EMPTY_STR
)
port = authority[/:([^:@\[\]]*?)$/, 1]
end
if port == EMPTY_STR
port = nil
end
return new(
:scheme => scheme,
:user => user,
:password => password,
:host => host,
:port => port,
:path => path,
:query => query,
:fragment => fragment
)
end