starkbank 2.6.0 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/lib/balance/balance.rb +8 -8
  3. data/lib/boleto/boleto.rb +22 -19
  4. data/lib/boleto/log.rb +10 -10
  5. data/lib/boleto_holmes/boleto_holmes.rb +14 -14
  6. data/lib/boleto_holmes/log.rb +16 -13
  7. data/lib/boleto_payment/boleto_payment.rb +21 -18
  8. data/lib/boleto_payment/log.rb +10 -10
  9. data/lib/brcode_payment/brcode_payment.rb +23 -20
  10. data/lib/brcode_payment/log.rb +10 -10
  11. data/lib/brcode_payment/rule.rb +49 -0
  12. data/lib/darf_payment/darf_payment.rb +23 -21
  13. data/lib/darf_payment/log.rb +10 -10
  14. data/lib/deposit/deposit.rb +9 -9
  15. data/lib/deposit/log.rb +10 -10
  16. data/lib/dict_key/dict_key.rb +26 -27
  17. data/lib/dynamic_brcode/dynamic_brcode.rb +155 -0
  18. data/lib/error.rb +7 -40
  19. data/lib/event/attempt.rb +9 -9
  20. data/lib/event/event.rb +30 -56
  21. data/lib/institution/institution.rb +2 -3
  22. data/lib/invoice/invoice.rb +24 -23
  23. data/lib/invoice/log.rb +10 -10
  24. data/lib/invoice/payment.rb +1 -2
  25. data/lib/payment_preview/boleto_preview.rb +4 -5
  26. data/lib/payment_preview/brcode_preview.rb +2 -3
  27. data/lib/payment_preview/payment_preview.rb +10 -6
  28. data/lib/payment_preview/tax_preview.rb +2 -3
  29. data/lib/payment_preview/utility_preview.rb +2 -3
  30. data/lib/payment_request/payment_request.rb +22 -16
  31. data/lib/starkbank.rb +16 -5
  32. data/lib/tax_payment/log.rb +10 -10
  33. data/lib/tax_payment/tax_payment.rb +22 -19
  34. data/lib/transaction/transaction.rb +13 -13
  35. data/lib/transfer/log.rb +10 -10
  36. data/lib/transfer/rule.rb +49 -0
  37. data/lib/transfer/transfer.rb +30 -24
  38. data/lib/utility_payment/log.rb +10 -10
  39. data/lib/utility_payment/utility_payment.rb +26 -17
  40. data/lib/utils/parse.rb +35 -0
  41. data/lib/utils/rest.rb +132 -109
  42. data/lib/webhook/webhook.rb +5 -5
  43. data/lib/workspace/workspace.rb +38 -10
  44. metadata +15 -25
  45. data/lib/brcode_preview/brcode_preview.rb +0 -79
  46. data/lib/key.rb +0 -33
  47. data/lib/user/organization.rb +0 -54
  48. data/lib/user/project.rb +0 -37
  49. data/lib/user/user.rb +0 -20
  50. data/lib/utils/api.rb +0 -79
  51. data/lib/utils/cache.rb +0 -10
  52. data/lib/utils/case.rb +0 -21
  53. data/lib/utils/checks.rb +0 -101
  54. data/lib/utils/environment.rb +0 -13
  55. data/lib/utils/request.rb +0 -79
  56. data/lib/utils/resource.rb +0 -13
  57. data/lib/utils/sub_resource.rb +0 -28
  58. data/lib/utils/url.rb +0 -28
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: starkbank
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - starkbank
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-04 00:00:00.000000000 Z
11
+ date: 2023-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: starkbank-ecdsa
14
+ name: starkcore
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.5
19
+ version: 0.0.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.5
26
+ version: 0.0.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,8 +66,8 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.81'
69
- description:
70
- email:
69
+ description:
70
+ email:
71
71
  executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files: []
@@ -81,12 +81,13 @@ files:
81
81
  - lib/boleto_payment/log.rb
82
82
  - lib/brcode_payment/brcode_payment.rb
83
83
  - lib/brcode_payment/log.rb
84
- - lib/brcode_preview/brcode_preview.rb
84
+ - lib/brcode_payment/rule.rb
85
85
  - lib/darf_payment/darf_payment.rb
86
86
  - lib/darf_payment/log.rb
87
87
  - lib/deposit/deposit.rb
88
88
  - lib/deposit/log.rb
89
89
  - lib/dict_key/dict_key.rb
90
+ - lib/dynamic_brcode/dynamic_brcode.rb
90
91
  - lib/error.rb
91
92
  - lib/event/attempt.rb
92
93
  - lib/event/event.rb
@@ -94,7 +95,6 @@ files:
94
95
  - lib/invoice/invoice.rb
95
96
  - lib/invoice/log.rb
96
97
  - lib/invoice/payment.rb
97
- - lib/key.rb
98
98
  - lib/payment_preview/boleto_preview.rb
99
99
  - lib/payment_preview/brcode_preview.rb
100
100
  - lib/payment_preview/payment_preview.rb
@@ -106,29 +106,19 @@ files:
106
106
  - lib/tax_payment/tax_payment.rb
107
107
  - lib/transaction/transaction.rb
108
108
  - lib/transfer/log.rb
109
+ - lib/transfer/rule.rb
109
110
  - lib/transfer/transfer.rb
110
- - lib/user/organization.rb
111
- - lib/user/project.rb
112
- - lib/user/user.rb
113
111
  - lib/utility_payment/log.rb
114
112
  - lib/utility_payment/utility_payment.rb
115
- - lib/utils/api.rb
116
- - lib/utils/cache.rb
117
- - lib/utils/case.rb
118
- - lib/utils/checks.rb
119
- - lib/utils/environment.rb
120
- - lib/utils/request.rb
121
- - lib/utils/resource.rb
113
+ - lib/utils/parse.rb
122
114
  - lib/utils/rest.rb
123
- - lib/utils/sub_resource.rb
124
- - lib/utils/url.rb
125
115
  - lib/webhook/webhook.rb
126
116
  - lib/workspace/workspace.rb
127
117
  homepage: https://github.com/starkbank/sdk-ruby
128
118
  licenses:
129
119
  - MIT
130
120
  metadata: {}
131
- post_install_message:
121
+ post_install_message:
132
122
  rdoc_options: []
133
123
  require_paths:
134
124
  - lib
@@ -143,8 +133,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
133
  - !ruby/object:Gem::Version
144
134
  version: '0'
145
135
  requirements: []
146
- rubygems_version: 3.1.4
147
- signing_key:
136
+ rubygems_version: 3.0.3.1
137
+ signing_key:
148
138
  specification_version: 4
149
139
  summary: SDK to facilitate Ruby integrations with Stark Bank
150
140
  test_files: []
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative('../utils/resource')
4
- require_relative('../utils/rest')
5
- require_relative('../utils/checks')
6
-
7
- module StarkBank
8
- # # BrcodePreview object
9
- #
10
- # A BrcodePreview is used to get information from a BR Code you received to check the informations before paying it.
11
- #
12
- # ## Attributes (return-only):
13
- # - status [string]: Payment status. ex: 'active', 'paid', 'canceled' or 'unknown'
14
- # - name [string]: Payment receiver name. ex: 'Tony Stark'
15
- # - tax_id [string]: Payment receiver tax ID. ex: '012.345.678-90'
16
- # - bank_code [string]: Payment receiver bank code. ex: '20018183'
17
- # - branch_code [string]: Payment receiver branch code. ex: '0001'
18
- # - account_number [string]: Payment receiver account number. ex: '1234567'
19
- # - account_type [string]: Payment receiver account type. ex: 'checking'
20
- # - allow_change [bool]: If True, the payment is able to receive amounts that are diferent from the nominal one. ex: True or False
21
- # - amount [integer]: Value in cents that this payment is expecting to receive. If 0, any value is accepted. ex: 123 (= R$1,23)
22
- # - reconciliation_id [string]: Reconciliation ID linked to this payment. ex: 'txId', 'payment-123'
23
- class BrcodePreview < StarkBank::Utils::Resource
24
- attr_reader :status, :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :allow_change, :amount, :reconciliation_id
25
- def initialize(status:, name:, tax_id:, bank_code:, branch_code:, account_number:, account_type:, allow_change:, amount:, reconciliation_id:)
26
- @status = status
27
- @name = name
28
- @tax_id = tax_id
29
- @bank_code = bank_code
30
- @branch_code = branch_code
31
- @account_number = account_number
32
- @account_type = account_type
33
- @allow_change = allow_change
34
- @amount = amount
35
- @reconciliation_id = reconciliation_id
36
- end
37
-
38
- # # BrcodePreview is DEPRECATED: Please use PaymentPreview instead.
39
- # Retrieve BrcodePreviews
40
- #
41
- # Receive a generator of BrcodePreview objects previously created in the Stark Bank API
42
- #
43
- # ## Parameters (optional):
44
- # - brcodes [list of strings]: List of brcodes to preview. ex: %w[00020126580014br.gov.bcb.pix0136a629532e-7693-4846-852d-1bbff817b5a8520400005303986540510.005802BR5908T'Challa6009Sao Paulo62090505123456304B14A]
45
- # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call
46
- #
47
- # ## Return:
48
- # - generator of BrcodePreview objects with updated attributes
49
- def self.query(limit: nil, brcodes: nil, user: nil)
50
- warn "[DEPRECATION] `BrcodePreview` is deprecated. Please use `PaymentPreview` instead."
51
- StarkBank::Utils::Rest.get_stream(
52
- user: user,
53
- limit: nil,
54
- brcodes: brcodes,
55
- **resource
56
- )
57
- end
58
-
59
- def self.resource
60
- {
61
- resource_name: 'BrcodePreview',
62
- resource_maker: proc { |json|
63
- BrcodePreview.new(
64
- status: json['status'],
65
- name: json['name'],
66
- tax_id: json['tax_id'],
67
- bank_code: json['bank_code'],
68
- branch_code: json['branch_code'],
69
- account_number: json['account_number'],
70
- account_type: json['account_type'],
71
- allow_change: json['allow_change'],
72
- amount: json['amount'],
73
- reconciliation_id: json['reconciliation_id']
74
- )
75
- }
76
- }
77
- end
78
- end
79
- end
data/lib/key.rb DELETED
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require('fileutils')
4
- require('starkbank-ecdsa')
5
-
6
- module StarkBank
7
- module Key
8
- # # Generate a new key pair
9
- # Generates a secp256k1 ECDSA private/public key pair to be used in the API
10
- # authentications
11
- #
12
- # ## Parameters (optional):
13
- # - path [string]: path to save the keys .pem files. No files will be saved if this parameter isn't provided
14
- #
15
- # ## Return:
16
- # - private and public key pems
17
- def self.create(path = nil)
18
- private_key = EllipticCurve::PrivateKey.new
19
- public_key = private_key.publicKey
20
-
21
- private_key_pem = private_key.toPem
22
- public_key_pem = public_key.toPem
23
-
24
- unless path.nil?
25
- FileUtils.mkdir_p(path)
26
- File.write(File.join(path, 'private.pem'), private_key_pem)
27
- File.write(File.join(path, 'public.pem'), public_key_pem)
28
- end
29
-
30
- [private_key_pem, public_key_pem]
31
- end
32
- end
33
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative('user')
4
-
5
- module StarkBank
6
- # # Organization object
7
- # The Organization object is an authentication entity for the SDK that
8
- # represents your entire Organization, being able to access any Workspace
9
- # underneath it and even create new Workspaces. Only a legal representative
10
- # of your organization can register or change the Organization credentials.
11
- # All requests to the Stark Bank API must be authenticated via an SDK user,
12
- # which must have been previously created at the Stark Bank website
13
- # [https://web.sandbox.starkbank.com] or [https://web.starkbank.com]
14
- # before you can use it in this SDK. Organizations may be passed as the user parameter on
15
- # each request or may be defined as the default user at the start (See README).
16
- # If you are accessing a specific Workspace using Organization credentials, you should
17
- # specify the workspace ID when building the Organization object or by request, using
18
- # the Organization.replace(organization, workspace_id) method, which creates a copy of the organization
19
- # object with the altered workspace ID. If you are listing or creating new Workspaces, the
20
- # workspace_id should be nil.
21
- #
22
- # ## Parameters (required):
23
- # - environment [string]: environment where the organization is being used. ex: 'sandbox' or 'production'
24
- # - id [string]: unique id required to identify organization. ex: '5656565656565656'
25
- # - private_key [string]: PEM string of the private key linked to the organization. ex: '-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEyTIHK6jYuik6ktM9FIF3yCEYzpLjO5X/\ntqDioGM+R2RyW0QEo+1DG8BrUf4UXHSvCjtQ0yLppygz23z0yPZYfw==\n-----END PUBLIC KEY-----'
26
- # - workspace_id [string]: unique id of the accessed Workspace, if any. ex: nil or '4848484848484848'
27
- #
28
- # ## Attributes (return-only):
29
- # - pem [string]: private key in pem format. ex: '-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEyTIHK6jYuik6ktM9FIF3yCEYzpLjO5X/\ntqDioGM+R2RyW0QEo+1DG8BrUf4UXHSvCjtQ0yLppygz23z0yPZYfw==\n-----END PUBLIC KEY-----'
30
- class Organization < StarkBank::User
31
- attr_reader :workspace_id
32
- def initialize(id:, environment:, private_key:, workspace_id: nil)
33
- super(environment, id, private_key)
34
- @workspace_id = workspace_id
35
- end
36
-
37
- def access_id
38
- if @workspace_id
39
- "organization/#{@id}/workspace/#{@workspace_id}"
40
- else
41
- "organization/#{@id}"
42
- end
43
- end
44
-
45
- def self.replace(organization, workspace_id)
46
- Organization.new(
47
- environment: organization.environment,
48
- id: organization.id,
49
- private_key: organization.pem,
50
- workspace_id: workspace_id
51
- )
52
- end
53
- end
54
- end
data/lib/user/project.rb DELETED
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative('user')
4
-
5
- module StarkBank
6
- # # Project object
7
- #
8
- # The Project object is an authentication entity for the SDK that is permanently
9
- # linked to a specific Workspace.
10
- # All requests to the Stark Bank API must be authenticated via an SDK user,
11
- # which must have been previously created at the Stark Bank website
12
- # [https://web.sandbox.starkbank.com] or [https://web.starkbank.com]
13
- # before you can use it in this SDK. Projects may be passed as the user parameter on
14
- # each request or may be defined as the default user at the start (See README).
15
- #
16
- # ## Parameters (required):
17
- # - id [string]: unique id required to identify project. ex: '5656565656565656'
18
- # - private_key [string]: PEM string of the private key linked to the project. ex: '-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEyTIHK6jYuik6ktM9FIF3yCEYzpLjO5X/\ntqDioGM+R2RyW0QEo+1DG8BrUf4UXHSvCjtQ0yLppygz23z0yPZYfw==\n-----END PUBLIC KEY-----'
19
- # - environment [string]: environment where the project is being used. ex: 'sandbox' or 'production'
20
- #
21
- # ## Attributes (return-only):
22
- # - name [string, default '']: project name. ex: 'MyProject'
23
- # - allowed_ips [list of strings]: list containing the strings of the ips allowed to make requests on behalf of this project. ex: ['190.190.0.50']
24
- # - pem [string]: private key in pem format. ex: '-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEyTIHK6jYuik6ktM9FIF3yCEYzpLjO5X/\ntqDioGM+R2RyW0QEo+1DG8BrUf4UXHSvCjtQ0yLppygz23z0yPZYfw==\n-----END PUBLIC KEY-----'
25
- class Project < StarkBank::User
26
- attr_reader :name, :allowed_ips
27
- def initialize(environment:, id:, private_key:, name: '', allowed_ips: nil)
28
- super(environment, id, private_key)
29
- @name = name
30
- @allowed_ips = allowed_ips
31
- end
32
-
33
- def access_id
34
- "project/#{@id}"
35
- end
36
- end
37
- end
data/lib/user/user.rb DELETED
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require('starkbank-ecdsa')
4
- require_relative('../utils/resource')
5
-
6
- module StarkBank
7
- class User < StarkBank::Utils::Resource
8
- attr_reader :pem, :environment
9
- def initialize(environment, id, private_key)
10
- require_relative('../utils/checks')
11
- super(id)
12
- @pem = StarkBank::Utils::Checks.check_private_key(private_key)
13
- @environment = StarkBank::Utils::Checks.check_environment(environment)
14
- end
15
-
16
- def private_key
17
- EllipticCurve::PrivateKey.fromPem(@pem)
18
- end
19
- end
20
- end
data/lib/utils/api.rb DELETED
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative('case')
4
-
5
- module StarkBank
6
- module Utils
7
- module API
8
- def self.build_entity_hash(entity)
9
- if entity.is_a?(Hash)
10
- entity_hash = entity
11
- else
12
- entity_hash = {}
13
- entity.instance_variables.each do |key|
14
- variable = entity.instance_variable_get(key)
15
- entity_hash[key[1..-1]] = variable.is_a?(StarkBank::Utils::Resource) ? build_entity_hash(variable) : entity.instance_variable_get(key)
16
- end
17
- end
18
- entity_hash
19
- end
20
-
21
- def self.api_json(entity)
22
- built_hash = build_entity_hash(entity)
23
- cast_json_to_api_format(built_hash)
24
- end
25
-
26
- def self.cast_json_to_api_format(hash)
27
- entity_hash = {}
28
- hash.each do |key, value|
29
- next if value.nil?
30
-
31
- entity_hash[StarkBank::Utils::Case.snake_to_camel(key)] = parse_value(value)
32
- end
33
- entity_hash
34
- end
35
-
36
- def self.parse_value(value)
37
- return value.strftime('%Y-%m-%d') if value.is_a?(Date)
38
- return value.strftime('%Y-%m-%dT%H:%M:%S+00:00') if value.is_a?(DateTime) || value.is_a?(Time)
39
- return cast_json_to_api_format(value) if value.is_a?(Hash)
40
- return value unless value.is_a?(Array)
41
-
42
- list = []
43
- value.each do |v|
44
- list << (v.is_a?(Hash) ? cast_json_to_api_format(v) : v)
45
- end
46
- list
47
- end
48
-
49
- def self.from_api_json(resource_maker, json)
50
- snakes = {}
51
- json.each do |key, value|
52
- snakes[StarkBank::Utils::Case.camel_to_snake(key)] = value
53
- end
54
- resource_maker.call(snakes)
55
- end
56
-
57
- def self.endpoint(resource_name)
58
- kebab = StarkBank::Utils::Case.camel_to_kebab(resource_name)
59
- kebab.sub!('-log', '/log')
60
- kebab.sub!('-attempt', '/attempt')
61
- kebab
62
- end
63
-
64
- def self.last_name_plural(resource_name)
65
- base = last_name(resource_name)
66
-
67
- return base if base[-1].eql?('s')
68
- return "#{base}s" if base[-2..-1].eql?('ey')
69
- return "#{base[0...-1]}ies" if base[-1].eql?('y')
70
-
71
- "#{base}s"
72
- end
73
-
74
- def self.last_name(resource_name)
75
- StarkBank::Utils::Case.camel_to_kebab(resource_name).split('-').last
76
- end
77
- end
78
- end
79
- end
data/lib/utils/cache.rb DELETED
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module StarkBank
4
- module Utils
5
- module Cache
6
- @starkbank_public_key = nil
7
- class << self; attr_accessor :starkbank_public_key; end
8
- end
9
- end
10
- end
data/lib/utils/case.rb DELETED
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module StarkBank
4
- module Utils
5
- module Case
6
- def self.camel_to_snake(camel)
7
- camel.to_s.gsub(/([a-z])([A-Z\d])/, '\1_\2').downcase
8
- end
9
-
10
- def self.snake_to_camel(snake)
11
- camel = snake.to_s.split('_').map(&:capitalize).join
12
- camel[0] = camel[0].downcase
13
- camel
14
- end
15
-
16
- def self.camel_to_kebab(camel)
17
- camel_to_snake(camel).tr('_', '-')
18
- end
19
- end
20
- end
21
- end
data/lib/utils/checks.rb DELETED
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require('date')
4
- require('starkbank-ecdsa')
5
- require_relative('environment')
6
- require_relative('../user/user')
7
-
8
- module StarkBank
9
- module Utils
10
- class Checks
11
- def self.check_user(user)
12
- return user if user.is_a?(StarkBank::User)
13
-
14
- user = user.nil? ? StarkBank.user : user
15
- raise(ArgumentError, 'A user is required to access our API. Check our README: https://github.com/starkbank/sdk-ruby/') if user.nil?
16
-
17
- user
18
- end
19
-
20
- def self.check_language
21
- language = StarkBank.language
22
- accepted_languages = %w[en-US pt-BR]
23
- raise(ArgumentError, "Select a valid language: #{accepted_languages.join(', ')}") unless accepted_languages.include?(language)
24
-
25
- language
26
- end
27
-
28
- def self.check_environment(environment)
29
- environments = StarkBank::Utils::Environment.constants(false).map { |c| StarkBank::Utils::Environment.const_get(c) }
30
- raise(ArgumentError, "Select a valid environment: #{environments.join(', ')}") unless environments.include?(environment)
31
-
32
- environment
33
- end
34
-
35
- def self.check_private_key(pem)
36
- EllipticCurve::PrivateKey.fromPem(pem)
37
- pem
38
- rescue
39
- raise(ArgumentError, 'Private-key must be a valid secp256k1 ECDSA string in pem format')
40
- end
41
-
42
- def self.check_date_or_datetime(data)
43
- return if data.nil?
44
-
45
- return data if data.is_a?(Time) || data.is_a?(DateTime)
46
-
47
- return data if data.is_a?(Date)
48
-
49
- data, type = check_datetime_string(data)
50
- type == 'date' ? Date.new(data.year, data.month, data.day) : data
51
- end
52
-
53
- def self.check_datetime(data)
54
- return if data.nil?
55
-
56
- return data if data.is_a?(Time) || data.is_a?(DateTime)
57
-
58
- return Time.new(data.year, data.month, data.day) if data.is_a?(Date)
59
-
60
- data, _type = check_datetime_string(data)
61
- data
62
- end
63
-
64
- def self.check_date(data)
65
- return if data.nil?
66
-
67
- return Date.new(data.year, data.month, data.day) if data.is_a?(Time) || data.is_a?(DateTime)
68
-
69
- return data if data.is_a?(Date)
70
-
71
- data, type = check_datetime_string(data)
72
-
73
- type == 'date' ? Date.new(data.year, data.month, data.day) : data
74
- end
75
-
76
- class << self
77
- private
78
-
79
- def check_datetime_string(data)
80
- data = data.to_s
81
-
82
- begin
83
- return [DateTime.strptime(data, '%Y-%m-%dT%H:%M:%S.%L+00:00'), 'datetime']
84
- rescue ArgumentError
85
- end
86
-
87
- begin
88
- return [DateTime.strptime(data, '%Y-%m-%dT%H:%M:%S+00:00'), 'datetime']
89
- rescue ArgumentError
90
- end
91
-
92
- begin
93
- return [DateTime.strptime(data, '%Y-%m-%d'), 'date']
94
- rescue ArgumentError
95
- raise(ArgumentError, 'invalid datetime string ' + data)
96
- end
97
- end
98
- end
99
- end
100
- end
101
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module StarkBank
4
- module Utils
5
- class Environment
6
- PRODUCTION = 'production'
7
- public_constant :PRODUCTION
8
-
9
- SANDBOX = 'sandbox'
10
- public_constant :SANDBOX
11
- end
12
- end
13
- end
data/lib/utils/request.rb DELETED
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require('json')
4
- require('starkbank-ecdsa')
5
- require('net/http')
6
- require_relative('url')
7
- require_relative('checks')
8
- require_relative('../error')
9
-
10
- module StarkBank
11
- module Utils
12
- module Request
13
- class Response
14
- attr_reader :status, :content
15
- def initialize(status, content)
16
- @status = status
17
- @content = content
18
- end
19
-
20
- def json
21
- JSON.parse(@content)
22
- end
23
- end
24
-
25
- def self.fetch(method:, path:, payload: nil, query: nil, user: nil)
26
- user = Checks.check_user(user)
27
- language = Checks.check_language
28
-
29
- base_url = {
30
- Environment::PRODUCTION => 'https://api.starkbank.com/',
31
- Environment::SANDBOX => 'https://sandbox.api.starkbank.com/'
32
- }[user.environment] + 'v2'
33
-
34
- url = "#{base_url}/#{path}#{StarkBank::Utils::URL.urlencode(query)}"
35
- uri = URI(url)
36
-
37
- access_time = Time.now.to_i
38
- body = payload.nil? ? '' : payload.to_json
39
- message = "#{user.access_id}:#{access_time}:#{body}"
40
- signature = EllipticCurve::Ecdsa.sign(message, user.private_key).toBase64
41
-
42
- case method
43
- when 'GET'
44
- req = Net::HTTP::Get.new(uri)
45
- when 'DELETE'
46
- req = Net::HTTP::Delete.new(uri)
47
- when 'POST'
48
- req = Net::HTTP::Post.new(uri)
49
- req.body = body
50
- when 'PATCH'
51
- req = Net::HTTP::Patch.new(uri)
52
- req.body = body
53
- when 'PUT'
54
- req = Net::HTTP::Put.new(uri)
55
- req.body = body
56
- else
57
- raise(ArgumentError, 'unknown HTTP method ' + method)
58
- end
59
-
60
- req['Access-Id'] = user.access_id
61
- req['Access-Time'] = access_time
62
- req['Access-Signature'] = signature
63
- req['Content-Type'] = 'application/json'
64
- req['User-Agent'] = "Ruby-#{RUBY_VERSION}-SDK-2.6.0"
65
- req['Accept-Language'] = language
66
-
67
- request = Net::HTTP.start(uri.hostname, use_ssl: true) { |http| http.request(req) }
68
-
69
- response = Response.new(Integer(request.code, 10), request.body)
70
-
71
- raise(StarkBank::Error::InternalServerError) if response.status == 500
72
- raise(StarkBank::Error::InputErrors, response.json['errors']) if response.status == 400
73
- raise(StarkBank::Error::UnknownError, response.content) unless response.status == 200
74
-
75
- response
76
- end
77
- end
78
- end
79
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
- require_relative("sub_resource")
3
-
4
- module StarkBank
5
- module Utils
6
- class Resource < StarkBank::Utils::SubResource
7
- attr_reader :id
8
- def initialize(id = nil)
9
- @id = id
10
- end
11
- end
12
- end
13
- end