Class: Fron::Response
Overview
Class for handling responses from requests.
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The response body.
-
#headers ⇒ Hash
readonly
The response headers.
-
#status ⇒ Numeric
readonly
The response status code.
Instance Method Summary collapse
-
#content_type ⇒ String
Returns the content type of the response.
-
#dom ⇒ DOM::Fragment
Returns the response body as DOM::Fragment.
-
#initialize(status, body, headers) ⇒ type
constructor
Initializes the response.
-
#json ⇒ Hash
Returns the response body as json.
-
#ok? ⇒ Boolean
Returns whether the request was successfull.
Constructor Details
#initialize(status, body, headers) ⇒ type
Initializes the response
20 21 22 23 24 25 26 27 28 29 |
# File 'opal/fron/request/response.rb', line 20 def initialize(status, body, headers) @body = body @status = status @headers = {} headers.strip.split(/\n/).each do |item| match = item.split(/:/) @headers[match[0]] = match[1].strip end end |
Instance Attribute Details
#body ⇒ String (readonly)
Returns The response body.
5 6 7 |
# File 'opal/fron/request/response.rb', line 5 def body @body end |
#headers ⇒ Hash (readonly)
Returns The response headers.
8 9 10 |
# File 'opal/fron/request/response.rb', line 8 def headers @headers end |
#status ⇒ Numeric (readonly)
Returns The response status code.
11 12 13 |
# File 'opal/fron/request/response.rb', line 11 def status @status end |
Instance Method Details
#content_type ⇒ String
Returns the content type of the response
34 35 36 |
# File 'opal/fron/request/response.rb', line 34 def content_type @headers['Content-Type'] end |
#dom ⇒ DOM::Fragment
Returns the response body as DOM::Fragment
55 56 57 58 59 60 61 |
# File 'opal/fron/request/response.rb', line 55 def dom div = DOM::Element.new 'div' div.html = @body fragment = DOM::Fragment.new fragment << div fragment end |
#json ⇒ Hash
Returns the response body as json
48 49 50 |
# File 'opal/fron/request/response.rb', line 48 def json JSON.parse @body end |
#ok? ⇒ Boolean
Returns whether the request was successfull
41 42 43 |
# File 'opal/fron/request/response.rb', line 41 def ok? @status == 200 end |