Class: Browser::HTTP::Response
- Includes:
- Native::Wrapper
- 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.
-
#url ⇒ String
readonly
The response URL (after redirects).
-
#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.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'opal/browser/http/response.rb', line 98 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.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'opal/browser/http/response.rb', line 70 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.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'opal/browser/http/response.rb', line 56 def text %x{ var result = #@native.responseText; if (!result) { return nil; } return result; } end |
#url ⇒ String (readonly)
Returns the response URL (after redirects).
52 |
# File 'opal/browser/http/response.rb', line 52 alias_native :url, :responseURL |
#xml ⇒ DOM::Document (readonly)
Returns the response body as DOM document.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'opal/browser/http/response.rb', line 84 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 |