Class URI::Generic
In: uri/generic.rb
open-uri.rb
Parent: Object

Base class for all URI classes.

Methods

Included Modules

Constants

DEFAULT_PORT = nil
COMPONENT = [ :scheme, :userinfo, :host, :port, :registry, :path, :opaque, :query, :fragment
USE_REGISTRY = false

Attributes

fragment  [R] 
host  [R] 
opaque  [R] 
path  [R] 
port  [R] 
query  [R] 
registry  [R] 
scheme  [R] 

Public Class methods

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.

[Source]

     # File uri/generic.rb, line 107
107:     def self.build(args)
108:       if args.kind_of?(Array) &&
109:           args.size == ::URI::Generic::COMPONENT.size
110:         tmp = args
111:       elsif args.kind_of?(Hash)
112:         tmp = ::URI::Generic::COMPONENT.collect do |c|
113:           if args.include?(c)
114:             args[c]
115:           else
116:             nil
117:           end
118:         end
119:       else
120:         raise ArgumentError, 
121:         "expected Array of or Hash of components of #{self.class} (#{self.class.component.join(', ')})"
122:       end
123: 
124:       tmp << true
125:       return self.new(*tmp)
126:     end

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.

[Source]

    # File uri/generic.rb, line 69
69:     def self.build2(args)
70:       begin
71:         return self.build(args)
72:       rescue InvalidComponentError
73:         if args.kind_of?(Array)
74:           return self.build(args.collect{|x| 
75:             if x
76:               URI.escape(x)
77:             else
78:               x
79:             end
80:           })
81:         elsif args.kind_of?(Hash)
82:           tmp = {}
83:           args.each do |key, value|
84:             tmp[key] = if value
85:                 URI.escape(value)
86:               else
87:                 value
88:               end
89:           end
90:           return self.build(tmp)
91:         end
92:       end
93:     end

Components of the URI in the order.

[Source]

    # File uri/generic.rb, line 44
44:     def self.component
45:       self::COMPONENT
46:     end

Returns default port

[Source]

    # File uri/generic.rb, line 25
25:     def self.default_port
26:       self::DEFAULT_PORT
27:     end

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.

[Source]

     # File uri/generic.rb, line 155
155:     def initialize(scheme, 
156:                    userinfo, host, port, registry, 
157:                    path, opaque, 
158:                    query, 
159:                    fragment,
160:                    arg_check = false)
161:       @scheme = nil
162:       @user = nil
163:       @password = nil
164:       @host = nil
165:       @port = nil
166:       @path = nil
167:       @query = nil
168:       @opaque = nil
169:       @registry = nil
170:       @fragment = nil
171: 
172:       if arg_check
173:         self.scheme = scheme
174:         self.userinfo = userinfo
175:         self.host = host
176:         self.port = port
177:         self.path = path
178:         self.query = query
179:         self.opaque = opaque
180:         self.registry = registry
181:         self.fragment = fragment
182:       else
183:         self.set_scheme(scheme)
184:         self.set_userinfo(userinfo)
185:         self.set_host(host)
186:         self.set_port(port)
187:         self.set_path(path)
188:         self.set_query(query)
189:         self.set_opaque(opaque)
190:         self.set_registry(registry)
191:         self.set_fragment(fragment)
192:       end
193:       if @registry && !self.class.use_registry
194:         raise InvalidURIError, 
195:           "the scheme #{@scheme} does not accept registry part: #{@registry} (or bad hostname?)"
196:       end
197:       
198:       @scheme.freeze if @scheme
199:       self.set_path('') if !@path && !@opaque # (see RFC2396 Section 5.2)
200:       self.set_port(self.default_port) if self.default_port && !@port
201:     end

DOC: FIXME!

[Source]

    # File uri/generic.rb, line 53
53:     def self.use_registry
54:       self::USE_REGISTRY
55:     end

Public Instance methods

+(oth)

Alias for merge

-(oth)

Alias for route_from

Compares to URI’s

[Source]

      # File uri/generic.rb, line 1041
1041:     def ==(oth)
1042:       if self.class == oth.class
1043:         self.normalize.component_ary == oth.normalize.component_ary
1044:       else
1045:         false
1046:       end
1047:     end
absolute()

Alias for absolute?

Checks if URI is an absolute one

[Source]

     # File uri/generic.rb, line 594
594:     def absolute?
595:       if @scheme
596:         true
597:       else
598:         false
599:       end
600:     end

[Source]

      # File uri/generic.rb, line 1107
1107:     def coerce(oth)
1108:       case oth
1109:       when String
1110:         oth = URI.parse(oth)
1111:       else
1112:         super
1113:       end
1114: 
1115:       return oth, self
1116:     end

[Source]

     # File uri/generic.rb, line 223
223:     def component
224:       self.class.component
225:     end

[Source]

    # File uri/generic.rb, line 29
29:     def default_port
30:       self.class.default_port
31:     end

[Source]

      # File uri/generic.rb, line 1053
1053:     def eql?(oth)
1054:       self.component_ary.eql?(oth.component_ary)
1055:     end

returns a proxy URI. The proxy URI is obtained from environment variables such as http_proxy, ftp_proxy, no_proxy, etc. If there is no proper proxy, nil is returned.

Note that capitalized variables (HTTP_PROXY, FTP_PROXY, NO_PROXY, etc.) are examined too.

But http_proxy and HTTP_PROXY is treated specially under CGI environment. It’s because HTTP_PROXY may be set by Proxy: header. So HTTP_PROXY is not used. http_proxy is not used too if the variable is case insensitive. CGI_HTTP_PROXY can be used instead.

[Source]

     # File open-uri.rb, line 563
563:     def find_proxy
564:       name = self.scheme.downcase + '_proxy'
565:       proxy_uri = nil
566:       if name == 'http_proxy' && ENV.include?('REQUEST_METHOD') # CGI?
567:         # HTTP_PROXY conflicts with *_proxy for proxy settings and
568:         # HTTP_* for header information in CGI.
569:         # So it should be careful to use it.
570:         pairs = ENV.reject {|k, v| /\Ahttp_proxy\z/i !~ k }
571:         case pairs.length
572:         when 0 # no proxy setting anyway.
573:           proxy_uri = nil
574:         when 1
575:           k, v = pairs.shift
576:           if k == 'http_proxy' && ENV[k.upcase] == nil
577:             # http_proxy is safe to use because ENV is case sensitive.
578:             proxy_uri = ENV[name]
579:           else
580:             proxy_uri = nil
581:           end
582:         else # http_proxy is safe to use because ENV is case sensitive.
583:           proxy_uri = ENV[name]
584:         end
585:         if !proxy_uri
586:           # Use CGI_HTTP_PROXY.  cf. libwww-perl.
587:           proxy_uri = ENV["CGI_#{name.upcase}"]
588:         end
589:       elsif name == 'http_proxy'
590:         unless proxy_uri = ENV[name]
591:           if proxy_uri = ENV[name.upcase]
592:             warn 'The environment variable HTTP_PROXY is discouraged.  Use http_proxy.'
593:           end
594:         end
595:       else
596:         proxy_uri = ENV[name] || ENV[name.upcase]
597:       end
598: 
599:       if proxy_uri && self.host
600:         require 'socket'
601:         begin
602:           addr = IPSocket.getaddress(self.host)
603:           proxy_uri = nil if /\A127\.|\A::1\z/ =~ addr
604:         rescue SocketError
605:         end
606:       end
607: 
608:       if proxy_uri
609:         proxy_uri = URI.parse(proxy_uri)
610:         name = 'no_proxy'
611:         if no_proxy = ENV[name] || ENV[name.upcase]
612:           no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
613:             if /(\A|\.)#{Regexp.quote host}\z/i =~ self.host &&
614:                (!port || self.port == port.to_i)
615:               proxy_uri = nil
616:               break
617:             end
618:           }
619:         end
620:         proxy_uri
621:       else
622:         nil
623:       end
624:     end

[Source]

     # File uri/generic.rb, line 574
574:     def fragment=(v)
575:       check_fragment(v)
576:       set_fragment(v)
577:       v
578:     end

[Source]

      # File uri/generic.rb, line 1049
1049:     def hash
1050:       self.component_ary.hash
1051:     end

Checks if URI has a path

[Source]

     # File uri/generic.rb, line 583
583:     def hierarchical?
584:       if @path
585:         true
586:       else
587:         false
588:       end
589:     end

[Source]

     # File uri/generic.rb, line 393
393:     def host=(v)
394:       check_host(v)
395:       set_host(v)
396:       v
397:     end

[Source]

      # File uri/generic.rb, line 1103
1103:     def inspect
1104:       sprintf("#<%s:%#0x URL:%s>", self.class.to_s, self.object_id, self.to_s)
1105:     end

Args

oth:URI or String

Description

Merges two URI’s.

Usage

  require 'uri'

  uri = URI.parse("http://my.example.com")
  p uri.merge("/main.rbx?page=1")
  # =>  #<URI::HTTP:0x2021f3b0 URL:http://my.example.com/main.rbx?page=1>

[Source]

     # File uri/generic.rb, line 725
725:     def merge(oth)
726:       begin
727:         base, rel = merge0(oth)
728:       rescue
729:         raise $!.class, $!.message
730:       end
731: 
732:       if base == rel
733:         return base
734:       end
735: 
736:       authority = rel.userinfo || rel.host || rel.port
737: 
738:       # RFC2396, Section 5.2, 2)
739:       if (rel.path.nil? || rel.path.empty?) && !authority && !rel.query
740:         base.set_fragment(rel.fragment) if rel.fragment
741:         return base
742:       end
743: 
744:       base.set_query(nil)
745:       base.set_fragment(nil)
746: 
747:       # RFC2396, Section 5.2, 4)
748:       if !authority
749:         base.set_path(merge_path(base.path, rel.path)) if base.path && rel.path
750:       else
751:         # RFC2396, Section 5.2, 4)
752:         base.set_path(rel.path) if rel.path
753:       end
754: 
755:       # RFC2396, Section 5.2, 7)
756:       base.set_userinfo(rel.userinfo) if rel.userinfo
757:       base.set_host(rel.host)         if rel.host
758:       base.set_port(rel.port)         if rel.port
759:       base.set_query(rel.query)       if rel.query
760:       base.set_fragment(rel.fragment) if rel.fragment
761: 
762:       return base
763:     end

Args

oth:URI or String

Description

Destructive form of merge

Usage

  require 'uri'

  uri = URI.parse("http://my.example.com")
  uri.merge!("/main.rbx?page=1")
  p uri
  # =>  #<URI::HTTP:0x2021f3b0 URL:http://my.example.com/main.rbx?page=1>

[Source]

     # File uri/generic.rb, line 697
697:     def merge!(oth)
698:       t = merge(oth)
699:       if self == t
700:         nil
701:       else
702:         replace!(t)
703:         self
704:       end
705:     end

Returns normalized URI

[Source]

     # File uri/generic.rb, line 967
967:     def normalize
968:       uri = dup
969:       uri.normalize!
970:       uri
971:     end

Destructive version of normalize

[Source]

     # File uri/generic.rb, line 976
976:     def normalize!
977:       if path && path == ''
978:         set_path('/')
979:       end
980:       if host && host != host.downcase
981:         set_host(self.host.downcase)
982:       end        
983:     end

[Source]

     # File uri/generic.rb, line 551
551:     def opaque=(v)
552:       check_opaque(v)
553:       set_opaque(v)
554:       v
555:     end

[Source]

     # File uri/generic.rb, line 369
369:     def password
370:       @password
371:     end

[Source]

     # File uri/generic.rb, line 315
315:     def password=(password)
316:       check_password(password)
317:       set_password(password)
318:       password
319:     end

[Source]

     # File uri/generic.rb, line 491
491:     def path=(v)
492:       check_path(v)
493:       set_path(v)
494:       v
495:     end

[Source]

     # File uri/generic.rb, line 426
426:     def port=(v)
427:       check_port(v)
428:       set_port(v)
429:       port
430:     end

[Source]

     # File uri/generic.rb, line 522
522:     def query=(v)
523:       check_query(v)
524:       set_query(v)
525:       v
526:     end

[Source]

     # File uri/generic.rb, line 455
455:     def registry=(v)
456:       check_registry(v)
457:       set_registry(v)
458:       v
459:     end

Checks if URI is relative

[Source]

     # File uri/generic.rb, line 606
606:     def relative?
607:       !absolute?
608:     end

Args

oth:URI or String

Description

Calculates relative path from oth to self

Usage

  require 'uri'

  uri = URI.parse('http://my.example.com/main.rbx?page=1')
  p uri.route_from('http://my.example.com')
  #=> #<URI::Generic:0x20218858 URL:/main.rbx?page=1>

[Source]

     # File uri/generic.rb, line 911
911:     def route_from(oth)
912:       # you can modify `rel', but can not `oth'.
913:       begin
914:         oth, rel = route_from0(oth)
915:       rescue
916:         raise $!.class, $!.message
917:       end
918:       if oth == rel
919:         return rel
920:       end
921: 
922:       rel.set_path(route_from_path(oth.path, self.path))
923:       if rel.path == './' && self.query
924:         # "./?foo" -> "?foo"
925:         rel.set_path('')
926:       end
927: 
928:       return rel
929:     end

Args

oth:URI or String

Description

Calculates relative path to oth from self

Usage

  require 'uri'

  uri = URI.parse('http://my.example.com')
  p uri.route_to('http://my.example.com/main.rbx?page=1')
  #=> #<URI::Generic:0x2020c2f6 URL:/main.rbx?page=1>

[Source]

     # File uri/generic.rb, line 951
951:     def route_to(oth)
952:       case oth
953:       when Generic
954:       when String
955:         oth = URI.parse(oth)
956:       else
957:         raise ArgumentError,
958:           "bad argument(expected URI object or URI string)"
959:       end
960: 
961:       oth.route_from(self)
962:     end

[Source]

     # File uri/generic.rb, line 242
242:     def scheme=(v)
243:       check_scheme(v)
244:       set_scheme(v)
245:       v
246:     end

Args

components:Multiple Symbol arguments defined in URI::HTTP

Description

Selects specified components from URI

Usage

  require 'uri'

  uri = URI.parse('http://myuser:mypass@my.example.com/test.rbx')
  p uri.select(:userinfo, :host, :path)
  # => ["myuser:mypass", "my.example.com", "/test.rbx"]

[Source]

      # File uri/generic.rb, line 1092
1092:     def select(*components)
1093:       components.collect do |c|
1094:         if component.include?(c)
1095:           self.send(c)
1096:         else
1097:           raise ArgumentError, 
1098:             "expected of components of #{self.class} (#{self.class.component.join(', ')})"
1099:         end
1100:       end
1101:     end

Constructs String from URI

[Source]

      # File uri/generic.rb, line 997
 997:     def to_s
 998:       str = ''
 999:       if @scheme
1000:         str << @scheme
1001:         str << ':'
1002:       end
1003: 
1004:       if @opaque
1005:         str << @opaque
1006: 
1007:       else
1008:         if @registry
1009:           str << @registry
1010:         else
1011:           if @host
1012:             str << '//'
1013:           end
1014:           if self.userinfo
1015:             str << self.userinfo
1016:             str << '@'
1017:           end
1018:           if @host
1019:             str << @host
1020:           end
1021:           if @port && @port != self.default_port
1022:             str << ':'
1023:             str << @port.to_s
1024:           end
1025:         end
1026: 
1027:         str << path_query
1028:       end
1029: 
1030:       if @fragment
1031:         str << '#'
1032:         str << @fragment
1033:       end
1034: 
1035:       str
1036:     end

[Source]

     # File uri/generic.rb, line 365
365:     def user
366:       @user
367:     end

[Source]

     # File uri/generic.rb, line 309
309:     def user=(user)
310:       check_user(user)
311:       set_user(user)
312:       user
313:     end

[Source]

     # File uri/generic.rb, line 357
357:     def userinfo
358:       if !@password
359:         @user
360:       else
361:         @user + ':' + @password
362:       end
363:     end

Sets userinfo, argument is string like ‘name:pass’

[Source]

     # File uri/generic.rb, line 300
300:     def userinfo=(userinfo)
301:       if userinfo.nil?
302:         return nil
303:       end
304:       check_userinfo(*userinfo)
305:       set_userinfo(*userinfo)
306:       userinfo
307:     end

Protected Instance methods

[Source]

      # File uri/generic.rb, line 1068
1068:     def component_ary
1069:       component.collect do |x|
1070:         self.send(x)
1071:       end
1072:     end

[Source]

     # File uri/generic.rb, line 569
569:     def set_fragment(v)
570:       @fragment = v
571:     end

[Source]

     # File uri/generic.rb, line 388
388:     def set_host(v)
389:       @host = v
390:     end

[Source]

     # File uri/generic.rb, line 546
546:     def set_opaque(v)
547:       @opaque = v
548:     end

[Source]

     # File uri/generic.rb, line 338
338:     def set_password(v)
339:       set_userinfo(@user, v)
340:       v
341:     end

[Source]

     # File uri/generic.rb, line 486
486:     def set_path(v)
487:       @path = v
488:     end

[Source]

     # File uri/generic.rb, line 414
414:     def set_port(v)
415:       unless !v || v.kind_of?(Fixnum)
416:         if v.empty?
417:           v = nil
418:         else
419:           v = v.to_i
420:         end
421:       end
422:       @port = v
423:     end

[Source]

     # File uri/generic.rb, line 517
517:     def set_query(v)
518:       @query = v
519:     end

[Source]

     # File uri/generic.rb, line 450
450:     def set_registry(v)
451:       @registry = v
452:     end

[Source]

     # File uri/generic.rb, line 237
237:     def set_scheme(v)
238:       @scheme = v
239:     end

[Source]

     # File uri/generic.rb, line 332
332:     def set_user(v)
333:       set_userinfo(v, @password)
334:       v
335:     end

[Source]

     # File uri/generic.rb, line 321
321:     def set_userinfo(user, password = nil)
322:       unless password 
323:         user, password = split_userinfo(user)
324:       end
325:       @user     = user
326:       @password = password if password
327: 
328:       [@user, @password]
329:     end

Search

Google

Ruby API Docs

Links