Module: OpenURI
- Defined in:
- opal/stdlib/open-uri.rb,
opal/stdlib/nodejs/open-uri.rb
Overview
backtick_javascript: true
Defined Under Namespace
Modules: Meta, OpenRead Classes: Buffer, HTTPError
Class Method Summary collapse
- .build_response(req, status, status_text) ⇒ Object
- .close_io(io) ⇒ Object
- .data(req) ⇒ Object
-
.open_loop(uri, options) ⇒ Object
:nodoc:.
-
.open_uri(name, *rest) ⇒ Object
:nodoc:.
- .request(uri) ⇒ Object
Class Method Details
.build_response(req, status, status_text) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'opal/stdlib/open-uri.rb', line 133 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.('content-type', `req.getResponseHeader("Content-Type") || ''`) last_modified = `req.getResponseHeader("Last-Modified")` io.('last-modified', last_modified) if last_modified io end |
.close_io(io) ⇒ Object
113 114 115 116 117 118 119 |
# File 'opal/stdlib/open-uri.rb', line 113 def self.close_io(io) if io.respond_to? :close! io.close! # Tempfile else io.close unless io.closed? end end |
.data(req) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'opal/stdlib/open-uri.rb', line 145 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:
121 122 123 124 125 126 127 128 129 130 131 |
# File 'opal/stdlib/open-uri.rb', line 121 def self.open_loop(uri, ) # :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:
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'opal/stdlib/open-uri.rb', line 99 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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'opal/stdlib/open-uri.rb', line 158 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 |