Module: OpenURI

Defined in:
opal/stdlib/open-uri.rb,
opal/stdlib/nodejs/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:

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.

open("http://www.ruby-lang.org/en") {|f| f.each_line {|line| p line} p f.base_uri # 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" => "[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:

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

See OpenURI::OpenRead.open and Kernel#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

Class Method Summary collapse

Class Method Details

.build_response(req, status, status_text) ⇒ Object


130
131
132
133
134
135
136
137
138
139
140
# File 'opal/stdlib/open-uri.rb', line 130

def self.build_response(req, status, status_text)
  buf = Buffer.new
  buf << data(req).pack('c*')
  io = buf.io
  #io.base_uri = uri # TODO: Generate a URI object from the uri String
  io.status = "#{status} #{status_text}"
  io.meta_add_field('content-type', `req.getResponseHeader("Content-Type") || ''`)
  last_modified = `req.getResponseHeader("Last-Modified")`
  io.meta_add_field('last-modified', last_modified) if last_modified
  io
end

.close_io(io) ⇒ Object


110
111
112
113
114
115
116
# File 'opal/stdlib/open-uri.rb', line 110

def self.close_io(io)
  if io.respond_to? :close!
    io.close! # Tempfile
  else
    io.close unless io.closed?
  end
end

.data(req) ⇒ Object


142
143
144
145
146
147
148
149
150
151
152
153
# File 'opal/stdlib/open-uri.rb', line 142

def self.data(req)
  %x{
    var binStr = req.responseText;
    var byteArray = [];
    for (var i = 0, len = binStr.length; i < len; ++i) {
      var c = binStr.charCodeAt(i);
      var byteCode = c & 0xff; // byte at offset i
      byteArray.push(byteCode);
    }
    return byteArray;
  }
end

.open_loop(uri, options) ⇒ Object

:nodoc:


118
119
120
121
122
123
124
125
126
127
128
# File 'opal/stdlib/open-uri.rb', line 118

def self.open_loop(uri, options) # :nodoc:
  req = request(uri)
  data = `req.responseText`
  status = `req.status`
  status_text = `req.statusText && req.statusText.errno ? req.statusText.errno : req.statusText`
  if status == 200 || (status == 0 && data)
    build_response(req, status, status_text)
  else
    raise OpenURI::HTTPError.new("#{status} #{status_text}", '')
  end
end

.open_uri(name, *rest) ⇒ Object

:nodoc:


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'opal/stdlib/open-uri.rb', line 96

def self.open_uri(name, *rest) # :nodoc:
  io = open_loop(name, {})
  io.rewind
  if block_given?
    begin
      yield io
    ensure
      close_io(io)
    end
  else
    io
  end
end

.request(uri) ⇒ Object


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'opal/stdlib/open-uri.rb', line 155

def self.request(uri)
  %x{
    try {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', uri, false);
      // We cannot use xhr.responseType = "arraybuffer" because XMLHttpRequest is used in synchronous mode.
      // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType#Synchronous_XHR_restrictions
      xhr.overrideMimeType('text/plain; charset=x-user-defined');
      xhr.send();
      return xhr;
    } catch (error) {
      #{raise OpenURI::HTTPError.new(`error.message`, '')}
    }
  }
end