class Telnyx::TelnyxResponse

TelnyxResponse encapsulates some vitals of a response that came back from the Telnyx API.

Attributes

data[RW]

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

http_body[RW]

The raw HTTP body of the response.

http_headers[RW]

A Hash of the HTTP headers of the response.

http_status[RW]

The integer HTTP status code of the response.

request_id[RW]

The Telnyx request ID of the response.

Public Class Methods

from_faraday_hash(http_resp) click to toggle source

Initializes a TelnyxResponse 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.

# File lib/telnyx/telnyx_response.rb, line 27
def self.from_faraday_hash(http_resp)
  resp = TelnyxResponse.new
  resp.data = JSON.parse(preprocess_response(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]["X-Request-Id"]
  resp
end
from_faraday_response(http_resp) click to toggle source

Initializes a TelnyxResponse object from a Faraday HTTP response object.

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

# File lib/telnyx/telnyx_response.rb, line 40
def self.from_faraday_response(http_resp)
  resp = TelnyxResponse.new
  resp.data = JSON.parse(preprocess_response(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["X-Request-Id"]
  resp
end

Private Class Methods

preprocess_response(resp) click to toggle source

Helper to handle when the server responds with a blank body (as is the case with SimCards).

# File lib/telnyx/telnyx_response.rb, line 54
def preprocess_response(resp)
  resp.empty? ? "{}" : resp
end