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


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


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


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


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


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

def ok?
  @status == 200
end