Class: Fron::Response

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

Overview

Response

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


13
14
15
16
17
18
19
20
21
22
# File 'opal/fron/request/response.rb', line 13

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

#bodyObject (readonly)

Returns the value of attribute body


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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers


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

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status


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

def status
  @status
end

Instance Method Details

#contentTypeString

Returns the content type of the response

Returns:

  • (String)

    The content type


27
28
29
# File 'opal/fron/request/response.rb', line 27

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

#domDOM::Fragment

Returns the response body as DOM::Fragment

Returns:


48
49
50
51
52
53
54
# File 'opal/fron/request/response.rb', line 48

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


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

def json
  JSON.parse @body
end

#ok?Boolean

Returns whether the request was successfull

Returns:

  • (Boolean)

    True if it was false if not


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

def ok?
  @status == 200
end