Class: Helio::HelioResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/helio/helio_response.rb

Overview

HelioResponse encapsulates some vitals of a response that came back from the Helio API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dataObject

The data contained by the HTTP body of the response deserialized from JSON.



9
10
11
# File 'lib/helio/helio_response.rb', line 9

def data
  @data
end

#http_bodyObject

The raw HTTP body of the response.



12
13
14
# File 'lib/helio/helio_response.rb', line 12

def http_body
  @http_body
end

#http_headersObject

A Hash of the HTTP headers of the response.



15
16
17
# File 'lib/helio/helio_response.rb', line 15

def http_headers
  @http_headers
end

#http_statusObject

The integer HTTP status code of the response.



18
19
20
# File 'lib/helio/helio_response.rb', line 18

def http_status
  @http_status
end

#request_idObject

The Helio request ID of the response.



21
22
23
# File 'lib/helio/helio_response.rb', line 21

def request_id
  @request_id
end

Class Method Details

.from_faraday_hash(http_resp) ⇒ Object

Initializes a HelioResponse object from a Hash like the kind returned as part of a Faraday exception.

This may throw JSON::ParserError if the response body is not valid JSON.



27
28
29
30
31
32
33
34
35
# File 'lib/helio/helio_response.rb', line 27

def self.from_faraday_hash(http_resp)
  resp = HelioResponse.new
  resp.data = JSON.parse(http_resp[:body], symbolize_names: true)
  resp.http_body = http_resp[:body]
  resp.http_headers = http_resp[:headers]
  resp.http_status = http_resp[:status]
  resp.request_id = http_resp[:headers]["Request-Id"]
  resp
end

.from_faraday_response(http_resp) ⇒ Object

Initializes a HelioResponse object from a Faraday HTTP response object.

This may throw JSON::ParserError if the response body is not valid JSON.



40
41
42
43
44
45
46
47
48
# File 'lib/helio/helio_response.rb', line 40

def self.from_faraday_response(http_resp)
  resp = HelioResponse.new
  resp.data = JSON.parse(http_resp.body, symbolize_names: true)
  resp.http_body = http_resp.body
  resp.http_headers = http_resp.headers
  resp.http_status = http_resp.status
  resp.request_id = http_resp.headers["Request-Id"]
  resp
end