Class: URI::Generic
- Includes:
- URI
- Defined in:
- lib/extensions/uri/uri/generic.rb
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 URI
DEFAULT_PARSER, HTML5ASCIIINCOMPAT, TBLDECWWWCOMP_, TBLENCWWWCOMP_, VERSION, VERSION_CODE, WFKV_
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, parser = DEFAULT_PARSER, 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.
- #parser ⇒ Object
- #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
decode_www_form, decode_www_form_component, encode_www_form, encode_www_form_component, extract, join, parse, regexp, scheme_list, split
Methods included from Escape
Constructor Details
#initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, 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
parser
-
Parser for internal use [URI::DEFAULT_PARSER by default]
arg_check
-
Check arguments [false by default]
Description
Creates a new URI::Generic instance from “generic” components without check.
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 203 204 205 206 |
# File 'lib/extensions/uri/uri/generic.rb', line 158 def initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false) @scheme = nil @user = nil @password = nil @host = nil @port = nil @path = nil @query = nil @opaque = nil @registry = nil @fragment = nil @parser = parser == DEFAULT_PARSER ? nil : parser 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.
214 215 216 |
# File 'lib/extensions/uri/uri/generic.rb', line 214 def fragment @fragment end |
#host ⇒ Object
Returns the value of attribute host.
208 209 210 |
# File 'lib/extensions/uri/uri/generic.rb', line 208 def host @host end |
#opaque ⇒ Object
Returns the value of attribute opaque.
213 214 215 |
# File 'lib/extensions/uri/uri/generic.rb', line 213 def opaque @opaque end |
#path ⇒ Object
Returns the value of attribute path.
211 212 213 |
# File 'lib/extensions/uri/uri/generic.rb', line 211 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
209 210 211 |
# File 'lib/extensions/uri/uri/generic.rb', line 209 def port @port end |
#query ⇒ Object
Returns the value of attribute query.
212 213 214 |
# File 'lib/extensions/uri/uri/generic.rb', line 212 def query @query end |
#registry ⇒ Object
Returns the value of attribute registry.
210 211 212 |
# File 'lib/extensions/uri/uri/generic.rb', line 210 def registry @registry end |
#scheme ⇒ Object
Returns the value of attribute scheme.
207 208 209 |
# File 'lib/extensions/uri/uri/generic.rb', line 207 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.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/extensions/uri/uri/generic.rb', line 107 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 << nil 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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/extensions/uri/uri/generic.rb', line 69 def self.build2(args) begin return self.build(args) rescue InvalidComponentError if args.kind_of?(Array) return self.build(args.collect{|x| if x parser.escape(x) else x end }) elsif args.kind_of?(Hash) tmp = {} args.each do |key, value| tmp[key] = if value parser.escape(value) else value end end return self.build(tmp) end end end |
.component ⇒ Object
Components of the URI in the order.
44 45 46 |
# File 'lib/extensions/uri/uri/generic.rb', line 44 def self.component self::COMPONENT end |
.default_port ⇒ Object
Returns default port
25 26 27 |
# File 'lib/extensions/uri/uri/generic.rb', line 25 def self.default_port self::DEFAULT_PORT end |
.use_registry ⇒ Object
DOC: FIXME!
53 54 55 |
# File 'lib/extensions/uri/uri/generic.rb', line 53 def self.use_registry self::USE_REGISTRY end |
Instance Method Details
#==(oth) ⇒ Object
Compares to URI’s
1057 1058 1059 1060 1061 1062 1063 |
# File 'lib/extensions/uri/uri/generic.rb', line 1057 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
611 612 613 614 615 616 617 |
# File 'lib/extensions/uri/uri/generic.rb', line 611 def absolute? if @scheme true else false end end |
#coerce(oth) ⇒ Object
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 |
# File 'lib/extensions/uri/uri/generic.rb', line 1126 def coerce(oth) case oth when String oth = parser.parse(oth) else super end return oth, self end |
#component ⇒ Object
236 237 238 |
# File 'lib/extensions/uri/uri/generic.rb', line 236 def component self.class.component end |
#default_port ⇒ Object
29 30 31 |
# File 'lib/extensions/uri/uri/generic.rb', line 29 def default_port self.class.default_port end |
#eql?(oth) ⇒ Boolean
1069 1070 1071 1072 1073 |
# File 'lib/extensions/uri/uri/generic.rb', line 1069 def eql?(oth) self.class == oth.class && parser == oth.parser && self.component_ary.eql?(oth.component_ary) end |
#hash ⇒ Object
1065 1066 1067 |
# File 'lib/extensions/uri/uri/generic.rb', line 1065 def hash self.component_ary.hash end |
#hierarchical? ⇒ Boolean
Checks if URI has a path
600 601 602 603 604 605 606 |
# File 'lib/extensions/uri/uri/generic.rb', line 600 def hierarchical? if @path true else false end end |
#inspect ⇒ Object
1122 1123 1124 |
# File 'lib/extensions/uri/uri/generic.rb', line 1122 def inspect @@to_s.bind(self).call.sub!(/>\z/) {" URL:#{self}>"} end |
#merge(oth) ⇒ Object Also known as: +
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 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/extensions/uri/uri/generic.rb', line 742 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
714 715 716 717 718 719 720 721 722 |
# File 'lib/extensions/uri/uri/generic.rb', line 714 def merge!(oth) t = merge(oth) if self == t nil else replace!(t) self end end |
#normalize ⇒ Object
Returns normalized URI
980 981 982 983 984 |
# File 'lib/extensions/uri/uri/generic.rb', line 980 def normalize uri = dup uri.normalize! uri end |
#normalize! ⇒ Object
Destructive version of #normalize
989 990 991 992 993 994 995 996 997 998 999 |
# File 'lib/extensions/uri/uri/generic.rb', line 989 def normalize! if path && path == '' set_path('/') end if scheme && scheme != scheme.downcase set_scheme(self.scheme.downcase) end if host && host != host.downcase set_host(self.host.downcase) end end |
#parser ⇒ Object
216 217 218 219 220 221 222 |
# File 'lib/extensions/uri/uri/generic.rb', line 216 def parser if !defined?(@parser) || !@parser DEFAULT_PARSER else @parser || DEFAULT_PARSER end end |
#password ⇒ Object
384 385 386 |
# File 'lib/extensions/uri/uri/generic.rb', line 384 def password @password end |
#password=(password) ⇒ Object
328 329 330 331 332 |
# File 'lib/extensions/uri/uri/generic.rb', line 328 def password=(password) check_password(password) set_password(password) # returns password end |
#relative? ⇒ Boolean
Checks if URI is relative
623 624 625 |
# File 'lib/extensions/uri/uri/generic.rb', line 623 def relative? !absolute? end |
#route_from(oth) ⇒ Object Also known as: -
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 |
# File 'lib/extensions/uri/uri/generic.rb', line 924 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
964 965 966 967 968 969 970 971 972 973 974 975 |
# File 'lib/extensions/uri/uri/generic.rb', line 964 def route_to(oth) case oth when Generic when String oth = parser.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"]
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
# File 'lib/extensions/uri/uri/generic.rb', line 1110 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
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 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 |
# File 'lib/extensions/uri/uri/generic.rb', line 1013 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
380 381 382 |
# File 'lib/extensions/uri/uri/generic.rb', line 380 def user @user end |
#user=(user) ⇒ Object
322 323 324 325 326 |
# File 'lib/extensions/uri/uri/generic.rb', line 322 def user=(user) check_user(user) set_user(user) # returns user end |
#userinfo ⇒ Object
370 371 372 373 374 375 376 377 378 |
# File 'lib/extensions/uri/uri/generic.rb', line 370 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’
313 314 315 316 317 318 319 320 |
# File 'lib/extensions/uri/uri/generic.rb', line 313 def userinfo=(userinfo) if userinfo.nil? return nil end check_userinfo(*userinfo) set_userinfo(*userinfo) # returns userinfo end |