Setter for to v
.
Setter for headers v
.
Creates a new URI::WS
object from components, with syntax checking.
The components accepted are userinfo, host, port, path, and query.
The components should be provided either as an Array
, or as a Hash
with keys formed by preceding the component names with a colon.
If an Array
is used, the components must be passed in the order [userinfo, host, port, path, query]
.
Example:
uri = URI::WS.build(host: 'www.example.com', path: '/foo/bar') uri = URI::WS.build([nil, "www.example.com", nil, "/path", "query"])
Currently, if passed userinfo components this method generates invalid WS
URIs as per RFC 1738.
Returns a URL-encoded string derived from the given Enumerable enum
.
The result is suitable for use as form data for an HTTP request whose Content-Type
is 'application/x-www-form-urlencoded'
.
The returned string consists of the elements of enum
, each converted to one or more URL-encoded strings, and all joined with character '&'
.
Simple examples:
URI.encode_www_form([['foo', 0], ['bar', 1], ['baz', 2]]) # => "foo=0&bar=1&baz=2" URI.encode_www_form({foo: 0, bar: 1, baz: 2}) # => "foo=0&bar=1&baz=2"
The returned string is formed using method URI.encode_www_form_component
, which converts certain characters:
URI.encode_www_form('f#o': '/', 'b-r': '$', 'b z': '@') # => "f%23o=%2F&b-r=%24&b+z=%40"
When enum
is Array-like, each element ele
is converted to a field:
If ele
is an array of two or more elements, the field is formed from its first two elements (and any additional elements are ignored):
name = URI.encode_www_form_component(ele[0], enc) value = URI.encode_www_form_component(ele[1], enc) "#{name}=#{value}"
Examples:
URI.encode_www_form([%w[foo bar], %w[baz bat bah]]) # => "foo=bar&baz=bat" URI.encode_www_form([['foo', 0], ['bar', :baz, 'bat']]) # => "foo=0&bar=baz"
If ele
is an array of one element, the field is formed from ele[0]
:
URI.encode_www_form_component(ele[0])
Example:
URI.encode_www_form([['foo'], [:bar], [0]]) # => "foo&bar&0"
Otherwise the field is formed from ele
:
URI.encode_www_form_component(ele)
Example:
URI.encode_www_form(['foo', :bar, 0]) # => "foo&bar&0"
The elements of an Array-like enum
may be mixture:
URI.encode_www_form([['foo', 0], ['bar', 1, 2], ['baz'], :bat]) # => "foo=0&bar=1&baz&bat"
When enum
is Hash-like, each key
/value
pair is converted to one or more fields:
If value
is Array-convertible, each element ele
in value
is paired with key
to form a field:
name = URI.encode_www_form_component(key, enc) value = URI.encode_www_form_component(ele, enc) "#{name}=#{value}"
Example:
URI.encode_www_form({foo: [:bar, 1], baz: [:bat, :bam, 2]}) # => "foo=bar&foo=1&baz=bat&baz=bam&baz=2"
Otherwise, key
and value
are paired to form a field:
name = URI.encode_www_form_component(key, enc) value = URI.encode_www_form_component(value, enc) "#{name}=#{value}"
Example:
URI.encode_www_form({foo: 0, bar: 1, baz: 2}) # => "foo=0&bar=1&baz=2"
The elements of a Hash-like enum
may be mixture:
URI.encode_www_form({foo: [0, 1], bar: 2}) # => "foo=0&foo=1&bar=2"
Returns name/value pairs derived from the given string str
, which must be an ASCII string.
The method may be used to decode the body of Net::HTTPResponse
object res
for which res['Content-Type']
is 'application/x-www-form-urlencoded'
.
The returned data is an array of 2-element subarrays; each subarray is a name/value pair (both are strings). Each returned string has encoding enc
, and has had invalid characters removed via String#scrub
.
A simple example:
URI.decode_www_form('foo=0&bar=1&baz') # => [["foo", "0"], ["bar", "1"], ["baz", ""]]
The returned strings have certain conversions, similar to those performed in URI.decode_www_form_component
:
URI.decode_www_form('f%23o=%2F&b-r=%24&b+z=%40') # => [["f#o", "/"], ["b-r", "$"], ["b z", "@"]]
The given string may contain consecutive separators:
URI.decode_www_form('foo=0&&bar=1&&baz=2') # => [["foo", "0"], ["", ""], ["bar", "1"], ["", ""], ["baz", "2"]]
A different separator may be specified:
URI.decode_www_form('foo=0--bar=1--baz', separator: '--') # => [["foo", "0"], ["bar", "1"], ["baz", ""]]
No longer used by internal code.
OpenURI
is an easy-to-use wrapper for Net::HTTP
, Net::HTTPS and Net::FTP.
It is possible to open an http, https or ftp URL as though it were a file:
URI.open("http://www.ruby-lang.org/") {|f| f.each_line {|line| p line} }
The opened file has several getter methods for its meta-information, as follows, since it is extended by OpenURI::Meta
.
URI.open("http://www.ruby-lang.org/en") {|f| f.each_line {|line| p line} p f.base_uri # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/> p f.content_type # "text/html" p f.charset # "iso-8859-1" p f.content_encoding # [] p f.last_modified # Thu Dec 05 02:45:02 UTC 2002 }
Additional header fields can be specified by an optional hash argument.
URI.open("http://www.ruby-lang.org/en/", "User-Agent" => "Ruby/#{RUBY_VERSION}", "From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/") {|f| # ... }
The environment variables such as http_proxy, https_proxy and ftp_proxy are in effect by default. Here we disable proxy:
URI.open("http://www.ruby-lang.org/en/", :proxy => nil) {|f| # ... }
See OpenURI::OpenRead.open
and URI.open
for more on available options.
URI
objects can be opened in a similar way.
uri = URI.parse("http://www.ruby-lang.org/en/") uri.open {|f| # ... }
URI
objects can be read directly. The returned string is also extended by OpenURI::Meta
.
str = uri.read p str.base_uri
Tanaka Akira <akr@m17n.org>
Validates typecode v
, returns true
or false
.
Private setter for the path of the URI::FTP
.
do nothing
do nothing
do nothing
Returns default port.
Returns default port.
Checks the scheme v
component against the URI::Parser Regexp
for :SCHEME.