Class: Fron::Response

Inherits:
Object show all
Defined in:
opal/fron/request/response.rb

Overview

Class for handling responses from requests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, body, headers) ⇒ type

Initializes the response

Parameters:

  • status (Numeric)

    The status

  • body (String)

    The response body

  • headers (Hash)

    The headers



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

#bodyString (readonly)

Returns The response body.

Returns:

  • (String)

    The response body



5
6
7
# File 'opal/fron/request/response.rb', line 5

def body
  @body
end

#headersHash (readonly)

Returns The response headers.

Returns:

  • (Hash)

    The response headers



8
9
10
# File 'opal/fron/request/response.rb', line 8

def headers
  @headers
end

#statusNumeric (readonly)

Returns The response status code.

Returns:

  • (Numeric)

    The response status code



11
12
13
# File 'opal/fron/request/response.rb', line 11

def status
  @status
end

Instance Method Details

#content_typeString

Returns the content type of the response

Returns:

  • (String)

    The content type



34
35
36
# File 'opal/fron/request/response.rb', line 34

def content_type
  @headers['Content-Type']
end

#domDOM::Fragment

Returns the response body as DOM::Fragment

Returns:



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

#jsonHash

Returns the response body as json

Returns:

  • (Hash)

    The body



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

Returns:

  • (Boolean)

    True if it was false if not



41
42
43
# File 'opal/fron/request/response.rb', line 41

def ok?
  @status == 200
end