Class: URI::Generic
Overview
Base class for all URI classes. Implements generic URI syntax as per RFC 2396.
Constant Summary collapse
- DEFAULT_PORT =
nil
- COMPONENT =
[ :scheme, :userinfo, :host, :port, :registry, :path, :opaque, :query, :fragment ].freeze
- USE_REGISTRY =
false
- @@to_s =
Kernel.instance_method(:to_s)
Constants included from REGEXP
REGEXP::ABS_PATH, REGEXP::ABS_URI, REGEXP::ABS_URI_REF, REGEXP::ESCAPED, REGEXP::FRAGMENT, REGEXP::HOST, REGEXP::OPAQUE, REGEXP::PORT, REGEXP::QUERY, REGEXP::REGISTRY, REGEXP::REL_PATH, REGEXP::REL_URI, REGEXP::REL_URI_REF, REGEXP::SCHEME, REGEXP::UNSAFE, REGEXP::URI_REF, REGEXP::USERINFO
Constants included from URI
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Returns the value of attribute fragment.
-
#host ⇒ Object
Returns the value of attribute host.
-
#opaque ⇒ Object
Returns the value of attribute opaque.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#query ⇒ Object
Returns the value of attribute query.
-
#registry ⇒ Object
Returns the value of attribute registry.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
Class Method Summary collapse
-
.build(args) ⇒ Object
Synopsis.
-
.build2(args) ⇒ Object
Synopsis.
-
.component ⇒ Object
Components of the URI in the order.
-
.default_port ⇒ Object
Returns default port.
-
.use_registry ⇒ Object
DOC: FIXME!.
Instance Method Summary collapse
-
#==(oth) ⇒ Object
Compares to URI's.
-
#absolute? ⇒ Boolean
(also: #absolute)
Checks if URI is an absolute one.
- #coerce(oth) ⇒ Object
- #component ⇒ Object
- #default_port ⇒ Object
- #eql?(oth) ⇒ Boolean
- #hash ⇒ Object
-
#hierarchical? ⇒ Boolean
Checks if URI has a path.
-
#initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, arg_check = false) ⇒ Generic
constructor
Args.
- #inspect ⇒ Object
-
#merge(oth) ⇒ Object
(also: #+)
Args.
-
#merge!(oth) ⇒ Object
Args.
-
#normalize ⇒ Object
Returns normalized URI.
-
#normalize! ⇒ Object
Destructive version of #normalize.
- #password ⇒ Object
- #password=(password) ⇒ Object
-
#relative? ⇒ Boolean
Checks if URI is relative.
-
#route_from(oth) ⇒ Object
(also: #-)
Args.
-
#route_to(oth) ⇒ Object
Args.
-
#select(*components) ⇒ Object
Args.
-
#to_s ⇒ Object
Constructs String from URI.
- #user ⇒ Object
- #user=(user) ⇒ Object
- #userinfo ⇒ Object
-
#userinfo=(userinfo) ⇒ Object
Sets userinfo, argument is string like 'name:pass'.
Methods included from URI
extract, join, parse, regexp, split
Methods included from Escape
Constructor Details
#initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, arg_check = false) ⇒ Generic
Args
scheme
-
Protocol scheme, i.e. 'http','ftp','mailto' and so on.
userinfo
-
User name and password, i.e. 'sdmitry:bla'
host
-
Server host name
port
-
Server port
registry
-
DOC: FIXME!
path
-
Path on server
opaque
-
DOC: FIXME!
query
-
Query data
fragment
-
A part of URI after '#' sign
arg_check
-
Check arguments [false by default]
Description
Creates a new URI::Generic instance from "generic" components without check.
156 157 158 159 160 161 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/uri/generic.rb', line 156 def initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, arg_check = false) @scheme = nil @user = nil @password = nil @host = nil @port = nil @path = nil @query = nil @opaque = nil @registry = nil @fragment = nil if arg_check self.scheme = scheme self.userinfo = userinfo self.host = host self.port = port self.path = path self.query = query self.opaque = opaque self.registry = registry self.fragment = fragment else self.set_scheme(scheme) self.set_userinfo(userinfo) self.set_host(host) self.set_port(port) self.set_path(path) self.set_query(query) self.set_opaque(opaque) self.set_registry(registry) self.set_fragment(fragment) end if @registry && !self.class.use_registry raise InvalidURIError, "the scheme #{@scheme} does not accept registry part: #{@registry} (or bad hostname?)" end @scheme.freeze if @scheme self.set_path('') if !@path && !@opaque # (see RFC2396 Section 5.2) self.set_port(self.default_port) if self.default_port && !@port end |
Instance Attribute Details
#fragment ⇒ Object
Returns the value of attribute fragment
210 211 212 |
# File 'lib/uri/generic.rb', line 210 def fragment @fragment end |
#host ⇒ Object
Returns the value of attribute host
204 205 206 |
# File 'lib/uri/generic.rb', line 204 def host @host end |
#opaque ⇒ Object
Returns the value of attribute opaque
209 210 211 |
# File 'lib/uri/generic.rb', line 209 def opaque @opaque end |
#path ⇒ Object
Returns the value of attribute path
207 208 209 |
# File 'lib/uri/generic.rb', line 207 def path @path end |
#port ⇒ Object
Returns the value of attribute port
205 206 207 |
# File 'lib/uri/generic.rb', line 205 def port @port end |
#query ⇒ Object
Returns the value of attribute query
208 209 210 |
# File 'lib/uri/generic.rb', line 208 def query @query end |
#registry ⇒ Object
Returns the value of attribute registry
206 207 208 |
# File 'lib/uri/generic.rb', line 206 def registry @registry end |
#scheme ⇒ Object
Returns the value of attribute scheme
203 204 205 |
# File 'lib/uri/generic.rb', line 203 def scheme @scheme end |
Class Method Details
.build(args) ⇒ Object
Synopsis
See #new
Description
Creates a new URI::Generic instance from components of URI::Generic with check. Components are: scheme, userinfo, host, port, registry, path, opaque, query and fragment. You can provide arguments either by an Array or a Hash. See #new for hash keys to use or for order of array items.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/uri/generic.rb', line 108 def self.build(args) if args.kind_of?(Array) && args.size == ::URI::Generic::COMPONENT.size tmp = args elsif args.kind_of?(Hash) tmp = ::URI::Generic::COMPONENT.collect do |c| if args.include?(c) args[c] else nil end end else raise ArgumentError, "expected Array of or Hash of components of #{self.class} (#{self.class.component.join(', ')})" end tmp << true return self.new(*tmp) end |
.build2(args) ⇒ Object
Synopsis
See #new
Description
At first, tries to create a new URI::Generic instance using URI::Generic::build. But, if exception URI::InvalidComponentError is raised, then it URI::Escape.escape all URI components and tries again.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/uri/generic.rb', line 70 def self.build2(args) begin return self.build(args) rescue InvalidComponentError if args.kind_of?(Array) return self.build(args.collect{|x| if x URI.escape(x) else x end }) elsif args.kind_of?(Hash) tmp = {} args.each do |key, value| tmp[key] = if value URI.escape(value) else value end end return self.build(tmp) end end end |
.component ⇒ Object
Components of the URI in the order.
45 46 47 |
# File 'lib/uri/generic.rb', line 45 def self.component self::COMPONENT end |
.default_port ⇒ Object
Returns default port
26 27 28 |
# File 'lib/uri/generic.rb', line 26 def self.default_port self::DEFAULT_PORT end |
.use_registry ⇒ Object
DOC: FIXME!
54 55 56 |
# File 'lib/uri/generic.rb', line 54 def self.use_registry self::USE_REGISTRY end |
Instance Method Details
#==(oth) ⇒ Object
Compares to URI's
1044 1045 1046 1047 1048 1049 1050 |
# File 'lib/uri/generic.rb', line 1044 def ==(oth) if self.class == oth.class self.normalize.component_ary == oth.normalize.component_ary else false end end |
#absolute? ⇒ Boolean Also known as: absolute
Checks if URI is an absolute one
597 598 599 600 601 602 603 |
# File 'lib/uri/generic.rb', line 597 def absolute? if @scheme true else false end end |
#coerce(oth) ⇒ Object
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 |
# File 'lib/uri/generic.rb', line 1112 def coerce(oth) case oth when String oth = URI.parse(oth) else super end return oth, self end |
#component ⇒ Object
224 225 226 |
# File 'lib/uri/generic.rb', line 224 def component self.class.component end |
#default_port ⇒ Object
30 31 32 |
# File 'lib/uri/generic.rb', line 30 def default_port self.class.default_port end |
#eql?(oth) ⇒ Boolean
1056 1057 1058 1059 |
# File 'lib/uri/generic.rb', line 1056 def eql?(oth) self.class == oth.class && self.component_ary.eql?(oth.component_ary) end |
#hash ⇒ Object
1052 1053 1054 |
# File 'lib/uri/generic.rb', line 1052 def hash self.component_ary.hash end |
#hierarchical? ⇒ Boolean
Checks if URI has a path
586 587 588 589 590 591 592 |
# File 'lib/uri/generic.rb', line 586 def hierarchical? if @path true else false end end |
#inspect ⇒ Object
1108 1109 1110 |
# File 'lib/uri/generic.rb', line 1108 def inspect @@to_s.bind(self).call.sub!(/>\z/) {" URL:#{self}>"} end |
#merge(oth) ⇒ Object Also known as: +
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 |
# File 'lib/uri/generic.rb', line 728 def merge(oth) begin base, rel = merge0(oth) rescue raise $!.class, $!. end if base == rel return base end = rel.userinfo || rel.host || rel.port # RFC2396, Section 5.2, 2) if (rel.path.nil? || rel.path.empty?) && ! && !rel.query base.set_fragment(rel.fragment) if rel.fragment return base end base.set_query(nil) base.set_fragment(nil) # RFC2396, Section 5.2, 4) if ! base.set_path(merge_path(base.path, rel.path)) if base.path && rel.path else # RFC2396, Section 5.2, 4) base.set_path(rel.path) if rel.path end # RFC2396, Section 5.2, 7) base.set_userinfo(rel.userinfo) if rel.userinfo base.set_host(rel.host) if rel.host base.set_port(rel.port) if rel.port base.set_query(rel.query) if rel.query base.set_fragment(rel.fragment) if rel.fragment return base end |
#merge!(oth) ⇒ Object
700 701 702 703 704 705 706 707 708 |
# File 'lib/uri/generic.rb', line 700 def merge!(oth) t = merge(oth) if self == t nil else replace!(t) self end end |
#normalize ⇒ Object
Returns normalized URI
970 971 972 973 974 |
# File 'lib/uri/generic.rb', line 970 def normalize uri = dup uri.normalize! uri end |
#normalize! ⇒ Object
Destructive version of #normalize
979 980 981 982 983 984 985 986 |
# File 'lib/uri/generic.rb', line 979 def normalize! if path && path == '' set_path('/') end if host && host != host.downcase set_host(self.host.downcase) end end |
#password ⇒ Object
372 373 374 |
# File 'lib/uri/generic.rb', line 372 def password @password end |
#password=(password) ⇒ Object
316 317 318 319 320 |
# File 'lib/uri/generic.rb', line 316 def password=(password) check_password(password) set_password(password) # returns password end |
#relative? ⇒ Boolean
Checks if URI is relative
609 610 611 |
# File 'lib/uri/generic.rb', line 609 def relative? !absolute? end |
#route_from(oth) ⇒ Object Also known as: -
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 |
# File 'lib/uri/generic.rb', line 914 def route_from(oth) # you can modify `rel', but can not `oth'. begin oth, rel = route_from0(oth) rescue raise $!.class, $!. end if oth == rel return rel end rel.set_path(route_from_path(oth.path, self.path)) if rel.path == './' && self.query # "./?foo" -> "?foo" rel.set_path('') end return rel end |
#route_to(oth) ⇒ Object
954 955 956 957 958 959 960 961 962 963 964 965 |
# File 'lib/uri/generic.rb', line 954 def route_to(oth) case oth when Generic when String oth = URI.parse(oth) else raise ArgumentError, "bad argument(expected URI object or URI string)" end oth.route_from(self) end |
#select(*components) ⇒ Object
Args
components
-
Multiple Symbol arguments defined in URI::HTTP
Description
Selects specified components from URI
Usage
require 'uri'
uri = URI.parse('http://myuser:[email protected]/test.rbx')
p uri.select(:userinfo, :host, :path)
# => ["myuser:mypass", "my.example.com", "/test.rbx"]
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 |
# File 'lib/uri/generic.rb', line 1096 def select(*components) components.collect do |c| if component.include?(c) self.send(c) else raise ArgumentError, "expected of components of #{self.class} (#{self.class.component.join(', ')})" end end end |
#to_s ⇒ Object
Constructs String from URI
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 |
# File 'lib/uri/generic.rb', line 1000 def to_s str = '' if @scheme str << @scheme str << ':' end if @opaque str << @opaque else if @registry str << @registry else if @host str << '//' end if self.userinfo str << self.userinfo str << '@' end if @host str << @host end if @port && @port != self.default_port str << ':' str << @port.to_s end end str << path_query end if @fragment str << '#' str << @fragment end str end |
#user ⇒ Object
368 369 370 |
# File 'lib/uri/generic.rb', line 368 def user @user end |
#user=(user) ⇒ Object
310 311 312 313 314 |
# File 'lib/uri/generic.rb', line 310 def user=(user) check_user(user) set_user(user) # returns user end |
#userinfo ⇒ Object
358 359 360 361 362 363 364 365 366 |
# File 'lib/uri/generic.rb', line 358 def userinfo if @user.nil? nil elsif @password.nil? @user else @user + ':' + @password end end |
#userinfo=(userinfo) ⇒ Object
Sets userinfo, argument is string like 'name:pass'
301 302 303 304 305 306 307 308 |
# File 'lib/uri/generic.rb', line 301 def userinfo=(userinfo) if userinfo.nil? return nil end check_userinfo(*userinfo) set_userinfo(*userinfo) # returns userinfo end |