class URI::Generic
Base class for all URI classes. Implements generic URI syntax as per RFC 2396.
Constants
- COMPONENT
-
An
Arrayof the available components forURI::Generic. - DEFAULT_PORT
-
A Default port of nil for
URI::Generic. - USE_REGISTRY
Attributes
Returns the fragment component of the URI.
URI("http://foo/bar/baz?search=FooBar#ponies").fragment #=> "ponies"
Returns the host component of the URI.
URI("http://foo/bar/baz").host #=> "foo"
It returns nil if no host component exists.
URI("mailto:foo@example.org").host #=> nil
The component does not contain the port number.
URI("http://foo:8080/bar/baz").host #=> "foo"
Since IPv6 addresses are wrapped with brackets in URIs, this method returns IPv6 addresses wrapped with brackets. This form is not appropriate to pass to socket methods such as TCPSocket.open. If unwrapped host names are required, use the hostname method.
URI("http://[::1]/bar/baz").host #=> "[::1]" URI("http://[::1]/bar/baz").hostname #=> "::1"
Returns the opaque part of the URI.
URI("mailto:foo@example.org").opaque #=> "foo@example.org" URI("http://foo/bar/baz").opaque #=> nil
The portion of the path that does not make use of the slash ‘/’. The path typically refers to an absolute path or an opaque part. (See RFC2396 Section 3 and 5.2.)
Returns the path component of the URI.
URI("http://foo/bar/baz").path #=> "/bar/baz"
Returns the port component of the URI.
URI("http://foo/bar/baz").port #=> 80 URI("http://foo:8080/bar/baz").port #=> 8080
Returns the query component of the URI.
URI("http://foo/bar/baz?search=FooBar").query #=> "search=FooBar"
Returns the scheme component of the URI.
URI("http://foo/bar/baz").scheme #=> "http"
Public Class Methods
(Array[nil | String | Integer]) → URI::Generic
({ scheme: String?, userinfo: String?, host: String?, port: Integer?, registry: String?, path: String?, opaque: String?, query: String?, fragment: String? }) → instance
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 168
def self.build: (Array[nil | String | Integer]) -> URI::Generic
| ({ scheme: String?, userinfo: String?, host: String?, port: Integer?, registry: String?, path: String?, opaque: String?, query: String?, fragment: String? }) -> instance
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.
(Array[nil | String | Integer]) → URI::Generic
({ scheme: String?, userinfo: String?, host: String?, port: Integer?, registry: String?, path: String?, opaque: String?, query: String?, fragment: String? }) → instance
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 150
def self.build2: (Array[nil | String | Integer]) -> URI::Generic
| ({ scheme: String?, userinfo: String?, host: String?, port: Integer?, registry: String?, path: String?, opaque: String?, query: String?, fragment: String? }) -> instance
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 does URI::RFC2396_PARSER.escape all URI components and tries again.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 130
def self.component: () -> Array[Symbol]
Components of the URI in the order.
() → (nil | Integer)
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 109
def self.default_port: () -> (nil | Integer)
Returns default port.
(String? scheme, String? userinfo, String? host, (String | Integer)? port, nil registry, String? path, String? opaque, String? query, String? fragment, ?untyped parser, ?boolish arg_check) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 215
def initialize: (String? scheme, String? userinfo, String? host, (String | Integer)? port, nil registry, String? path, String? opaque, String? query, String? fragment, ?untyped parser, ?boolish arg_check) -> void
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-
Registry of naming authorities.
path-
Path on server.
opaque-
Opaque part.
query-
Query data.
fragment-
Part of the
URIafter ‘#’ character. 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.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1114
def self.use_proxy?: (String hostname, String addr, Integer port, String no_proxy) -> bool
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 134
def self.use_registry: () -> bool
Public Instance Methods
(URI::Generic oth) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1013
def ==: (URI::Generic oth) -> bool
Compares two URIs.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 836
def absolute?: () -> bool
Returns true if URI has a scheme (e.g. http:// or https://) specified.
(String? v) → true?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 539
def check_host: (String? v) -> true?
Checks the host v component for RFC2396 compliance and against the parser Regexp for :HOST.
Can not have a registry or opaque component defined, with a host component defined.
(String? v) → true?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 750
def check_opaque: (String? v) -> true?
Checks the opaque v component for RFC2396 compliance and against the parser Regexp for :OPAQUE.
Can not have a host, port, user, or path component defined, with an opaque component defined.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 395
def check_password: (String? v, ?String user) -> (String? | true)
Checks the password v component for RFC2396 compliance and against the parser Regexp for :USERINFO.
Can not have a registry or opaque component defined, with a user component defined.
(String? v) → true
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 678
def check_path: (String? v) -> true
Checks the path v component for RFC2396 compliance and against the parser Regexp for :ABS_PATH and :REL_PATH.
Can not have a opaque component defined, with a path component defined.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 620
def check_port: ((String | Integer)? v) -> true?
Checks the port v component for RFC2396 compliance and against the parser Regexp for :PORT.
Can not have a registry or opaque component defined, with a port component defined.
(String v) → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 658
def check_registry: (String v) -> nil
(String? v) → true
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 322
def check_scheme: (String? v) -> true
Checks the scheme v component against the parser Regexp for :SCHEME.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 383
def check_user: (String v) -> (String | true)
Checks the user v component for RFC2396 compliance and against the parser Regexp for :USERINFO.
Can not have a registry or opaque component defined, with a user component defined.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 371
def check_userinfo: (String user, ?String? password) -> true
Checks the user and password.
If password is not provided, then user is split, using URI::Generic.split_userinfo, to pull user and +password.
See also URI::Generic.check_user, URI::Generic.check_password.
(URI::Generic | String oth) → Array[URI::Generic]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1092
def coerce: (URI::Generic | String oth) -> Array[URI::Generic]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 314
def component: () -> Array[Symbol]
Components of the URI in the order.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1037
def component_ary: () -> Array[nil | String | Integer]
() → (nil | Integer)
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 117
def default_port: () -> (nil | Integer)
Returns default port.
(URI::Generic oth) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1029
def eql?: (URI::Generic oth) -> bool
Compares with oth for Hash.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 503
def escape_userpass: (String v) -> String
Escapes ‘user:password’ v based on RFC 1738 section 3.1.
(?String env) → (nil | URI::Generic)
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1112
def find_proxy: (?String env) -> (nil | URI::Generic)
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.
If the optional parameter env is specified, it is used instead of ENV.
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.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 804
def fragment=: (String? v) -> String?
Checks the fragment v component against the parser Regexp for :FRAGMENT.
Args
v
Description
Public setter for the fragment component v (with validation).
Usage
require 'uri' uri = URI.parse("http://my.example.com/?id=25#time=1305212049") uri.fragment = "time=1305212086" uri.to_s #=> "http://my.example.com/?id=25#time=1305212086"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1021
def hash: () -> Integer
Returns the hash value.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 828
def hierarchical?: () -> bool
Returns true if URI is hierarchical.
Description
URI has components listed in order of decreasing significance from left to right, see RFC3986 www.rfc-editor.org/rfc/rfc3986 1.2.3.
Usage
require 'uri' uri = URI.parse("http://my.example.com/") uri.hierarchical? #=> true uri = URI.parse("mailto:joe@example.com") uri.hierarchical? #=> false
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 575
def host=: (String? v) -> String?
Args
v
Description
Public setter for the host component v (with validation).
See also URI::Generic.check_host.
Usage
require 'uri' uri = URI.parse("http://my.example.com") uri.host = "foo.com" uri.to_s #=> "http://foo.com"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 590
def hostname: () -> String?
Extract the host part of the URI and unwrap brackets for IPv6 addresses.
This method is the same as URI::Generic#host except brackets for IPv6 (and future IP) addresses are removed.
uri = URI("http://[::1]/bar") uri.hostname #=> "::1" uri.host #=> "[::1]"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 608
def hostname=: (String? v) -> String?
Sets the host part of the URI as the argument with brackets for IPv6 addresses.
This method is the same as URI::Generic#host= except the argument can be a bare IPv6 address.
uri = URI("http://foo/bar") uri.hostname = "::1" uri.to_s #=> "http://[::1]/bar"
If the argument seems to be an IPv6 address, it is wrapped with brackets.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1068
def inspect: () -> String
(URI::Generic | string oth) → URI::Generic
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 910
def merge: (URI::Generic | string oth) -> URI::Generic
Args
Description
Merges two URIs.
Usage
require 'uri' uri = URI.parse("http://my.example.com") uri.merge("/main.rbx?page=1") # => "http://my.example.com/main.rbx?page=1"
(URI::Generic | string oth) → String
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 886
def merge!: (URI::Generic | string oth) -> String
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 862
def merge_path: (String base, String rel) -> String
Merges a base path base, with relative path rel, returns a modified base path.
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 989
def normalize: () -> untyped
Returns normalized URI.
require 'uri' URI("HTTP://my.EXAMPLE.com").normalize #=> #<URI::HTTP http://my.example.com/>
Normalization here means:
-
scheme and host are converted to lowercase,
-
an empty path component is set to “/”.
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 997
def normalize!: () -> untyped
Destructive version of normalize.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 778
def opaque=: (String? v) -> String?
Args
v
Description
Public setter for the opaque component v (with validation).
See also URI::Generic.check_opaque.
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 298
def parser: () -> untyped
Returns the parser to be used.
Unless the parser is defined, DEFAULT_PARSER is used.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 527
def password: () -> String?
Returns the password component (without URI decoding).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 455
def password=: (String? password) -> String?
Args
v
Description
Public setter for the password component (with validation).
See also URI::Generic.check_password.
Usage
require 'uri' uri = URI.parse("http://john:S3nsit1ve@my.example.com") uri.password = "V3ry_S3nsit1ve" uri.to_s #=> "http://john:V3ry_S3nsit1ve@my.example.com"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 714
def path=: (String? v) -> String?
Args
v
Description
Public setter for the path component v (with validation).
See also URI::Generic.check_path.
Usage
require 'uri' uri = URI.parse("http://my.example.com/pub/files") uri.path = "/faq/" uri.to_s #=> "http://my.example.com/faq/"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 656
def port=: ((String | Integer)? v) -> (String | Integer)?
Args
v
Description
Public setter for the port component v (with validation).
See also URI::Generic.check_port.
Usage
require 'uri' uri = URI.parse("http://my.example.com") uri.port = 8080 uri.to_s #=> "http://my.example.com:8080"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 738
def query=: (String? v) -> String?
Args
v
Description
Public setter for the query component v.
Usage
require 'uri' uri = URI.parse("http://my.example.com/?id=25") uri.query = "id=1" uri.to_s #=> "http://my.example.com/?id=1"
() → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 255
def registry: () -> nil
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 667
def registry=: (String v) -> nil
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 845
def relative?: () -> bool
Returns true if URI does not have a scheme (e.g. http:// or https://) specified.
(URI::Generic oth) → URI::Generic
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 306
def replace!: (URI::Generic oth) -> URI::Generic
Replaces self by other URI object.
(String oth) → URI::Generic
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 947
def route_from: (String oth) -> URI::Generic
Args
Description
Calculates relative path from oth to self.
Usage
require 'uri' uri = URI.parse('http://my.example.com/main.rbx?page=1') uri.route_from('http://my.example.com') #=> #<URI::Generic /main.rbx?page=1>
(String oth) → Array[URI::Generic]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 923
def route_from0: (String oth) -> Array[URI::Generic]
:stopdoc:
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 920
def route_from_path: (String src, String dst) -> String
:stopdoc:
(String oth) → URI::Generic
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 971
def route_to: (String oth) -> URI::Generic
Args
Description
Calculates relative path to oth from self.
Usage
require 'uri' uri = URI.parse('http://my.example.com') uri.route_to('http://my.example.com/main.rbx?page=1') #=> #<URI::Generic /main.rbx?page=1>
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 358
def scheme=: (String? v) -> String?
Args
v
Description
Public setter for the scheme component v (with validation).
See also URI::Generic.check_scheme.
Usage
require 'uri' uri = URI.parse("http://my.example.com") uri.scheme = "https" uri.to_s #=> "https://my.example.com"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1061
def select: (*Symbol components) -> Array[nil | String | Integer]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 549
def set_host: (String? v) -> String?
Protected setter for the host component v.
See also URI::Generic.host=.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 760
def set_opaque: (String? v) -> String?
Protected setter for the opaque component v.
See also URI::Generic.opaque=.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 486
def set_password: (String? v) -> String?
Protected setter for the password component v.
See also URI::Generic.password=.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 688
def set_path: (String? v) -> String?
Protected setter for the path component v.
See also URI::Generic.path=.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 630
def set_port: ((String | Integer)? v) -> (String | Integer)?
Protected setter for the port component v.
See also URI::Generic.port=.
(String v) → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 660
def set_registry: (String v) -> nil
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 332
def set_scheme: (String? v) -> String?
Protected setter for the scheme component v.
See also URI::Generic.scheme=.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 476
def set_user: (String? v) -> String?
Protected setter for the user component v.
See also URI::Generic.user=.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 466
def set_userinfo: (String? user, ?String? password) -> [ String?, String? ]
Protected setter for the user component, and password if available (with validation).
See also URI::Generic.userinfo=.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 853
def split_path: (String path) -> Array[String]
Returns an Array of the path split on ‘/’.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 495
def split_userinfo: (String ui) -> Array[String | nil]
Returns the userinfo ui as [user, password] if properly formatted as ‘user:password’.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 1005
def to_s: () -> String
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 519
def user: () -> String?
Returns the user component (without URI decoding).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 429
def user=: (String? user) -> String?
Args
v
Description
Public setter for the user component (with validation).
See also URI::Generic.check_user.
Usage
require 'uri' uri = URI.parse("http://john:S3nsit1ve@my.example.com") uri.user = "sam" uri.to_s #=> "http://sam@my.example.com"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/uri/0/generic.rbs, line 511
def userinfo: () -> String?
Returns the userinfo, either as ‘user’ or ‘user:password’.