Skip to content

Commit

Permalink
Handle jwt response (#96)
Browse files Browse the repository at this point in the history
* feat: Handle JWT responses by wrapping them in a JSON object

* fix: Handle JWT responses to prevent method_missing error in TelnyxResponse

* feat: Update create method definition for token action in TelephonyCredential

* bump version
  • Loading branch information
ADandyGuyInSpace authored Oct 18, 2024
1 parent e9cc645 commit 3bb6715
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.3
3.0.4
11 changes: 6 additions & 5 deletions lib/telnyx/telephony_credential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class TelephonyCredential < APIResource
instance_methods: { create: action }
end

%w[token].each do |action|
nested_resource_class_methods action,
path: %W[#{action}],
operations: [:create],
instance_methods: { create: action }
# Manually create JSON response object from JWT when calling token
def create_token(params = {}, opts = {})
url = "#{resource_url}/token"
resp, opts = request(:post, url, params, opts)
Util.convert_to_telnyx_object(resp.data, opts)
end

# Additional action to list tags
def self.tags(params = {}, opts = {})
opts = Util.normalize_opts(opts)
Expand Down
14 changes: 13 additions & 1 deletion lib/telnyx/telnyx_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@ class TelnyxResponse
# 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.
Expand Down
2 changes: 1 addition & 1 deletion lib/telnyx/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Telnyx
VERSION = "3.0.3".freeze
VERSION = "3.0.4".freeze
end

0 comments on commit 3bb6715

Please sign in to comment.