open-uri.rb

Path: open-uri.rb
Last Update: Tue May 04 22:57:04 GMT+10:00 2004

open-uri.rb

open-uri.rb is easy-to-use wrapper for net/http and net/ftp.

Example

It is possible to open http/ftp URL as usual a file:

  open("http://www.ruby-lang.org/") {|f|
    f.each_line {|line| p line}
  }

The opened file has several methods for meta information as follows since it is extended by OpenURI::Meta.

  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.

  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 and ftp_proxy are in effect by default. :proxy => nil disables proxy.

  open("http://www.ruby-lang.org/en/raa.html",
    :proxy => nil) {|f|
    ...
  }

URI objects can be opened in 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 <akr@m17n.org>

Required files

uri   stringio   time   tempfile   socket   net/http   net/ftp  

Classes and Modules

Module Kernel
Module OpenURI
  ::Module OpenURI::Meta
  ::Module OpenURI::OpenRead
  ::Class OpenURI::HTTPError
Module URI
  ::Class URI::FTP
  ::Class URI::Generic
  ::Class URI::HTTP

[Validate]