Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • open-uri.rb


Class/Module Index [+]



Mixin for HTTP and FTP URIs.

Public Instance Methods

open(*rest, &block) click to toggle source

#open provides `open' for URI::HTTP and URI::FTP.

#open takes optional 3 arguments as: #open([mode [, perm]] [, options]) [{|io| ... }]

`mode’, `perm’ is same as Kernel#open.

However, `mode’ must be read mode because #open doesn’t support write mode (yet). Also `perm’ is just ignored because it is meaningful only for file creation.

`options’ must be a hash.

Each pairs which key is a string in the hash specify a extra header field for HTTP. I.e. it is ignored for FTP without HTTP proxy.

The hash may include other options which key is a symbol:



:proxy => "http://proxy.foo.com:8000/"
:proxy => URI.parse("http://proxy.foo.com:8000/")
:proxy => true
:proxy => false
:proxy => nil

If :proxy option is specified, the value should be String, URI, boolean or nil. When String or URI is given, it is treated as proxy URI. When true is given or the option itself is not specified, environment variable `scheme_proxy’ is examined. `scheme’ is replaced by `http’, `https’ or `ftp’. When false or nil is given, the environment variables are ignored and connection will be made to a server directly.



:http_basic_authentication=>[user, password]

If :http_basic_authentication is specified, the value should be an array which contains 2 strings: username and password. It is used for HTTP Basic authentication defined by RFC 2617.



:content_length_proc => lambda {|content_length| ... }

If :content_length_proc option is specified, the option value procedure is called before actual transfer is started. It takes one argument which is expected content length in bytes.

If two or more transfer is done by HTTP redirection, the procedure is called only one for a last transfer.

When expected content length is unknown, the procedure is called with nil. It is happen when HTTP response has no Content-Length header.



:progress_proc => lambda {|size| ...}

If :progress_proc option is specified, the proc is called with one argument each time when `open’ gets content fragment from network. The argument `size’ `size’ is a accumulated transfered size in bytes.

If two or more transfer is done by HTTP redirection, the procedure is called only one for a last transfer.

:progress_proc and :content_length_proc are intended to be used for progress bar. For example, it can be implemented as follows using Ruby/ProgressBar.

pbar = nil
  :content_length_proc => lambda {|t|
    if t && 0 < t
      pbar = ProgressBar.new("...", t)
  :progress_proc => lambda {|s|
    pbar.set s if pbar
  }) {|f| ... }

#open returns an IO like object if block is not given. Otherwise it yields the IO object and return the value of the block. The IO object is extended with OpenURI::Meta.

               # File open-uri.rb, line 517
def open(*rest, &block)
  OpenURI.open_uri(self, *rest, &block)
read(options={}) click to toggle source

#read) reads a content referenced by self and returns the content as string. The string is extended with OpenURI::Meta. The argument `options' is same as #open.

               # File open-uri.rb, line 525
def read(options={})
  self.open(options) {|f|
    str = f.read
    Meta.init str, f