Class: Addressable::URI
- Inherits:
-
Object
- Object
- Addressable::URI
- Defined in:
- lib/addressable/uri.rb
Overview
This is an implementation of a URI parser based on <a href=“www.ietf.org/rfc/rfc3986.txt”>RFC 3986</a>, <a href=“www.ietf.org/rfc/rfc3987.txt”>RFC 3987</a>.
Defined Under Namespace
Modules: CharacterClasses Classes: InvalidOptionError, InvalidTemplateOperatorError, InvalidTemplateValueError, InvalidURIError, TemplateOperatorAbortedError
Class Method Summary collapse
-
.convert_path(path) ⇒ Addressable::URI
Converts a path to a file scheme URI.
-
.encode(uri, returning = String) ⇒ String, Addressable::URI
(also: escape)
Percent encodes any special characters in the URI.
-
.expand_join_operator(argument, variables, mapping) ⇒ String
Expands a URI Template join operator.
-
.expand_list_operator(argument, variables, mapping) ⇒ String
Expands a URI Template list operator.
-
.expand_neg_operator(argument, variables, mapping) ⇒ String
Expands a URI Template neg operator.
-
.expand_opt_operator(argument, variables, mapping) ⇒ String
Expands a URI Template opt operator.
-
.expand_prefix_operator(argument, variables, mapping) ⇒ String
Expands a URI Template prefix operator.
-
.expand_suffix_operator(argument, variables, mapping) ⇒ String
Expands a URI Template suffix operator.
-
.expand_template(pattern, mapping, processor = nil) ⇒ Addressable::URI
Expands a URI template into a full URI.
-
.extract(text, options = {}) ⇒ Array
Extracts uris from an arbitrary body of text.
-
.heuristic_parse(uri, hints = {}) ⇒ Addressable::URI
Converts an input to a URI.
-
.ip_based_schemes ⇒ Object
Returns an array of known ip-based schemes.
-
.join(*uris) ⇒ Addressable::URI
Joins several URIs together.
-
.normalized_encode(uri, returning = String) ⇒ String, Addressable::URI
Normalizes the encoding of a URI.
-
.parse(uri) ⇒ Addressable::URI
Returns a URI object based on the parsed string.
-
.parse_template_expansion(capture, mapping) ⇒ Array
Parses a URI template expansion
String
. -
.port_mapping ⇒ Object
Returns a hash of common IP-based schemes and their default port numbers.
-
.unencode(uri, returning = String) ⇒ String, Addressable::URI
(also: unescape, unencode_component, unescape_component)
Unencodes any percent encoded characters within a URI component.
Instance Method Summary collapse
-
#==(uri) ⇒ TrueClass, FalseClass
Returns
true
if the URI objects are equal. -
#===(uri) ⇒ TrueClass, FalseClass
Returns
true
if the URI objects are equal. -
#absolute? ⇒ TrueClass, FalseClass
Determines if the URI is absolute.
-
#authority ⇒ String
The authority component for this URI.
-
#authority=(new_authority) ⇒ Object
Sets the authority component for this URI.
-
#basename ⇒ String
The basename, if any, of the file in the path component.
-
#display_uri ⇒ Addressable::URI
Creates a URI suitable for display to users.
-
#dup ⇒ Addressable::URI
Clones the URI object.
-
#eql?(uri) ⇒ TrueClass, FalseClass
Returns
true
if the URI objects are equal. -
#extname ⇒ String
The extname, if any, of the file in the path component.
-
#extract_mapping(pattern, processor = nil) ⇒ Hash, NilClass
Extracts a mapping from the URI using a URI Template pattern.
-
#fragment ⇒ String
The fragment component for this URI.
-
#fragment=(new_fragment) ⇒ Object
Sets the fragment component for this URI.
-
#hash ⇒ Integer
A hash value that will make a URI equivalent to its normalized form.
-
#host ⇒ String
The host component for this URI.
-
#host=(new_host) ⇒ Object
Sets the host component for this URI.
-
#inferred_port ⇒ Integer
The inferred port component for this URI.
-
#initialize(options = {}) ⇒ Addressable::URI
constructor
Creates a new uri object from component parts.
-
#inspect ⇒ String
Returns a
String
representation of the URI object’s state. -
#ip_based? ⇒ TrueClass, FalseClass
Determines if the scheme indicates an IP-based protocol.
-
#join(uri) ⇒ Addressable::URI
(also: #+)
Joins two URIs together.
-
#join!(uri) ⇒ Addressable::URI
Destructive form of
join
. -
#merge(hash) ⇒ Addressable::URI
Merges a URI with a
Hash
of components. -
#merge!(uri) ⇒ Addressable::URI
Destructive form of
merge
. -
#normalize ⇒ Addressable::URI
Returns a normalized URI object.
-
#normalize! ⇒ Addressable::URI
Destructively normalizes this URI object.
-
#normalized_authority ⇒ String
The authority component for this URI, normalized.
-
#normalized_fragment ⇒ String
The fragment component for this URI, normalized.
-
#normalized_host ⇒ String
The host component for this URI, normalized.
-
#normalized_password ⇒ String
The password component for this URI, normalized.
-
#normalized_path ⇒ String
The path component for this URI, normalized.
-
#normalized_port ⇒ Integer
The port component for this URI, normalized.
-
#normalized_query ⇒ String
The query component for this URI, normalized.
-
#normalized_scheme ⇒ String
The scheme component for this URI, normalized.
-
#normalized_user ⇒ String
The user component for this URI, normalized.
-
#normalized_userinfo ⇒ String
The userinfo component for this URI, normalized.
-
#omit(*components) ⇒ Addressable::URI
Omits components from a URI.
-
#omit!(*components) ⇒ Addressable::URI
Destructive form of omit.
-
#password ⇒ String
The password component for this URI.
-
#password=(new_password) ⇒ Object
Sets the password component for this URI.
-
#path ⇒ String
The path component for this URI.
-
#path=(new_path) ⇒ Object
Sets the path component for this URI.
-
#port ⇒ Integer
The port component for this URI.
-
#port=(new_port) ⇒ Object
Sets the port component for this URI.
-
#query ⇒ String
The query component for this URI.
-
#query=(new_query) ⇒ Object
Sets the query component for this URI.
-
#query_values(options = {}) ⇒ Hash
Converts the query component to a Hash value.
-
#query_values=(new_query_values) ⇒ Object
Sets the query component for this URI from a Hash object.
-
#relative? ⇒ TrueClass, FalseClass
Determines if the URI is relative.
-
#route_from(uri) ⇒ Addressable::URI
Returns the shortest normalized relative form of this URI that uses the supplied URI as a base for resolution.
-
#route_to(uri) ⇒ Addressable::URI
Returns the shortest normalized relative form of the supplied URI that uses this URI as a base for resolution.
-
#scheme ⇒ String
The scheme component for this URI.
-
#scheme=(new_scheme) ⇒ Object
Sets the scheme component for this URI.
-
#to_hash ⇒ Hash
Returns a Hash of the URI components.
-
#to_s ⇒ String
(also: #to_str)
Converts the URI to a
String
. -
#user ⇒ String
The user component for this URI.
-
#user=(new_user) ⇒ Object
Sets the user component for this URI.
-
#userinfo ⇒ String
The userinfo component for this URI.
-
#userinfo=(new_userinfo) ⇒ Object
Sets the userinfo component for this URI.
-
#validation_deferred ⇒ TrueClass, FalseClass
If URI validation needs to be disabled, this can be set to true.
-
#validation_deferred=(new_validation_deferred) ⇒ Object
If URI validation needs to be disabled, this can be set to true.
Constructor Details
#initialize(options = {}) ⇒ Addressable::URI
Creates a new uri object from component parts.
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 |
# File 'lib/addressable/uri.rb', line 1201 def initialize(={}) if .has_key?(:authority) if (.keys & [:userinfo, :user, :password, :host, :port]).any? raise ArgumentError, "Cannot specify both an authority and any of the components " + "within the authority." end end if .has_key?(:userinfo) if (.keys & [:user, :password]).any? raise ArgumentError, "Cannot specify both a userinfo and either the user or password." end end self.validation_deferred = true self.scheme = [:scheme] if [:scheme] self.user = [:user] if [:user] self.password = [:password] if [:password] self.userinfo = [:userinfo] if [:userinfo] self.host = [:host] if [:host] self.port = [:port] if [:port] self. = [:authority] if [:authority] self.path = [:path] if [:path] self.query = [:query] if [:query] self.fragment = [:fragment] if [:fragment] self.validation_deferred = false end |
Class Method Details
.convert_path(path) ⇒ Addressable::URI
Converts a path to a file scheme URI. If the path supplied is relative, it will be returned as a relative URI. If the path supplied is actually a non-file URI, it will parse the URI as if it had been parsed with Addressable::URI.parse
. Handles all of the various Microsoft-specific formats for specifying paths.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/addressable/uri.rb', line 233 def self.convert_path(path) # If we were given nil, return nil. return nil unless path # If a URI object is passed, just return itself. return path if path.kind_of?(self) if !path.respond_to?(:to_str) raise TypeError, "Can't convert #{path.class} into String." end # Otherwise, convert to a String path = path.to_str.strip path.gsub!(/^file:\/?\/?/, "") if path =~ /^file:\/?\/?/ path = "/" + path if path =~ /^([a-zA-Z])(\||:)/ uri = self.parse(path) if uri.scheme == nil # Adjust windows-style uris uri.path.gsub!(/^\/?([a-zA-Z])\|(\\|\/)/, "/\\1:/") uri.path.gsub!(/\\/, "/") if File.exists?(uri.path) && File.stat(uri.path).directory? uri.path.gsub!(/\/$/, "") uri.path = uri.path + '/' end # If the path is absolute, set the scheme and host. if uri.path =~ /^\// uri.scheme = "file" uri.host = "" end uri.normalize! end return uri end |
.encode(uri, returning = String) ⇒ String, Addressable::URI Also known as: escape
Percent encodes any special characters in the URI.
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 |
# File 'lib/addressable/uri.rb', line 1026 def self.encode(uri, returning=String) return nil if uri.nil? if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if ![String, ::Addressable::URI].include?(returning) raise TypeError, "Expected String or Addressable::URI, got #{returning.inspect}" end uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_str) encoded_uri = Addressable::URI.new( :scheme => self.encode_component(uri_object.scheme, Addressable::URI::CharacterClasses::SCHEME), :authority => self.encode_component(uri_object., Addressable::URI::CharacterClasses::AUTHORITY), :path => self.encode_component(uri_object.path, Addressable::URI::CharacterClasses::PATH), :query => self.encode_component(uri_object.query, Addressable::URI::CharacterClasses::QUERY), :fragment => self.encode_component(uri_object.fragment, Addressable::URI::CharacterClasses::FRAGMENT) ) if returning == String return encoded_uri.to_s elsif returning == ::Addressable::URI return encoded_uri end end |
.expand_join_operator(argument, variables, mapping) ⇒ String
Expands a URI Template join operator.
487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/addressable/uri.rb', line 487 def self.(argument, variables, mapping) variable_values = variables.inject([]) do |accu, variable| if !mapping[variable].kind_of?(Array) if mapping[variable] accu << variable + "=" + (mapping[variable]) end else raise InvalidTemplateOperatorError, "Template operator 'join' does not accept Array values." end accu end variable_values.join(argument) end |
.expand_list_operator(argument, variables, mapping) ⇒ String
Expands a URI Template list operator.
511 512 513 514 515 516 517 |
# File 'lib/addressable/uri.rb', line 511 def self.(argument, variables, mapping) if variables.size != 1 raise InvalidTemplateOperatorError, "Template operator 'list' takes exactly one variable." end mapping[variables.first].join(argument) end |
.expand_neg_operator(argument, variables, mapping) ⇒ String
Expands a URI Template neg operator.
423 424 425 426 427 428 429 430 431 432 |
# File 'lib/addressable/uri.rb', line 423 def self.(argument, variables, mapping) if (variables.any? do |variable| mapping[variable] != [] && mapping[variable] end) "" else argument end end |
.expand_opt_operator(argument, variables, mapping) ⇒ String
Expands a URI Template opt operator.
403 404 405 406 407 408 409 410 411 412 |
# File 'lib/addressable/uri.rb', line 403 def self.(argument, variables, mapping) if (variables.any? do |variable| mapping[variable] != [] && mapping[variable] end) argument else "" end end |
.expand_prefix_operator(argument, variables, mapping) ⇒ String
Expands a URI Template prefix operator.
443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/addressable/uri.rb', line 443 def self.(argument, variables, mapping) if variables.size != 1 raise InvalidTemplateOperatorError, "Template operator 'prefix' takes exactly one variable." end value = mapping[variables.first] if value.kind_of?(Array) (value.map { |list_value| argument + list_value }).join("") else argument + value.to_s end end |
.expand_suffix_operator(argument, variables, mapping) ⇒ String
Expands a URI Template suffix operator.
465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/addressable/uri.rb', line 465 def self.(argument, variables, mapping) if variables.size != 1 raise InvalidTemplateOperatorError, "Template operator 'suffix' takes exactly one variable." end value = mapping[variables.first] if value.kind_of?(Array) (value.map { |list_value| list_value + argument }).join("") else value.to_s + argument end end |
.expand_template(pattern, mapping, processor = nil) ⇒ Addressable::URI
Expands a URI template into a full URI.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/addressable/uri.rb', line 320 def self.(pattern, mapping, processor=nil) # FIXME: MUST REFACTOR!!! result = pattern.dup reserved = Addressable::URI::CharacterClasses::RESERVED unreserved = Addressable::URI::CharacterClasses::UNRESERVED anything = reserved + unreserved operator_expansion = /\{-([a-zA-Z]+)\|([#{anything}]+)\|([#{anything}]+)\}/ variable_expansion = /\{([#{anything}]+?)(=([#{anything}]+))?\}/ transformed_mapping = mapping.inject({}) do |accu, pair| name, value = pair unless value.respond_to?(:to_ary) || value.respond_to?(:to_str) raise TypeError, "Can't convert #{value.class} into String or Array." end transformed_value = value.respond_to?(:to_ary) ? value.to_ary : value.to_str # Handle percent escaping, and unicode normalization if transformed_value.kind_of?(Array) transformed_value.map! do |value| self.encode_component( Addressable::IDNA.unicode_normalize_kc(value), Addressable::URI::CharacterClasses::UNRESERVED ) end else transformed_value = self.encode_component( Addressable::IDNA.unicode_normalize_kc(transformed_value), Addressable::URI::CharacterClasses::UNRESERVED ) end # Process, if we've got a processor if processor != nil if processor.respond_to?(:validate) if !processor.validate(name, value) display_value = value.kind_of?(Array) ? value.inspect : value raise InvalidTemplateValueError, "#{name}=#{display_value} is an invalid template value." end end if processor.respond_to?(:transform) transformed_value = processor.transform(name, value) end end accu[name] = transformed_value accu end result.gsub!( /#{operator_expansion}|#{variable_expansion}/ ) do |capture| if capture =~ operator_expansion operator, argument, variables, default_mapping = parse_template_expansion(capture, transformed_mapping) = "expand_#{operator}_operator" if ([, .to_sym] & private_methods).empty? raise InvalidTemplateOperatorError, "Invalid template operator: #{operator}" else send(.to_sym, argument, variables, default_mapping) end else varname, _, vardefault = capture.scan(/^\{(.+?)(=(.*))?\}$/)[0] transformed_mapping[varname] || vardefault end end return Addressable::URI.parse(result) end |
.extract(text, options = {}) ⇒ Array
Extracts uris from an arbitrary body of text.
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 |
# File 'lib/addressable/uri.rb', line 1136 def self.extract(text, ={}) defaults = {:base => nil, :parse => false} = defaults.merge() raise InvalidOptionError unless (.keys - defaults.keys).empty? # This regular expression needs to be less forgiving or else it would # match virtually all text. Which isn't exactly what we're going for. extract_regex = /((([a-z\+]+):)[^ \n\<\>\"\\]+[\w\/])/ extracted_uris = text.scan(extract_regex).collect { |match| match[0] } sgml_extract_regex = /<[^>]+href=\"([^\"]+?)\"[^>]*>/ sgml_extracted_uris = text.scan(sgml_extract_regex).collect { |match| match[0] } extracted_uris.concat(sgml_extracted_uris - extracted_uris) textile_extract_regex = /\".+?\":([^ ]+\/[^ ]+)[ \,\.\;\:\?\!\<\>\"]/i textile_extracted_uris = text.scan(textile_extract_regex).collect { |match| match[0] } extracted_uris.concat(textile_extracted_uris - extracted_uris) parsed_uris = [] base_uri = nil if [:base] != nil base_uri = [:base] if [:base].kind_of?(self) base_uri = self.parse([:base].to_s) if base_uri == nil end for uri_string in extracted_uris begin if base_uri == nil parsed_uris << self.parse(uri_string) else parsed_uris << (base_uri + self.parse(uri_string)) end rescue Exception nil end end parsed_uris = parsed_uris.select do |uri| (self.ip_based_schemes | [ "file", "git", "svn", "mailto", "tel" ]).include?(uri.normalized_scheme) end if [:parse] return parsed_uris else return parsed_uris.collect { |uri| uri.to_s } end end |
.heuristic_parse(uri, hints = {}) ⇒ Addressable::URI
Converts an input to a URI. The input does not have to be a valid URI — the method will use heuristics to guess what URI was intended. This is not standards-compliant, merely user-friendly.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/addressable/uri.rb', line 162 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 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 /^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. == nil if parsed.path =~ /^[^\/]+\./ new_host = parsed.path[/^([^\/]+\.[^\/]*)/, 1] if new_host new_path = parsed.path.gsub( Regexp.new("^" + Regexp.escape(new_host)), "") parsed.host = new_host parsed.path = new_path parsed.scheme = hints[:scheme] end end end return parsed end |
.ip_based_schemes ⇒ Object
Returns an array of known ip-based schemes. These schemes typically use a similar URI form: //<user>:<password>@<host>:<port>/<url-path>
1557 1558 1559 |
# File 'lib/addressable/uri.rb', line 1557 def self.ip_based_schemes return self.port_mapping.keys end |
.join(*uris) ⇒ Addressable::URI
Joins several URIs together.
903 904 905 906 907 908 909 910 911 912 913 914 915 |
# File 'lib/addressable/uri.rb', line 903 def self.join(*uris) uri_objects = uris.collect do |uri| if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end uri.kind_of?(self) ? uri : self.parse(uri.to_str) end result = uri_objects.shift.dup for uri in uri_objects result.join!(uri) end return result end |
.normalized_encode(uri, returning = String) ⇒ String, Addressable::URI
Normalizes the encoding of a URI. Characters within a hostname are not percent encoded to allow for internationalized domain names.
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
# File 'lib/addressable/uri.rb', line 1074 def self.normalized_encode(uri, returning=String) if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if ![String, ::Addressable::URI].include?(returning) raise TypeError, "Expected String or Addressable::URI, got #{returning.inspect}" end uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_str) components = { :scheme => self.unencode_component(uri_object.scheme), :user => self.unencode_component(uri_object.user), :password => self.unencode_component(uri_object.password), :host => self.unencode_component(uri_object.host), :port => uri_object.port, :path => self.unencode_component(uri_object.path), :query => self.unencode_component(uri_object.query), :fragment => self.unencode_component(uri_object.fragment) } components.each do |key, value| if value != nil components[key] = Addressable::IDNA.unicode_normalize_kc(value.to_s) end end encoded_uri = Addressable::URI.new( :scheme => self.encode_component(components[:scheme], Addressable::URI::CharacterClasses::SCHEME), :user => self.encode_component(components[:user], Addressable::URI::CharacterClasses::AUTHORITY), :password => self.encode_component(components[:password], Addressable::URI::CharacterClasses::AUTHORITY), :host => components[:host], :port => components[:port], :path => self.encode_component(components[:path], Addressable::URI::CharacterClasses::PATH), :query => self.encode_component(components[:query], Addressable::URI::CharacterClasses::QUERY), :fragment => self.encode_component(components[:fragment], Addressable::URI::CharacterClasses::FRAGMENT) ) if returning == String return encoded_uri.to_s elsif returning == ::Addressable::URI return encoded_uri end end |
.parse(uri) ⇒ Addressable::URI
Returns a URI object based on the parsed string.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 |
# File 'lib/addressable/uri.rb', line 88 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 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 # 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 # This Regexp supplied as an example in RFC 3986, and it works great. uri_regex = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/ scan = uri.scan(uri_regex) fragments = scan[0] return nil if fragments.nil? scheme = fragments[1] = fragments[3] path = fragments[4] query = fragments[6] fragment = fragments[8] userinfo = nil 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 = .gsub(/^([^\[\]]*)@/, "").gsub(/:([^:@\[\]]*?)$/, "") port = [/:([^:@\[\]]*?)$/, 1] end if port == "" port = nil end return Addressable::URI.new( :scheme => scheme, :user => user, :password => password, :host => host, :port => port, :path => path, :query => query, :fragment => fragment ) end |
.parse_template_expansion(capture, mapping) ⇒ Array
Parses a URI template expansion String
.
528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/addressable/uri.rb', line 528 def self.parse_template_expansion(capture, mapping) operator, argument, variables = capture[1...-1].split("|") operator.gsub!(/^\-/, "") variables = variables.split(",") mapping = (variables.inject({}) do |accu, var| varname, _, vardefault = var.scan(/^(.+?)(=(.*))?$/)[0] accu[varname] = vardefault accu end).merge(mapping) variables = variables.map { |var| var.gsub(/=.*$/, "") } return operator, argument, variables, mapping end |
.port_mapping ⇒ Object
Returns a hash of common IP-based schemes and their default port numbers. Adding new schemes to this hash, as necessary, will allow for better URI normalization.
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 |
# File 'lib/addressable/uri.rb', line 1564 def self.port_mapping @port_mapping ||= { "http" => 80, "https" => 443, "ftp" => 21, "tftp" => 69, "sftp" => 22, "ssh" => 22, "svn+ssh" => 22, "telnet" => 23, "nntp" => 119, "gopher" => 70, "wais" => 210, "ldap" => 389, "prospero" => 1525 } end |
.unencode(uri, returning = String) ⇒ String, Addressable::URI Also known as: unescape, unencode_component, unescape_component
Unencodes any percent encoded characters within a URI component. This method may be used for unencoding either components or full URIs, however, it is recommended to use the unencode_component
alias when unencoding components.
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 |
# File 'lib/addressable/uri.rb', line 986 def self.unencode(uri, returning=String) return nil if uri.nil? if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if ![String, ::Addressable::URI].include?(returning) raise TypeError, "Expected String or Addressable::URI, got #{returning.inspect}" end result = uri.to_str.gsub(/%[0-9a-f]{2}/i) do |sequence| sequence[1..3].to_i(16).chr end result.force_encoding("utf-8") if result.respond_to?(:force_encoding) if returning == String return result elsif returning == ::Addressable::URI return ::Addressable::URI.parse(result) end end |
Instance Method Details
#==(uri) ⇒ TrueClass, FalseClass
Returns true
if the URI objects are equal. This method normalizes both URIs before doing the comparison.
2240 2241 2242 2243 |
# File 'lib/addressable/uri.rb', line 2240 def ==(uri) return false unless uri.kind_of?(self.class) return self.normalize.to_s == uri.normalize.to_s end |
#===(uri) ⇒ TrueClass, FalseClass
Returns true
if the URI objects are equal. This method normalizes both URIs before doing the comparison, and allows comparison against Strings
.
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 |
# File 'lib/addressable/uri.rb', line 2219 def ===(uri) if uri.respond_to?(:normalize) uri_string = uri.normalize.to_s else begin uri_string = ::Addressable::URI.parse(uri).normalize.to_s rescue InvalidURIError, TypeError return false end end return self.normalize.to_s == uri_string end |
#absolute? ⇒ TrueClass, FalseClass
Determines if the URI is absolute.
1887 1888 1889 |
# File 'lib/addressable/uri.rb', line 1887 def absolute? return !relative? end |
#authority ⇒ String
The authority component for this URI. Combines the user, password, host, and port components.
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 |
# File 'lib/addressable/uri.rb', line 1481 def @authority ||= (begin if self.host.nil? nil else = "" if self.userinfo != nil << "#{self.userinfo}@" end << self.host if self.port != nil << ":#{self.port}" end end end) end |
#authority=(new_authority) ⇒ Object
Sets the authority component for this URI.
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 |
# File 'lib/addressable/uri.rb', line 1525 def () if = .to_str new_userinfo = [/^([^\[\]]*)@/, 1] if new_userinfo new_user = new_userinfo.strip[/^([^:]*):?/, 1] new_password = new_userinfo.strip[/:(.*)$/, 1] end new_host = .gsub(/^([^\[\]]*)@/, "").gsub(/:([^:@\[\]]*?)$/, "") new_port = [/:([^:@\[\]]*?)$/, 1] end # Password assigned first to ensure validity in case of nil self.password = new_password self.user = new_user self.host = new_host self.port = new_port # Reset dependant values @inferred_port = nil @userinfo = nil @normalized_userinfo = nil # Ensure we haven't created an invalid URI validate() end |
#basename ⇒ String
The basename, if any, of the file in the path component.
1689 1690 1691 1692 |
# File 'lib/addressable/uri.rb', line 1689 def basename # Path cannot be nil return File.basename(self.path).gsub(/;[^\/]*$/, "") end |
#display_uri ⇒ Addressable::URI
Creates a URI suitable for display to users. If semantic attacks are likely, the application should try to detect these and warn the user. See <a href=“www.ietf.org/rfc/rfc3986.txt”>RFC 3986</a>, section 7.6 for more information.
2203 2204 2205 2206 2207 2208 |
# File 'lib/addressable/uri.rb', line 2203 def display_uri display_uri = self.normalize display_uri.instance_variable_set("@host", ::Addressable::IDNA.to_unicode(display_uri.host)) return display_uri end |
#dup ⇒ Addressable::URI
Clones the URI object.
2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 |
# File 'lib/addressable/uri.rb', line 2271 def dup duplicated_uri = Addressable::URI.new( :scheme => self.scheme ? self.scheme.dup : nil, :user => self.user ? self.user.dup : nil, :password => self.password ? self.password.dup : nil, :host => self.host ? self.host.dup : nil, :port => self.port, :path => self.path ? self.path.dup : nil, :query => self.query ? self.query.dup : nil, :fragment => self.fragment ? self.fragment.dup : nil ) return duplicated_uri end |
#eql?(uri) ⇒ TrueClass, FalseClass
Returns true
if the URI objects are equal. This method does NOT normalize either URI before doing the comparison.
2253 2254 2255 2256 |
# File 'lib/addressable/uri.rb', line 2253 def eql?(uri) return false unless uri.kind_of?(self.class) return self.to_s == uri.to_s end |
#extname ⇒ String
The extname, if any, of the file in the path component. Empty string if there is no extension.
1699 1700 1701 1702 |
# File 'lib/addressable/uri.rb', line 1699 def extname return nil unless self.path return File.extname(self.basename) end |
#extract_mapping(pattern, processor = nil) ⇒ Hash, NilClass
Extracts a mapping from the URI using a URI Template pattern.
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'lib/addressable/uri.rb', line 597 def extract_mapping(pattern, processor=nil) reserved = Addressable::URI::CharacterClasses::RESERVED unreserved = Addressable::URI::CharacterClasses::UNRESERVED anything = reserved + unreserved operator_expansion = /\{-([a-zA-Z]+)\|([#{anything}]+)\|([#{anything}]+)\}/ variable_expansion = /\{([#{anything}]+?)(=([#{anything}]+))?\}/ # First, we need to process the pattern, and extract the values. expansions, expansion_regexp = parse_template_pattern(pattern, processor) unparsed_values = self.to_s.scan(expansion_regexp).flatten mapping = {} if self.to_s == pattern return mapping elsif expansions.size > 0 && expansions.size == unparsed_values.size expansions.each_with_index do |expansion, index| unparsed_value = unparsed_values[index] if expansion =~ operator_expansion operator, argument, variables = parse_template_expansion(expansion) extract_method = "extract_#{operator}_operator" if ([extract_method, extract_method.to_sym] & private_methods).empty? raise InvalidTemplateOperatorError, "Invalid template operator: #{operator}" else begin send( extract_method.to_sym, unparsed_value, processor, argument, variables, mapping ) rescue TemplateOperatorAbortedError return nil end end else name = expansion[variable_expansion, 1] value = unparsed_value if processor != nil && processor.respond_to?(:restore) value = processor.restore(name, value) end mapping[name] = value end end return mapping else return nil end end |
#fragment ⇒ String
The fragment component for this URI.
1834 1835 1836 |
# File 'lib/addressable/uri.rb', line 1834 def fragment return @fragment end |
#fragment=(new_fragment) ⇒ Object
Sets the fragment component for this URI.
1850 1851 1852 1853 1854 1855 |
# File 'lib/addressable/uri.rb', line 1850 def fragment=(new_fragment) @fragment = new_fragment ? new_fragment.to_str : nil # Reset dependant values @normalized_fragment = nil end |
#hash ⇒ Integer
A hash value that will make a URI equivalent to its normalized form.
2263 2264 2265 |
# File 'lib/addressable/uri.rb', line 2263 def hash return (self.normalize.to_s.hash * -1) end |
#host ⇒ String
The host component for this URI.
1432 1433 1434 |
# File 'lib/addressable/uri.rb', line 1432 def host return @host end |
#host=(new_host) ⇒ Object
Sets the host component for this URI.
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 |
# File 'lib/addressable/uri.rb', line 1465 def host=(new_host) @host = new_host ? new_host.to_str : nil # Reset dependant values @authority = nil @normalized_host = nil # Ensure we haven't created an invalid URI validate() end |
#inferred_port ⇒ Integer
The inferred port component for this URI. This method will normalize to the default port for the URI’s scheme if the port isn’t explicitly specified in the URI.
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 |
# File 'lib/addressable/uri.rb', line 1634 def inferred_port @inferred_port ||= (begin if port.to_i == 0 if scheme self.class.port_mapping[scheme.strip.downcase] else nil end else port.to_i end end) end |
#inspect ⇒ String
Returns a String
representation of the URI object’s state.
2369 2370 2371 |
# File 'lib/addressable/uri.rb', line 2369 def inspect sprintf("#<%s:%#0x URI:%s>", self.class.to_s, self.object_id, self.to_s) end |
#ip_based? ⇒ TrueClass, FalseClass
Determines if the scheme indicates an IP-based protocol.
1863 1864 1865 1866 1867 1868 1869 |
# File 'lib/addressable/uri.rb', line 1863 def ip_based? if self.scheme return self.class.ip_based_schemes.include?( self.scheme.strip.downcase) end return false end |
#join(uri) ⇒ Addressable::URI Also known as: +
Joins two URIs together.
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 |
# File 'lib/addressable/uri.rb', line 1897 def join(uri) if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if !uri.kind_of?(self.class) # Otherwise, convert to a String, then parse. uri = self.class.parse(uri.to_str) end if uri.to_s == "" return self.dup end joined_scheme = nil joined_user = nil joined_password = nil joined_host = nil joined_port = nil joined_path = nil joined_query = nil joined_fragment = nil # Section 5.2.2 of RFC 3986 if uri.scheme != nil joined_scheme = uri.scheme joined_user = uri.user joined_password = uri.password joined_host = uri.host joined_port = uri.port joined_path = self.class.normalize_path(uri.path) joined_query = uri.query else if uri. != nil joined_user = uri.user joined_password = uri.password joined_host = uri.host joined_port = uri.port joined_path = self.class.normalize_path(uri.path) joined_query = uri.query else if uri.path == nil || uri.path == "" joined_path = self.path if uri.query != nil joined_query = uri.query else joined_query = self.query end else if uri.path[0..0] == "/" joined_path = self.class.normalize_path(uri.path) else base_path = self.path.dup base_path = "" if base_path == nil base_path = self.class.normalize_path(base_path) # Section 5.2.3 of RFC 3986 # # Removes the right-most path segment from the base path. if base_path =~ /\// base_path.gsub!(/\/[^\/]+$/, "/") else base_path = "" end # If the base path is empty and an authority segment has been # defined, use a base path of "/" if base_path == "" && self. != nil base_path = "/" end joined_path = self.class.normalize_path(base_path + uri.path) end joined_query = uri.query end joined_user = self.user joined_password = self.password joined_host = self.host joined_port = self.port end joined_scheme = self.scheme end joined_fragment = uri.fragment return Addressable::URI.new( :scheme => joined_scheme, :user => joined_user, :password => joined_password, :host => joined_host, :port => joined_port, :path => joined_path, :query => joined_query, :fragment => joined_fragment ) end |
#join!(uri) ⇒ Addressable::URI
Destructive form of join
.
2000 2001 2002 |
# File 'lib/addressable/uri.rb', line 2000 def join!(uri) replace_self(self.join(uri)) end |
#merge(hash) ⇒ Addressable::URI
Merges a URI with a Hash
of components. This method has different behavior from join
. Any components present in the hash
parameter will override the original components. The path component is not treated specially.
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 |
# File 'lib/addressable/uri.rb', line 2015 def merge(hash) if !hash.respond_to?(:to_hash) raise TypeError, "Can't convert #{hash.class} into Hash." end hash = hash.to_hash if hash.has_key?(:authority) if (hash.keys & [:userinfo, :user, :password, :host, :port]).any? raise ArgumentError, "Cannot specify both an authority and any of the components " + "within the authority." end end if hash.has_key?(:userinfo) if (hash.keys & [:user, :password]).any? raise ArgumentError, "Cannot specify both a userinfo and either the user or password." end end uri = Addressable::URI.new uri.validation_deferred = true uri.scheme = hash.has_key?(:scheme) ? hash[:scheme] : self.scheme if hash.has_key?(:authority) uri. = hash.has_key?(:authority) ? hash[:authority] : self. end if hash.has_key?(:userinfo) uri.userinfo = hash.has_key?(:userinfo) ? hash[:userinfo] : self.userinfo end if !hash.has_key?(:userinfo) && !hash.has_key?(:authority) uri.user = hash.has_key?(:user) ? hash[:user] : self.user uri.password = hash.has_key?(:password) ? hash[:password] : self.password end if !hash.has_key?(:authority) uri.host = hash.has_key?(:host) ? hash[:host] : self.host uri.port = hash.has_key?(:port) ? hash[:port] : self.port end uri.path = hash.has_key?(:path) ? hash[:path] : self.path uri.query = hash.has_key?(:query) ? hash[:query] : self.query uri.fragment = hash.has_key?(:fragment) ? hash[:fragment] : self.fragment uri.validation_deferred = false return uri end |
#merge!(uri) ⇒ Addressable::URI
Destructive form of merge
.
2078 2079 2080 |
# File 'lib/addressable/uri.rb', line 2078 def merge!(uri) replace_self(self.merge(uri)) end |
#normalize ⇒ Addressable::URI
Returns a normalized URI object.
NOTE: This method does not attempt to fully conform to specifications. It exists largely to correct other people’s failures to read the specifications, and also to deal with caching issues since several different URIs may represent the same resource and should not be cached multiple times.
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 |
# File 'lib/addressable/uri.rb', line 2163 def normalize # This is a special exception for the frequently misused feed # URI scheme. if normalized_scheme == "feed" if self.to_s =~ /^feed:\/*http:\/*/ return self.class.parse( self.to_s[/^feed:\/*(http:\/*.*)/, 1] ).normalize end end return Addressable::URI.normalized_encode( Addressable::URI.new( :scheme => normalized_scheme, :authority => , :path => normalized_path, :query => normalized_query, :fragment => normalized_fragment ), ::Addressable::URI ) end |
#normalize! ⇒ Addressable::URI
Destructively normalizes this URI object.
2192 2193 2194 |
# File 'lib/addressable/uri.rb', line 2192 def normalize! replace_self(self.normalize) end |
#normalized_authority ⇒ String
The authority component for this URI, normalized.
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 |
# File 'lib/addressable/uri.rb', line 1503 def @normalized_authority ||= (begin if self.normalized_host.nil? nil else = "" if self.normalized_userinfo != nil << "#{self.normalized_userinfo}@" end << self.normalized_host if self.normalized_port != nil << ":#{self.normalized_port}" end end end) end |
#normalized_fragment ⇒ String
The fragment component for this URI, normalized.
1842 1843 1844 |
# File 'lib/addressable/uri.rb', line 1842 def normalized_fragment @normalized_fragment ||= (self.fragment ? self.fragment.strip : nil) end |
#normalized_host ⇒ String
The host component for this URI, normalized.
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 |
# File 'lib/addressable/uri.rb', line 1440 def normalized_host @normalized_host ||= (begin if self.host != nil if self.host.strip != "" result = ::Addressable::IDNA.to_ascii( self.class.unencode_component(self.host.strip.downcase) ) if result[-1..-1] == "." # Trailing dots are unnecessary result = result[0...-1] end result else "" end else nil end end) end |
#normalized_password ⇒ String
The password component for this URI, normalized.
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 |
# File 'lib/addressable/uri.rb', line 1329 def normalized_password @normalized_password ||= (begin if self.password if normalized_scheme =~ /https?/ && self.password.strip == "" && (!self.user || self.user.strip == "") nil else self.password.strip end else nil end end) end |
#normalized_path ⇒ String
The path component for this URI, normalized.
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 |
# File 'lib/addressable/uri.rb', line 1660 def normalized_path @normalized_path ||= (begin result = self.class.normalize_path(self.path.strip) if result == "" && ["http", "https", "ftp", "tftp"].include?(self.normalized_scheme) result = "/" end result end) end |
#normalized_port ⇒ Integer
The port component for this URI, normalized.
1596 1597 1598 1599 1600 1601 1602 1603 1604 |
# File 'lib/addressable/uri.rb', line 1596 def normalized_port @normalized_port ||= (begin if self.class.port_mapping[normalized_scheme] == self.port nil else self.port end end) end |
#normalized_query ⇒ String
The query component for this URI, normalized.
1716 1717 1718 |
# File 'lib/addressable/uri.rb', line 1716 def normalized_query @normalized_query ||= (self.query ? self.query.strip : nil) end |
#normalized_scheme ⇒ String
The scheme component for this URI, normalized.
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 |
# File 'lib/addressable/uri.rb', line 1242 def normalized_scheme @normalized_scheme ||= (begin if self.scheme != nil if self.scheme =~ /^\s*ssh\+svn\s*$/i "svn+ssh" else self.scheme.strip.downcase end else nil end end) end |
#normalized_user ⇒ String
The user component for this URI, normalized.
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 |
# File 'lib/addressable/uri.rb', line 1280 def normalized_user @normalized_user ||= (begin if self.user if normalized_scheme =~ /https?/ && self.user.strip == "" && (!self.password || self.password.strip == "") nil else self.user.strip end else nil end end) end |
#normalized_userinfo ⇒ String
The userinfo component for this URI, normalized.
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 |
# File 'lib/addressable/uri.rb', line 1389 def normalized_userinfo @normalized_userinfo ||= (begin current_user = self.normalized_user current_password = self.normalized_password if !current_user && !current_password nil elsif current_user && current_password "#{current_user}:#{current_password}" elsif current_user && !current_password "#{current_user}" end end) end |
#omit(*components) ⇒ Addressable::URI
Omits components from a URI.
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 |
# File 'lib/addressable/uri.rb', line 2297 def omit(*components) invalid_components = components - [ :scheme, :user, :password, :userinfo, :host, :port, :authority, :path, :query, :fragment ] unless invalid_components.empty? raise ArgumentError, "Invalid component names: #{invalid_components.inspect}." end duplicated_uri = self.dup duplicated_uri.validation_deferred = true components.each do |component| duplicated_uri.send((component.to_s + "=").to_sym, nil) end duplicated_uri.validation_deferred = false duplicated_uri end |
#omit!(*components) ⇒ Addressable::URI
Destructive form of omit.
2323 2324 2325 |
# File 'lib/addressable/uri.rb', line 2323 def omit!(*components) replace_self(self.omit(*components)) end |
#password ⇒ String
The password component for this URI.
1321 1322 1323 |
# File 'lib/addressable/uri.rb', line 1321 def password return @password end |
#password=(new_password) ⇒ Object
Sets the password component for this URI.
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 |
# File 'lib/addressable/uri.rb', line 1348 def password=(new_password) @password = new_password ? new_password.to_str : nil # You can't have a nil user with a non-nil password if @password != nil @user = "" if @user.nil? end # Reset dependant values @userinfo = nil @normalized_userinfo = nil @authority = nil @normalized_password = nil # Ensure we haven't created an invalid URI validate() end |
#path ⇒ String
The path component for this URI.
1652 1653 1654 |
# File 'lib/addressable/uri.rb', line 1652 def path return (@path || "") end |
#path=(new_path) ⇒ Object
Sets the path component for this URI.
1675 1676 1677 1678 1679 1680 1681 1682 1683 |
# File 'lib/addressable/uri.rb', line 1675 def path=(new_path) @path = (new_path || "").to_str if @path != "" && @path[0..0] != "/" && host != nil @path = "/#{@path}" end # Reset dependant values @normalized_path = nil end |
#port ⇒ Integer
The port component for this URI. This is the port number actually given in the URI. This does not infer port numbers from default values.
1588 1589 1590 |
# File 'lib/addressable/uri.rb', line 1588 def port return @port end |
#port=(new_port) ⇒ Object
Sets the port component for this URI.
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 |
# File 'lib/addressable/uri.rb', line 1610 def port=(new_port) if new_port != nil && !(new_port.to_s =~ /^\d+$/) raise InvalidURIError, "Invalid port number: #{new_port.inspect}" end @port = new_port.to_s.to_i @port = nil if @port == 0 # Reset dependant values @authority = nil @inferred_port = nil @normalized_port = nil # Ensure we haven't created an invalid URI validate() end |
#query ⇒ String
The query component for this URI.
1708 1709 1710 |
# File 'lib/addressable/uri.rb', line 1708 def query return @query end |
#query=(new_query) ⇒ Object
Sets the query component for this URI.
1724 1725 1726 1727 1728 1729 |
# File 'lib/addressable/uri.rb', line 1724 def query=(new_query) @query = new_query.to_str # Reset dependant values @normalized_query = nil end |
#query_values(options = {}) ⇒ Hash
Converts the query component to a Hash value.
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 |
# File 'lib/addressable/uri.rb', line 1762 def query_values(={}) defaults = {:notation => :subscript} = defaults.merge() if ![:flat, :dot, :subscript].include?([:notation]) raise ArgumentError, "Invalid notation. Must be one of: [:flat, :dot, :subscript]." end return nil if self.query == nil return (self.query.split("&").map do |pair| pair.split("=") end).inject({}) do |accumulator, pair| key, value = pair value = true if value.nil? key = self.class.unencode_component(key) if value != true value = self.class.unencode_component(value).gsub(/\+/, " ") end if [:notation] == :flat if accumulator[key] raise ArgumentError, "Key was repeated: #{key.inspect}" end accumulator[key] = value else if [:notation] == :dot array_value = false subkeys = key.split(".") elsif [:notation] == :subscript array_value = !!(key =~ /\[\]$/) subkeys = key.split(/[\[\]]+/) end current_hash = accumulator for i in 0...(subkeys.size - 1) subkey = subkeys[i] current_hash[subkey] = {} unless current_hash[subkey] current_hash = current_hash[subkey] end if array_value current_hash[subkeys.last] = [] unless current_hash[subkeys.last] current_hash[subkeys.last] << value else current_hash[subkeys.last] = value end end accumulator end end |
#query_values=(new_query_values) ⇒ Object
Sets the query component for this URI from a Hash object.
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 |
# File 'lib/addressable/uri.rb', line 1813 def query_values=(new_query_values) @query = (new_query_values.to_hash.inject([]) do |accumulator, pair| key, value = pair key = self.class.encode_component(key, CharacterClasses::UNRESERVED) if value == true accumulator << "#{key}" else value = self.class.encode_component( value, CharacterClasses::UNRESERVED) accumulator << "#{key}=#{value}" end end).join("&") # Reset dependant values @normalized_query = nil end |
#relative? ⇒ TrueClass, FalseClass
Determines if the URI is relative.
1877 1878 1879 |
# File 'lib/addressable/uri.rb', line 1877 def relative? return self.scheme.nil? end |
#route_from(uri) ⇒ Addressable::URI
Returns the shortest normalized relative form of this URI that uses the supplied URI as a base for resolution. Returns an absolute URI if necessary. This is effectively the opposite of route_to
.
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 |
# File 'lib/addressable/uri.rb', line 2091 def route_from(uri) uri = self.class.parse(uri).normalize normalized_self = self.normalize if normalized_self.relative? raise ArgumentError, "Expected absolute URI, got: #{self.to_s}" end if uri.relative? raise ArgumentError, "Expected absolute URI, got: #{uri.to_s}" end if normalized_self == uri return Addressable::URI.parse("##{normalized_self.fragment}") end components = normalized_self.to_hash if normalized_self.scheme == uri.scheme components[:scheme] = nil if normalized_self. == uri. components[:user] = nil components[:password] = nil components[:host] = nil components[:port] = nil if normalized_self.path == uri.path components[:path] = nil if normalized_self.query == uri.query components[:query] = nil end else if uri.path != "/" components[:path].gsub!( Regexp.new("^" + Regexp.escape(uri.path)), "") end end end end # Avoid network-path references. if components[:host] != nil components[:scheme] = normalized_self.scheme end return Addressable::URI.new( :scheme => components[:scheme], :user => components[:user], :password => components[:password], :host => components[:host], :port => components[:port], :path => components[:path], :query => components[:query], :fragment => components[:fragment] ) end |
#route_to(uri) ⇒ Addressable::URI
Returns the shortest normalized relative form of the supplied URI that uses this URI as a base for resolution. Returns an absolute URI if necessary. This is effectively the opposite of route_from
.
2149 2150 2151 |
# File 'lib/addressable/uri.rb', line 2149 def route_to(uri) return self.class.parse(uri).route_from(self) end |
#scheme ⇒ String
The scheme component for this URI.
1234 1235 1236 |
# File 'lib/addressable/uri.rb', line 1234 def scheme return @scheme end |
#scheme=(new_scheme) ⇒ Object
Sets the scheme component for this URI.
1260 1261 1262 1263 1264 1265 1266 |
# File 'lib/addressable/uri.rb', line 1260 def scheme=(new_scheme) @scheme = new_scheme ? new_scheme.to_str : nil @scheme = nil if @scheme.to_s.strip == "" # Reset dependant values @normalized_scheme = nil end |
#to_hash ⇒ Hash
Returns a Hash of the URI components.
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 |
# File 'lib/addressable/uri.rb', line 2352 def to_hash return { :scheme => self.scheme, :user => self.user, :password => self.password, :host => self.host, :port => self.port, :path => self.path, :query => self.query, :fragment => self.fragment } end |
#to_s ⇒ String Also known as: to_str
Converts the URI to a String
.
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 |
# File 'lib/addressable/uri.rb', line 2331 def to_s uri_string = "" uri_string << "#{self.scheme}:" if self.scheme != nil uri_string << "//#{self.}" if self. != nil uri_string << self.path.to_s uri_string << "?#{self.query}" if self.query != nil uri_string << "##{self.fragment}" if self.fragment != nil if uri_string.respond_to?(:force_encoding) uri_string.force_encoding(Encoding::UTF_8) end return uri_string end |
#user ⇒ String
The user component for this URI.
1272 1273 1274 |
# File 'lib/addressable/uri.rb', line 1272 def user return @user end |
#user=(new_user) ⇒ Object
Sets the user component for this URI.
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 |
# File 'lib/addressable/uri.rb', line 1299 def user=(new_user) @user = new_user ? new_user.to_str : nil # You can't have a nil user with a non-nil password if @password != nil @user = "" if @user.nil? end # Reset dependant values @userinfo = nil @normalized_userinfo = nil @authority = nil @normalized_user = nil # Ensure we haven't created an invalid URI validate() end |
#userinfo ⇒ String
The userinfo component for this URI. Combines the user and password components.
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 |
# File 'lib/addressable/uri.rb', line 1371 def userinfo @userinfo ||= (begin current_user = self.user current_password = self.password if !current_user && !current_password nil elsif current_user && current_password "#{current_user}:#{current_password}" elsif current_user && !current_password "#{current_user}" end end) end |
#userinfo=(new_userinfo) ⇒ Object
Sets the userinfo component for this URI.
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 |
# File 'lib/addressable/uri.rb', line 1407 def userinfo=(new_userinfo) new_user, new_password = if new_userinfo [ new_userinfo.to_str.strip[/^(.*):/, 1], new_userinfo.to_str.strip[/:(.*)$/, 1] ] else [nil, nil] end # Password assigned first to ensure validity in case of nil self.password = new_password self.user = new_user # Reset dependant values @authority = nil # Ensure we haven't created an invalid URI validate() end |
#validation_deferred ⇒ TrueClass, FalseClass
If URI validation needs to be disabled, this can be set to true.
2379 2380 2381 |
# File 'lib/addressable/uri.rb', line 2379 def validation_deferred @validation_deferred ||= false end |
#validation_deferred=(new_validation_deferred) ⇒ Object
If URI validation needs to be disabled, this can be set to true.
2389 2390 2391 2392 |
# File 'lib/addressable/uri.rb', line 2389 def validation_deferred=(new_validation_deferred) @validation_deferred = new_validation_deferred validate unless @validation_deferred end |