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.
19 20 21 22 23 |
# File 'opal/browser/http/response.rb', line 19 def initialize(request) super(request.to_n) @request = request end |
Instance Attribute Details
#binary ⇒ Binary (readonly)
Returns the response body as binary.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'opal/browser/http/response.rb', line 99 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.
27 28 29 |
# File 'opal/browser/http/response.rb', line 27 def headers @headers ||= Headers.parse(`#@native.getAllResponseHeaders()`) end |
#json ⇒ Hash, Array (readonly)
Returns the response body as JSON.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'opal/browser/http/response.rb', line 71 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.
14 15 16 |
# File 'opal/browser/http/response.rb', line 14 def request @request end |
#status ⇒ Status (readonly)
Returns the response status.
33 34 35 |
# File 'opal/browser/http/response.rb', line 33 def status Status.new(`#@native.status || nil`, `#@native.statusText || nil`) end |
#text ⇒ String (readonly)
Returns the response body as text.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'opal/browser/http/response.rb', line 57 def text %x{ var result = #@native.responseText; if (!result) { return nil; } return result; } end |
#url ⇒ String (readonly)
Returns the response URL (after redirects).
53 |
# File 'opal/browser/http/response.rb', line 53 alias_native :url, :responseURL |
#xml ⇒ DOM::Document (readonly)
Returns the response body as DOM document.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'opal/browser/http/response.rb', line 85 def xml %x{ var result = #@native.responseXML; if (!result) { return nil; } } DOM(`result`) end |
Instance Method Details
#failure? ⇒ Boolean
Check if the response failed
47 48 49 |
# File 'opal/browser/http/response.rb', line 47 def failure? !success? end |
#success? ⇒ Boolean
Checks if the response was successful
38 39 40 41 42 43 44 |
# File 'opal/browser/http/response.rb', line 38 def success? if code = status.code code >= 200 && code < 300 || code == 304 else false end end |