Module: OpenURI

Defined in:
lib/open-uri.rb

Overview

OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP.

Example

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" => "[email protected]",
"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
Author

Tanaka Akira [email protected]

Defined Under Namespace

Modules: Meta, OpenRead Classes: Buffer, HTTPError, HTTPRedirect, TooManyRedirects

Constant Summary collapse

VERSION =
"0.5.0"
Options =
{
  :proxy => true,
  :proxy_http_basic_authentication => true,
  :progress_proc => true,
  :content_length_proc => true,
  :http_basic_authentication => true,
  :read_timeout => true,
  :open_timeout => true,
  :ssl_ca_cert => nil,
  :ssl_verify_mode => nil,
  :ssl_min_version => nil,
  :ssl_max_version => nil,
  :ftp_active_mode => false,
  :redirect => true,
  :encoding => nil,
  :max_redirects => 64,
  :request_specific_fields => nil,
}
RE_LWS =

:stopdoc:

/[\r\n\t ]+/n
RE_TOKEN =
%r{[^\x00- ()<>@,;:\\"/\[\]?={}\x7f]+}n
RE_QUOTED_STRING =
%r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])*"}n
RE_PARAMETERS =
%r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n