Method: Addressable::URI.parse
- Defined in:
- lib/addressable/uri.rb
.parse(uri) ⇒ Addressable::URI
Returns a URI object based on the parsed string.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/addressable/uri.rb', line 114 def self.parse(uri) # 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 a URI object of the Ruby standard library variety is passed, # convert it to a string, then parse the string. # We do the check this way because we don't want to accidentally # cause a missing constant exception to be thrown. if uri.class.name =~ /^URI\b/ uri = uri.to_s end # Otherwise, convert to a String begin uri = uri.to_str rescue TypeError, NoMethodError raise TypeError, "Can't convert #{uri.class} into String." end unless uri.is_a?(String) # This Regexp supplied as an example in RFC 3986, and it works great. scan = uri.scan(URIREGEX) fragments = scan[0] scheme = fragments[1] = fragments[3] path = fragments[4] query = fragments[6] fragment = fragments[8] user = nil password = nil host = nil port = nil if != nil # The Regexp above doesn't split apart the authority. userinfo = [/^([^\[\]]*)@/, 1] if userinfo != nil user = userinfo.strip[/^([^:]*):?/, 1] password = userinfo.strip[/:(.*)$/, 1] end host = .sub( /^([^\[\]]*)@/, EMPTY_STR ).sub( /:([^:@\[\]]*?)$/, EMPTY_STR ) port = [/:([^:@\[\]]*?)$/, 1] port = nil if port == EMPTY_STR end return new( :scheme => scheme, :user => user, :password => password, :host => host, :port => port, :path => path, :query => query, :fragment => fragment ) end |