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.cruri.cr
Class Method Summary
-
.escape(string : String, space_to_plus = false) : String
URL-encode a string.
-
.escape(string : String, io : IO, space_to_plus = false)
URL-encode a string and write the result to an
IO
. -
.escape(string : String, space_to_plus = false, &block) : String
URL-encode a string.
-
.escape(string : String, io : IO, space_to_plus = false, &block)
URL-encode a string and write the result to an
IO
. - .new(scheme = nil, host = nil, port = nil, path = nil, query = nil, user = nil, password = nil, fragment = nil, opaque = nil)
-
.parse(raw_url : String) : URI
Parses
raw_url
into an URI. -
.reserved?(byte) : Bool
Returns whether given byte is reserved character defined in RFC 3986.
-
.unescape(string : String, io : IO, plus_to_space = false, &block)
URL-decode a string and write the result to an
IO
. -
.unescape(string : String, plus_to_space = false, &block) : String
URL-decode a string.
-
.unescape(string : String, plus_to_space = false) : String
URL-decode a string.
-
.unescape(string : String, io : IO, plus_to_space = false)
URL-decode a string and write the result to an
IO
. -
.unescape_one(string, bytesize, i, byte, char, io, plus_to_space = false, &block)
:nodoc: Unescapes one character.
-
.unreserved?(byte) : Bool
Returns whether given byte is unreserved character defined in RFC 3986.
Instance Method Summary
- #==(other : self)
-
#fragment : String | Nil
Returns the fragment component of the URI.
-
#fragment=(fragment : String | Nil)
Sets the fragment component of the URI.
-
#full_path
Returns the full path of this URI.
- #hash
-
#host : String | Nil
Returns the host component of the URI.
-
#host=(host : String | Nil)
Sets the host component of the URI.
-
#opaque : String | Nil
Returns the opaque component of the URI.
-
#opaque=(opaque : String | Nil)
Sets the opaque component of the URI.
-
#password : String | Nil
Returns the password component of the URI.
-
#password=(password : String | Nil)
Sets the password component of the URI.
-
#path : String | Nil
Returns the path component of the URI.
-
#path=(path : String | Nil)
Sets the path component of the URI.
-
#port : Int32 | Nil
Returns the port component of the URI.
-
#port=(port : Int32 | Nil)
Sets the port component of the URI.
-
#query : String | Nil
Returns the query component of the URI.
-
#query=(query : String | Nil)
Sets the query component of the URI.
-
#scheme : String | Nil
Returns the scheme component of the URI.
-
#scheme=(scheme : String | Nil)
Sets the scheme component of the URI.
- #to_s(io : IO)
-
#user : String | Nil
Returns the user component of the URI.
-
#user=(user : String | Nil)
Sets the user component of the URI.
-
#userinfo
Returns the user-information component containing the provided username and password.
Instance methods inherited from class Reference
==(other)==(other : self) ==, dup dup, hash hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, same?(other : Reference)
same?(other : Nil) same?, to_s(io : IO) : Nil to_s
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, crystal_type_id crystal_type_id, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml
to_yaml(io : IO) to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
clone
clone,
dup
dup,
from_json(string_or_io) : selffrom_json(string_or_io, root : String) : self from_json, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, nilable? nilable?, to_s(io) to_s, |(other : U.class) forall U |
Class Method Detail
URL-encode a string.
If space_to_plus is true, it replace space character (0x20) to '+' and '+' is
encoded to '%2B'. e.g. application/x-www-form-urlencoded
want this replace.
URI.escape("'Stop!' said Fred") #=> "%27Stop%21%27%20said%20Fred"
URI.escape("'Stop!' said Fred", space_to_plus: true) #=> "%27Stop%21%27+said+Fred"
URL-encode a string and write the result to an IO
.
URL-encode a string.
This method requires block, the block is called with each characters
whose code is less than 0x80
. The characters that block returns
true
are not escaped, other characters are escaped.
# Escape URI path
URI.escape("/foo/file?(1).txt") do |byte|
URI.unreserved?(byte) || byte.chr == '/'
end
#=> "/foo/file%3F%281%29.txt"
URL-encode a string and write the result to an IO
.
This method requires block.
Parses raw_url
into an URI. The raw_url
may be relative or absolute.
require
Returns whether given byte is reserved character defined in RFC 3986.
Reserved characters are ':', '/', '?', '#', '[', ']', '@', '!', '$', '&', "'", '(', ')', '*', '+', ',', ';' and '='.
URL-decode a string and write the result to an IO
.
This method requires block.
URL-decode a string.
This method requires block, the block is called with each bytes
whose is less than 0x80
. The bytes that block returns true
are not unescaped, other characters are unescaped.
URL-decode a string.
If plus_to_space is true, it replace plus character (0x2B) to ' '.
e.g. application/x-www-form-urlencoded
wants this replace.
URI.unescape("%27Stop%21%27%20said%20Fred") #=> "'Stop!' said Fred"
URI.unescape("%27Stop%21%27+said+Fred", plus_to_space: true) #=> "'Stop!' said Fred"
URL-decode a string and write the result to an IO
.
:nodoc: Unescapes one character. Private API
Returns whether given byte is unreserved character defined in RFC 3986.
Unreserved characters are alphabet, digit, '_', '.', '-', '~'.
Instance Method Detail
Returns the fragment component of the URI.
URI.parse("http://foo.com/bar#section1").fragment # => "section1"
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"
Returns the host component of the URI.
URI.parse("http://foo.com").host # => "foo.com"
Returns the opaque component of the URI.
URI.parse("mailto:alice@example.com").opaque # => "alice@example.com"
Returns the password component of the URI.
URI.parse("http://admin:password@foo.com").password # => "password"
Returns the path component of the URI.
URI.parse("http://foo.com/bar").path # => "/bar"
Returns the port component of the URI.
URI.parse("http://foo.com:5432").port # => 5432
Returns the query component of the URI.
URI.parse("http://foo.com/bar?q=1").query # => "q=1"
Returns the scheme component of the URI.
URI.parse("http://foo.com").scheme # => "http"
URI.parse("mailto:alice@example.com").scheme # => "mailto"
Returns the user component of the URI.
URI.parse("http://admin:password@foo.com").user # => "admin"
Returns the user-information component containing the provided username and password.
uri = URI.parse "http://admin:password@foo.com"
uri.userinfo # => "admin:password"