Class: Telnyx::TelnyxResponse
- Inherits:
-
Object
- Object
- Telnyx::TelnyxResponse
- Defined in:
- lib/telnyx/telnyx_response.rb
Overview
TelnyxResponse encapsulates some vitals of a response that came back from the Telnyx API.
Instance Attribute Summary collapse
-
#data ⇒ Object
The data contained by the HTTP body of the response deserialized from JSON.
-
#http_body ⇒ Object
The raw HTTP body of the response.
-
#http_headers ⇒ Object
A Hash of the HTTP headers of the response.
-
#http_status ⇒ Object
The integer HTTP status code of the response.
-
#request_id ⇒ Object
The Telnyx request ID of the response.
Class Method Summary collapse
-
.from_faraday_hash(http_resp) ⇒ Object
Initializes a TelnyxResponse object from a Hash like the kind returned as part of a Faraday exception.
-
.from_faraday_response(http_resp) ⇒ Object
Initializes a TelnyxResponse object from a Faraday HTTP response object.
- .jwt_format?(body) ⇒ Boolean
- .parse_response_body(body) ⇒ Object
Instance Attribute Details
#data ⇒ Object
The data contained by the HTTP body of the response deserialized from JSON.
9 10 11 |
# File 'lib/telnyx/telnyx_response.rb', line 9 def data @data end |
#http_body ⇒ Object
The raw HTTP body of the response.
12 13 14 |
# File 'lib/telnyx/telnyx_response.rb', line 12 def http_body @http_body end |
#http_headers ⇒ Object
A Hash of the HTTP headers of the response.
15 16 17 |
# File 'lib/telnyx/telnyx_response.rb', line 15 def http_headers @http_headers end |
#http_status ⇒ Object
The integer HTTP status code of the response.
18 19 20 |
# File 'lib/telnyx/telnyx_response.rb', line 18 def http_status @http_status end |
#request_id ⇒ Object
The Telnyx request ID of the response.
21 22 23 |
# File 'lib/telnyx/telnyx_response.rb', line 21 def request_id @request_id end |
Class Method Details
.from_faraday_hash(http_resp) ⇒ Object
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.
27 28 29 30 31 32 33 34 35 |
# File 'lib/telnyx/telnyx_response.rb', line 27 def self.from_faraday_hash(http_resp) resp = TelnyxResponse.new resp.data = parse_response_body(http_resp[:body]) 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) ⇒ Object
Initializes a TelnyxResponse object from a Faraday HTTP response object.
This may throw JSON::ParserError if the response body is not valid JSON.
52 53 54 55 56 57 58 59 60 |
# File 'lib/telnyx/telnyx_response.rb', line 52 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 |
.jwt_format?(body) ⇒ Boolean
45 46 47 |
# File 'lib/telnyx/telnyx_response.rb', line 45 def self.jwt_format?(body) body.count(".") == 2 && body.split(".").all? { |segment| segment.match?(/\A[a-zA-Z0-9_-]+\z/) } end |
.parse_response_body(body) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/telnyx/telnyx_response.rb', line 37 def self.parse_response_body(body) if jwt_format?(body) { token: body } else JSON.parse(preprocess_response(body), symbolize_names: true) end end |