class URI

Overview

This class represents a URI reference as defined by [RFC 3986: Uniform Resource Identifier (URI): Generic Syntax](https://www.ietf.org/rfc/rfc3986.txt).

This class provides constructors for creating URI instances from their components or by parsing their string forms and methods for accessing the various components of an instance.

Basic example:

require "uri"

uri = URI.parse "http://foo.com/posts?id=30&limit=5#time=1305298413"
# => #<URI:0x1003f1e40 @scheme="http", @host="foo.com", @port=nil, @path="/posts", @query="id=30&limit=5", ... >
uri.scheme
# => "http"
uri.host
# => "foo.com"
uri.query
# => "id=30&limit=5"
uri.to_s
# => "http://foo.com/posts?id=30&limit=5#time=1305298413"

Defined in:

uri/uri_parser.cr
uri.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from class Reference

! !, ==(other)
==(other : self)
==
, hash hash, inspect(io : IO) : Nil inspect, nil? nil?, object_id object_id, same?(other : Nil)
same?(other : Reference)
same?
, to_s(io : IO) : Nil to_s

Instance methods inherited from class Object

! !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try

Class methods inherited from class Object

==(other : Class) ==, ===(other) ===, cast(other) : self cast, from_json(string_or_io) from_json, from_yaml(string : String) from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s

Class Method Detail

def self.escape(string : String, io : IO) #

URL-encode a string and write the result to an IO.


[View source]
def self.escape(string : String) #

URL-encode a string.

URI.escape("'Stop!' said Fred") #=> "%27Stop%21%27+said+Fred"

[View source]
def self.parse(raw_url : String) #

Parses raw_url into an URI. The raw_url may be relative or absolute.

require 

[View source]
def self.unescape(string : String) #

URL-decode a string.

URI.unescape("%27Stop%21%27+said+Fred") #=> "'Stop!' said Fred"

[View source]
def self.unescape(string : String, io : IO) #

URL-decode a string and write the result to an IO.


[View source]
def self.unescape_one(string, bytesize, i, byte, char, io) #

:nodoc: Unescapes one character. Private API


[View source]
def self.new(scheme = nil, host = nil, port = nil, path = nil, query = nil, user = nil, password = nil, fragment = nil, opaque = nil) #

[View source]

Instance Method Detail

def fragment #

Returns the fragment component of the URI.

URI.parse("http://foo.com/bar#section1").fragment # => "section1"

[View source]
def fragment=(fragment) #

Sets the fragment component of the URI.


[View source]
def full_path #

Returns the full path of this URI.

uri = URI.parse "http://foo.com/posts?id=30&limit=5#time=1305298413"
uri.full_path # => "/posts?id=30&limit=5"

[View source]
def host #

Returns the host component of the URI.

URI.parse("http://foo.com").host # => "foo.com"

[View source]
def host=(host) #

Sets the host component of the URI.


[View source]
def opaque #

Returns the opaque component of the URI.

URI.parse("mailto:alice@example.com").opaque # => "alice@example.com"

[View source]
def opaque=(opaque) #

Sets the opaque component of the URI.


[View source]
def password #

Returns the password component of the URI.

URI.parse("http://admin:password@foo.com").password # => "password"

[View source]
def password=(password) #

Sets the password component of the URI.


[View source]
def path #

Returns the path component of the URI.

URI.parse("http://foo.com/bar").path # => "/bar"

[View source]
def path=(path) #

Sets the path component of the URI.


[View source]
def port #

Returns the port component of the URI.

URI.parse("http://foo.com:5432").port # => 5432

[View source]
def port=(port) #

Sets the port component of the URI.


[View source]
def query #

Returns the query component of the URI.

URI.parse("http://foo.com/bar?q=1").query # => "q=1"

[View source]
def query=(query) #

Sets the query component of the URI.


[View source]
def scheme #

Returns the scheme component of the URI.

URI.parse("http://foo.com").scheme           # => "http"
URI.parse("mailto:alice@example.com").scheme # => "mailto"

[View source]
def scheme=(scheme) #

Sets the scheme component of the URI.


[View source]
def to_s(io : IO) #

[View source]
def user #

Returns the user component of the URI.

URI.parse("http://admin:password@foo.com").user # => "admin"

[View source]
def user=(user) #

Sets the user component of the URI.


[View source]
def userinfo #

Returns the user-information component containing the provided username and password.

uri = URI.parse "http://admin:password@foo.com"
uri.userinfo # => "admin:password"

[View source]