treezor_connect 0.7.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83f870f42093802e04b695222163095cdaa5208025df3f65d9b5af8838aacdc0
4
- data.tar.gz: 8e370b7b4b36f0494c4073769df7cb766d87b3c370593198dfc7acf4bf99239f
3
+ metadata.gz: d9ca1c1ca1b1277e8c80a0541670ed13d23609011347c19f89cbc0a9a155b393
4
+ data.tar.gz: 7dd6a7ad64155b4fb4ac2ca2df5c957e9215e257a91b58c59698694e27724515
5
5
  SHA512:
6
- metadata.gz: 8c2b70c6d491554bf1d004d934e73d88b9bf71943cbd1d05a549d22c51d2aba2a161ca2333a9aced5fb416b7599b369483e12b9826cd2e3c98db7725a3394f44
7
- data.tar.gz: 3a4ade481a1153570e307471e461393b195eee5d56ce60315f2c9bee2c3690999547b91e894e5d97c6dd56505b604168ab1b933c9c36ab25e8e1ee6de85403c8
6
+ metadata.gz: e95d83ed1cd19d96adbaae7218b5f40b74cb9c9e88cf22add9711a8425fd309372f8e6d59dc5cb58e7d8d993187cdca8815773753c16e065149befe8ad70302f
7
+ data.tar.gz: ae60758b4e780253c0b784bd8568d6e852dc420143b9d1f4bff75ff508e540befca9755de34c52aacf5b010a6b0301fe26fbf592c213a39b42d39956245cb7d3
@@ -3,9 +3,9 @@
3
3
  module TreezorConnect
4
4
  module ApiOperations
5
5
  module Create
6
- def create(params = {})
7
- treezor_response = request(:post, resource_url, body: params)
8
- data = defined?(self::OBJECT_KEY) ? treezor_response.data.fetch(self::OBJECT_KEY)[0] : treezor_response.data
6
+ def create(params = {}, access_token = nil)
7
+ treezor_response = request(:post, resource_url, params: { body: params }, access_token:)
8
+ data = extract_response_data(treezor_response, extract_all_objects: false)
9
9
  Util.convert_to_treezor_object(data, { object_class: self::OBJECT_NAME })
10
10
  end
11
11
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TreezorConnect
4
+ module ApiOperations
5
+ module Delete
6
+ def delete(id, params = {}, access_token = nil)
7
+ url = [resource_url, id].join('/')
8
+ treezor_response = request(:delete, url, params: { body: params }, access_token:)
9
+ data = extract_response_data(treezor_response, extract_all_objects: false)
10
+ Util.convert_to_treezor_object(data, { object_class: self::OBJECT_NAME })
11
+ end
12
+ end
13
+ end
14
+ end
@@ -3,10 +3,10 @@
3
3
  module TreezorConnect
4
4
  module ApiOperations
5
5
  module Fetch
6
- def fetch(id)
6
+ def fetch(id, access_token = nil)
7
7
  url = [resource_url, id].join('/')
8
- treezor_response = request(:get, url)
9
- data = treezor_response.data.fetch(self::OBJECT_KEY)[0]
8
+ treezor_response = request(:get, url, access_token:)
9
+ data = extract_response_data(treezor_response, extract_all_objects: false)
10
10
  Util.convert_to_treezor_object(data, { object_class: self::OBJECT_NAME })
11
11
  end
12
12
  end
@@ -3,9 +3,9 @@
3
3
  module TreezorConnect
4
4
  module ApiOperations
5
5
  module List
6
- def list(filters = {})
7
- treezor_response = request(:get, resource_url, { query: filters })
8
- data = treezor_response.data.fetch(self::OBJECT_KEY)
6
+ def list(filters = {}, access_token = nil)
7
+ treezor_response = request(:get, resource_url, params: { query: filters }, access_token:)
8
+ data = extract_response_data(treezor_response)
9
9
  Util.convert_to_treezor_object(data, { object_class: self::OBJECT_NAME })
10
10
  end
11
11
  end
@@ -4,9 +4,9 @@ module TreezorConnect
4
4
  module ApiOperations
5
5
  module Request
6
6
  module ClassMethods
7
- def request(method, url, params = {}, headers = {})
7
+ def request(method, url, params: {}, headers: {}, access_token: nil)
8
8
  params = Util.normalize_params(params)
9
- client = TreezorConnect::Client.active_client
9
+ client = TreezorConnect::Client.new(access_token)
10
10
  response = client.execute_request(
11
11
  method, url,
12
12
  headers:,
@@ -14,6 +14,17 @@ module TreezorConnect
14
14
  )
15
15
  TreezorResponse.from_http_response(response)
16
16
  end
17
+
18
+ def extract_response_data(response, extract_all_objects: true)
19
+ if defined?(self::OBJECT_KEY) && response.data.key?(self::OBJECT_KEY)
20
+ objects = response.data.fetch(self::OBJECT_KEY)
21
+ return objects if extract_all_objects
22
+
23
+ return objects[0]
24
+ end
25
+
26
+ response.data
27
+ end
17
28
  end
18
29
 
19
30
  def self.included(base)
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TreezorConnect
4
+ module ApiOperations
5
+ module Update
6
+ def update(id, params = {}, access_token = nil)
7
+ url = [resource_url, id].join('/')
8
+ treezor_response = request(:put, url, params: { body: params }, access_token:)
9
+ data = extract_response_data(treezor_response, extract_all_objects: false)
10
+ Util.convert_to_treezor_object(data, { object_class: self::OBJECT_NAME })
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,11 +1,90 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class ApiResource
4
- include TreezorConnect::ApiOperations::Request
3
+ module TreezorConnect
4
+ class ApiResource
5
+ include TreezorConnect::ApiOperations::Request
5
6
 
6
- def self.resource_url
7
- raise NotImplementedError,
8
- 'APIResource is an abstract class. You should perform actions ' \
9
- 'on its subclasses (Payin, Payout, Transfer ...)'
7
+ class << self
8
+ def resource_url
9
+ raise NotImplementedError,
10
+ 'APIResource is an abstract class. You should perform actions ' \
11
+ 'on its subclasses (Payin, Payout, Transfer ...)'
12
+ end
13
+
14
+ def custom_action(http_method, *actions, ignores_id: false)
15
+ actions.each do |action|
16
+ case http_method
17
+ when :post
18
+ define_method_with_params(http_method, action)
19
+ when :put, :patch
20
+ define_put_path_methods(http_method, action, ignores_id)
21
+ else
22
+ raise ArgumentError, "Unsupported HTTP method: #{http_method}"
23
+ end
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def define_method_with_params(http_method, action)
30
+ define_singleton_method(action) do |params = {}, access_token = nil|
31
+ url = build_url(action)
32
+ response = request(http_method, url, params: { body: params }, access_token:)
33
+ process_response(response)
34
+ end
35
+ end
36
+
37
+ def define_put_path_methods(http_method, action, ignores_id)
38
+ if ignores_id
39
+ define_method_without_id_and_params(http_method, action)
40
+ else
41
+ define_method_with_id_and_params(http_method, action)
42
+ end
43
+ end
44
+
45
+ def define_method_with_id_and_params(http_method, action)
46
+ define_singleton_method(action) do |id, params = {}, access_token = nil|
47
+ url = build_url(action, id)
48
+ response = request(http_method, url, params: { body: params }, access_token:)
49
+ process_response(response)
50
+ end
51
+ end
52
+
53
+ def define_method_without_id_and_params(http_method, action)
54
+ define_singleton_method(action) do |params = {}, access_token = nil|
55
+ url = build_url(action)
56
+ response = request(http_method, url, params: { body: params }, access_token:)
57
+ process_response(response)
58
+ end
59
+ end
60
+
61
+ def build_url(action, id = nil)
62
+ parts = [resource_url]
63
+ parts << id if id
64
+ parts << camelize(action.to_s)
65
+ parts.join('/')
66
+ end
67
+
68
+ def process_response(response)
69
+ return if response.data.nil?
70
+
71
+ data = if defined?(self::OBJECT_KEY) && response.data.key?(self::OBJECT_KEY)
72
+ response.data.fetch(self::OBJECT_KEY)[0]
73
+ else
74
+ response.data
75
+ end
76
+ Util.convert_to_treezor_object(data, { object_class: self::OBJECT_NAME })
77
+ end
78
+
79
+ def camelize(string, capitalize_first_letter: false)
80
+ string
81
+ .to_s
82
+ .split('_')
83
+ .map.with_index do |word, index|
84
+ capitalize_first_letter || index.positive? ? word.capitalize : word
85
+ end
86
+ .join
87
+ end
88
+ end
10
89
  end
11
90
  end
@@ -2,56 +2,63 @@
2
2
 
3
3
  module TreezorConnect
4
4
  class Client
5
- attr_accessor :conn, :oauth_client
6
-
7
- def initialize(conn = nil)
8
- self.conn = conn || self.class.default_conn
9
- self.oauth_client ||= OAuth2::Client.new(
10
- TreezorConnect.client_id,
11
- TreezorConnect.client_secret,
12
- site: TreezorConnect.api_base_url
13
- )
14
- end
15
-
16
- def self.active_client
17
- Thread.current[:treezor_connect_client] ||= default_client
18
- end
19
-
20
- def self.default_client
21
- Thread.current[:treezor_connect_client_default_client] ||=
22
- TreezorConnect::Client.new(default_conn)
23
- end
24
-
25
- def self.default_conn
26
- Thread.current[:treezor_connect_client_default_conn] ||= build_default_conn
5
+ def initialize(access_token)
6
+ @conn = build_default_conn
7
+ @access_token = access_token.nil? ? default_access_token : access_token
27
8
  end
28
9
 
29
10
  def execute_request(method, path, headers: {}, params: {})
30
11
  headers['Authorization'] = "Bearer #{access_token}"
31
12
  response = conn.public_send(method, path, params: params[:query], form: params[:form], json: params[:body],
32
13
  headers:)
33
- http_body = JSON.parse(response.to_s)
34
14
  http_status = response.status
35
- raise_api_error(http_status:, http_body:) unless http_status.success?
15
+ raise_api_error(http_status:, http_body: JSON.parse(response.to_s)) unless http_status.success?
36
16
 
37
17
  response
38
18
  end
39
19
 
40
- def self.build_default_conn
20
+ private
21
+
22
+ attr_reader :conn, :access_token
23
+
24
+ def build_default_conn
41
25
  http = HTTP.persistent(TreezorConnect.api_base_url)
42
26
  return http.use(logging: { logger: Logger.new($stdout) }) unless ENV['ENABLE_HTTP_LOGGING'].nil?
43
27
 
44
28
  http
45
29
  end
46
30
 
47
- private
31
+ def oauth_client
32
+ @oauth_client ||= OAuth2::Client.new(
33
+ TreezorConnect.client_id,
34
+ TreezorConnect.client_secret,
35
+ site: TreezorConnect.api_base_url
36
+ )
37
+ end
48
38
 
49
- def access_token
39
+ def fetch_access_token
50
40
  oauth_client.client_credentials.get_token.token
51
41
  end
52
42
 
53
43
  def raise_api_error(http_status:, http_body:)
54
- raise ApiError, "API request failed: #{http_body.inspect} (HTTP response code was #{http_status})"
44
+ raise ApiError.new(
45
+ "API request failed: #{http_body.inspect} (HTTP response code was #{http_status})",
46
+ http_body,
47
+ http_status
48
+ )
49
+ end
50
+
51
+ def token_expired?(token)
52
+ OAuth2::AccessToken.new(conn, token).expired?
53
+ end
54
+
55
+ def default_access_token
56
+ access_token = Thread.current[:treezor_default_access_token]
57
+ if access_token.nil? || token_expired?(access_token)
58
+ Thread.current[:treezor_default_access_token] = fetch_access_token
59
+ else
60
+ access_token
61
+ end
55
62
  end
56
63
  end
57
64
  end
@@ -1,3 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class ApiError < StandardError; end
3
+ class ApiError < StandardError
4
+ attr_reader :errors, :status
5
+
6
+ def initialize(message = '', errors = {}, status = 0)
7
+ @errors = errors
8
+ @status = status
9
+
10
+ super(message)
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TreezorConnect
4
+ class Beneficiary < ApiResource
5
+ extend TreezorConnect::ApiOperations::Fetch
6
+ extend TreezorConnect::ApiOperations::List
7
+ extend TreezorConnect::ApiOperations::Create
8
+ extend TreezorConnect::ApiOperations::Update
9
+
10
+ OBJECT_NAME = 'beneficiary'
11
+ OBJECT_KEY = 'beneficiaries'
12
+ OBJECT_PRIMARY_KEY = 'id'
13
+
14
+ def self.resource_url
15
+ '/v1/beneficiaries'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TreezorConnect
4
+ module Oauth
5
+ class Token
6
+ class << self
7
+ def create(params = {})
8
+ oauth_client.client_credentials.get_token({
9
+ grant_type: 'delegated_end_user',
10
+ client_id: TreezorConnect.client_id,
11
+ client_secret: TreezorConnect.client_secret,
12
+ username: params[:username],
13
+ password: params[:password],
14
+ sca: params[:sca]
15
+ }).token
16
+ end
17
+
18
+ private
19
+
20
+ def oauth_client
21
+ OAuth2::Client.new(
22
+ TreezorConnect.client_id,
23
+ TreezorConnect.client_secret,
24
+ site: TreezorConnect.api_base_url
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TreezorConnect
4
+ module Sca
5
+ class ExternalOperation < ApiResource
6
+ extend TreezorConnect::ApiOperations::Create
7
+ extend TreezorConnect::ApiOperations::Update
8
+
9
+ OBJECT_NAME = 'sca/external_operation'
10
+ OBJECT_PRIMARY_KEY = 'externalOperationId'
11
+
12
+ def self.resource_url
13
+ '/core-connect/sca/externalOperations'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TreezorConnect
4
+ module Sca
5
+ class Passcode < ApiResource
6
+ custom_action :put, :set_passcode, ignores_id: true
7
+
8
+ def self.resource_url
9
+ '/core-connect/sca'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TreezorConnect
4
+ module Sca
5
+ class Wallet < ApiResource
6
+ extend TreezorConnect::ApiOperations::Fetch
7
+ extend TreezorConnect::ApiOperations::List
8
+ extend TreezorConnect::ApiOperations::Create
9
+ extend TreezorConnect::ApiOperations::Delete
10
+
11
+ custom_action :put, :lock, :unlock
12
+ custom_action :post, :swap
13
+
14
+ OBJECT_NAME = 'sca/wallet'
15
+ OBJECT_KEY = 'scaWallets'
16
+ OBJECT_PRIMARY_KEY = 'id'
17
+
18
+ def self.resource_url
19
+ '/core-connect/sca/scawallets'
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'treezor_connect/resources/beneficiary'
3
4
  require 'treezor_connect/resources/payin'
5
+ require 'treezor_connect/resources/oauth/token'
6
+ require 'treezor_connect/resources/sca/external_operation'
7
+ require 'treezor_connect/resources/sca/passcode'
8
+ require 'treezor_connect/resources/sca/wallet'
4
9
  require 'treezor_connect/resources/sct_inst/payin'
5
10
  require 'treezor_connect/resources/sct_inst/recall'
@@ -6,7 +6,7 @@ module TreezorConnect
6
6
 
7
7
  def self.from_http_response(http_resp)
8
8
  resp = TreezorResponse.new
9
- resp.data = JSON.parse(http_resp.body)
9
+ resp.data = JSON.parse(http_resp.body) if http_resp.body.to_s != ''
10
10
  resp.http_body = http_resp.body
11
11
  resp.http_headers = http_resp.headers
12
12
  resp.http_status = http_resp.status
@@ -20,6 +20,8 @@ require 'treezor_connect/api_operations/fetch'
20
20
  require 'treezor_connect/api_operations/list'
21
21
  require 'treezor_connect/api_operations/create'
22
22
  require 'treezor_connect/api_operations/request'
23
+ require 'treezor_connect/api_operations/update'
24
+ require 'treezor_connect/api_operations/delete'
23
25
 
24
26
  # API resources
25
27
  require 'treezor_connect/api_resource'
@@ -27,7 +29,7 @@ require 'treezor_connect/resources'
27
29
 
28
30
  module TreezorConnect
29
31
  class << self
30
- attr_accessor :api_base_url, :api_version, :client_id, :client_secret
32
+ attr_accessor :api_base_url, :client_id, :client_secret
31
33
  end
32
34
 
33
35
  def self.configure
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: treezor_connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - stefakins
8
+ - jbauzone
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2023-10-27 00:00:00.000000000 Z
12
+ date: 2024-10-24 00:00:00.000000000 Z
12
13
  dependencies: []
13
14
  description: A gem for making HTTP calls to Treezor Connect.
14
15
  email: stefan.atkinson69@gmail.com
@@ -18,14 +19,21 @@ extra_rdoc_files: []
18
19
  files:
19
20
  - lib/treezor_connect.rb
20
21
  - lib/treezor_connect/api_operations/create.rb
22
+ - lib/treezor_connect/api_operations/delete.rb
21
23
  - lib/treezor_connect/api_operations/fetch.rb
22
24
  - lib/treezor_connect/api_operations/list.rb
23
25
  - lib/treezor_connect/api_operations/request.rb
26
+ - lib/treezor_connect/api_operations/update.rb
24
27
  - lib/treezor_connect/api_resource.rb
25
28
  - lib/treezor_connect/client.rb
26
29
  - lib/treezor_connect/errors.rb
27
30
  - lib/treezor_connect/resources.rb
31
+ - lib/treezor_connect/resources/beneficiary.rb
32
+ - lib/treezor_connect/resources/oauth/token.rb
28
33
  - lib/treezor_connect/resources/payin.rb
34
+ - lib/treezor_connect/resources/sca/external_operation.rb
35
+ - lib/treezor_connect/resources/sca/passcode.rb
36
+ - lib/treezor_connect/resources/sca/wallet.rb
29
37
  - lib/treezor_connect/resources/sct_inst/payin.rb
30
38
  - lib/treezor_connect/resources/sct_inst/recall.rb
31
39
  - lib/treezor_connect/treezor_object.rb
@@ -51,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
59
  - !ruby/object:Gem::Version
52
60
  version: '0'
53
61
  requirements: []
54
- rubygems_version: 3.4.10
62
+ rubygems_version: 3.4.19
55
63
  signing_key:
56
64
  specification_version: 4
57
65
  summary: A gem for making HTTP calls to Treezor Connect.