Class: Browser::HTTP::Response
- Includes:
- Native
- Defined in:
- opal/browser/http/response.rb
Overview
Represents an HTTP response.
Defined Under Namespace
Classes: Status
Instance Attribute Summary collapse
-
#binary ⇒ Binary
readonly
The response body as binary.
-
#headers ⇒ Headers
readonly
The response headers.
-
#json ⇒ Hash, Array
readonly
The response body as JSON.
-
#request ⇒ Request
readonly
The request to this response.
-
#status ⇒ Status
readonly
The response status.
-
#text ⇒ String
readonly
The response body as text.
-
#xml ⇒ DOM::Document
readonly
The response body as DOM document.
Instance Method Summary collapse
-
#failure? ⇒ Boolean
Check if the response failed.
-
#initialize(request) ⇒ Response
constructor
Create a response from a request.
-
#success? ⇒ Boolean
Checks if the response was successful.
Constructor Details
#initialize(request) ⇒ Response
Create a response from a request.
18 19 20 21 22 |
# File 'opal/browser/http/response.rb', line 18 def initialize(request) super(request.to_n) @request = request end |
Instance Attribute Details
#binary ⇒ Binary (readonly)
Returns the response body as binary.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'opal/browser/http/response.rb', line 94 def binary return unless request.binary? if Buffer.supported? %x{ var result = #@native.response; if (!result) { return nil; } } Binary.new(Buffer.new(`result`)) else return unless text Binary.new(text) end end |
#headers ⇒ Headers (readonly)
Returns the response headers.
26 27 28 |
# File 'opal/browser/http/response.rb', line 26 def headers @headers ||= Headers.parse(`#@native.getAllResponseHeaders()`) end |
#json ⇒ Hash, Array (readonly)
Returns the response body as JSON.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'opal/browser/http/response.rb', line 66 def json %x{ var result = #@native.responseText; if (!result) { return nil; } return #{JSON.parse(`result`)}; } end |
#request ⇒ Request (readonly)
Returns the request to this response.
13 14 15 |
# File 'opal/browser/http/response.rb', line 13 def request @request end |
#status ⇒ Status (readonly)
Returns the response status.
32 33 34 |
# File 'opal/browser/http/response.rb', line 32 def status Status.new(`#@native.status || nil`, `#@native.statusText || nil`) end |
#text ⇒ String (readonly)
Returns the response body as text.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'opal/browser/http/response.rb', line 52 def text %x{ var result = #@native.responseText; if (!result) { return nil; } return result; } end |
#xml ⇒ DOM::Document (readonly)
Returns the response body as DOM document.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'opal/browser/http/response.rb', line 80 def xml %x{ var result = #@native.responseXML; if (!result) { return nil; } } DOM(`result`) end |
Instance Method Details
#failure? ⇒ Boolean
Check if the response failed
46 47 48 |
# File 'opal/browser/http/response.rb', line 46 def failure? !success? end |
#success? ⇒ Boolean
Checks if the response was successful
37 38 39 40 41 42 43 |
# File 'opal/browser/http/response.rb', line 37 def success? if code = status.code code >= 200 && code < 300 || code == 304 else false end end |