Base class for all URI classes. Implements generic URI syntax as per RFC 2396.

Constants

An Array of the available components for URI::Generic

A Default port of nil for URI::Generic

No documentation available
Attributes

returns the fragment component of the URI.

URI("http://foo/bar/baz?search=FooBar#ponies").fragment #=> "ponies"
Read

returns the host component of the URI.

URI("http://foo/bar/baz").host #=> "foo"

It returns nil if no host component.

URI("mailto:foo@example.org").host #=> nil

The component doesn't contains the port number.

URI("http://foo:8080/bar/baz").host #=> "foo"

Since IPv6 addresses are wrapped by brackets in URIs, this method returns IPv6 addresses wrapped by brackets. This form is not appropriate to pass socket methods such as TCPSocket.open. If unwrapped host names are required, use “hostname” method.

URI("http://[::1]/bar/baz").host #=> "[::1]"
URI("http://[::1]/bar/baz").hostname #=> "::1"
Read

returns the opaque part of the URI.

URI("mailto:foo@example.org").opaque #=> "foo@example.org"

Portion of the path that does make use of the slash '/'. The path typically refers to the absolute path and the opaque part. (See RFC2396 Section 3 and 5.2.)

Read

returns the path component of the URI.

URI("http://foo/bar/baz").path #=> "/bar/baz"
Read

returns the port component of the URI.

URI("http://foo/bar/baz").port #=> "80"

URI("http://foo:8080/bar/baz").port #=> "8080"
Read

returns the query component of the URI.

URI("http://foo/bar/baz?search=FooBar").query #=> "search=FooBar"
Read

returns the scheme component of the URI.

URI("http://foo/bar/baz").scheme #=> "http"
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.

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.

Components of the URI in the order.

Returns default port

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

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.

Instance Methods
An alias for merge
An alias for route_from

Compares two URIs

An alias for absolute?

Checks if URI is an absolute one

check the host v component for RFC2396 compliance and against the URI::Parser Regexp for :HOST

Can not have a registry or opaque component defined, with a host component defined.

check the opaque v component for RFC2396 compliance and against the URI::Parser Regexp for :OPAQUE

Can not have a host, port, user or path component defined, with an opaque component defined.

check the password v component for RFC2396 compliance and against the URI::Parser Regexp for :USERINFO

Can not have a registry or opaque component defined, with a user component defined.

check the path v component for RFC2396 compliance and against the URI::Parser Regexp for :ABS_PATH and :REL_PATH

Can not have a opaque component defined, with a path component defined.

check the port v component for RFC2396 compliance and against the URI::Parser Regexp for :PORT

Can not have a registry or opaque component defined, with a port component defined.

check the scheme v component against the URI::Parser Regexp for :SCHEME

check the user v component for RFC2396 compliance and against the URI::Parser Regexp for :USERINFO

Can not have a registry or opaque component defined, with a user component defined.

check 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

Args

v

URI or String

Description

attempts to parse other URI oth, returns [parsed_oth, self]

Usage

require 'uri'

uri = URI.parse("http://my.example.com")
uri.coerce("http://foo.com")
#=> [#<URI::HTTP:0x00000000bcb028 URL:http://foo.com/>, #<URI::HTTP:0x00000000d92178 URL:http://my.example.com>]

Components of the URI in the order.

returns an Array of the components defined from the COMPONENT Array

Returns default port

No documentation available

escapes 'user:password' v based on RFC 1738 section 3.1

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.

check the fragment v component against the URI::Parser Regexp for :FRAGMENT

Args

v

String

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"
# =>  "time=1305212086"
uri
#=> #<URI::HTTP:0x000000007a81f8 URL:http://my.example.com/?id=25#time=1305212086>
No documentation available

Checks if URI has a path

Args

v

String

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"
# =>  "foo.com"
uri
#=> #<URI::HTTP:0x000000008e89e8 URL:http://foo.com>

extract the host part of the URI and unwrap brackets for IPv6 addresses.

This method is same as URI::Generic#host except brackets for IPv6 (and future IP) addresses are removed.

u = URI("http://[::1]/bar")
p u.hostname      #=> "::1"
p u.host          #=> "[::1]"

set the host part of the URI as the argument with brackets for IPv6 addresses.

This method is same as URI::Generic#host= except the argument can be bare IPv6 address.

u = URI("http://foo/bar")
p u.to_s                  #=> "http://foo/bar"
u.hostname = "::1"
p u.to_s                  #=> "http://[::1]/bar"

If the argument seems IPv6 address, it is wrapped by brackets.

No documentation available

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>

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>

Merges a base path base, with relative path rel, returns a modified base path.

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 “/”.

Destructive version of normalize

Args

v

String

Description

public setter for the opaque component v. (with validation)

see also URI::Generic.check_opaque

returns the parser to be used.

Unless a URI::Parser is defined, then DEFAULT_PARSER is used.

returns the password component

Args

v

String

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"
# =>  "V3ry_S3nsit1ve"
uri
#=> #<URI::HTTP:0x00000000881d90 URL:http://john:V3ry_S3nsit1ve@my.example.com>

Args

v

String

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/"
# =>  "/faq/"
uri
#=> #<URI::HTTP:0x000000008e89e8 URL:http://my.example.com/faq/>

Args

v

String

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
# =>  8080
uri
#=> #<URI::HTTP:0x000000008e89e8 URL:http://my.example.com:8080>

Args

v

String

Description

public setter for the query component v.

Usage

require 'uri'

uri = URI.parse("http://my.example.com/?id=25")
uri.query = "id=1"
# =>  "id=1"
uri
#=> #<URI::HTTP:0x000000008e89e8 URL:http://my.example.com/?id=1>
No documentation available

Checks if URI is relative

replace self by other URI object

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>

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>

Args

v

String

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"
# =>  "https"
uri
#=> #<URI::HTTP:0x000000008e89e8 URL:https://my.example.com>

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"]

protected setter for the host component v

see also URI::Generic.host=

protected setter for the opaque component v

see also URI::Generic.opaque=

protected setter for the password component v

see also URI::Generic.password=

protected setter for the path component v

see also URI::Generic.path=

protected setter for the port component v

see also URI::Generic.port=

protected setter for the scheme component v

see also URI::Generic.scheme=

protected setter for the user component v

see also URI::Generic.user=

protect setter for the user component, and password if available. (with validation)

see also URI::Generic.userinfo=

returns an Array of the path split on '/'

returns the userinfo ui as user, password if properly formatted as 'user:password'

Constructs String from URI

returns the user component

Args

v

String

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"
# =>  "sam"
uri
#=> #<URI::HTTP:0x00000000881d90 URL:http://sam:V3ry_S3nsit1ve@my.example.com>

returns the userinfo, either as 'user' or 'user:password'

Sets userinfo, argument is string like 'name:pass'