lib/telnyx/telnyx_response.rb in telnyx-3.0.3 vs lib/telnyx/telnyx_response.rb in telnyx-3.0.4

- old
+ new

@@ -24,15 +24,27 @@ # part of a Faraday exception. # # This may throw JSON::ParserError if the response body is not valid JSON. def self.from_faraday_hash(http_resp) resp = TelnyxResponse.new - resp.data = JSON.parse(preprocess_response(http_resp[:body]), symbolize_names: true) + 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 + + def self.parse_response_body(body) + if jwt_format?(body) + { token: body } + else + JSON.parse(preprocess_response(body), symbolize_names: true) + end + end + + def self.jwt_format?(body) + body.count(".") == 2 && body.split(".").all? { |segment| segment.match?(/\A[a-zA-Z0-9_-]+\z/) } end # Initializes a TelnyxResponse object from a Faraday HTTP response object. # # This may throw JSON::ParserError if the response body is not valid JSON.