starkinfra 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/resource')
4
+ require_relative('../utils/rest')
5
+ require_relative('../utils/checks')
6
+ require_relative('pixreversal')
7
+
8
+ module StarkInfra
9
+ class PixReversal
10
+ # # PixReversal::Log object
11
+ #
12
+ # Every time a PixReversal entity is modified, a corresponding PixReversal::Log
13
+ # is generated for the entity. This log is never generated by the
14
+ # user.
15
+ #
16
+ # ## Attributes:
17
+ # - id [string]: unique id returned when the log is created. ex: '5656565656565656'
18
+ # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
19
+ # - type [string]: type of the PixReversal event which triggered the log creation. ex: 'processing' or 'success'
20
+ # - reversal [PixReversal]: PixReversal entity to which the log refers to.
21
+ # - errors [list of strings]: list of errors linked to this PixReversal event.
22
+ class Log < StarkInfra::Utils::Resource
23
+ attr_reader :id, :created, :type, :errors, :reversal
24
+ def initialize(id:, created:, type:, errors:, reversal:)
25
+ super(id)
26
+ @type = type
27
+ @errors = errors
28
+ @reversal = reversal
29
+ @created = StarkInfra::Utils::Checks.check_datetime(created)
30
+ end
31
+
32
+ # # Retrieve a specific Log
33
+ #
34
+ # Receive a single Log object previously created by the Stark Infra API by passing its id
35
+ #
36
+ # ## Parameters (required):
37
+ # - id [string]: object unique id. ex: '5656565656565656'
38
+ #
39
+ # ## Parameters (optional):
40
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
41
+ #
42
+ # ## Return:
43
+ # - Log object with updated attributes
44
+ def self.get(id, user: nil)
45
+ StarkInfra::Utils::Rest.get_id(id: id, user: user, **resource)
46
+ end
47
+
48
+ # # Retrieve Logs
49
+ #
50
+ # Receive a generator of Log objects previously created in the Stark Infra API
51
+ #
52
+ # ## Parameters (optional):
53
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
54
+ # - after [Date, DateTime, Time or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
55
+ # - before [Date, DateTime, Time or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
56
+ # - types [list of strings, default nil]: filter retrieved objects by types. ex: 'success' or 'failed'
57
+ # - reversal_ids [list of strings, default nil]: list of PixReversal ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
58
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
59
+ #
60
+ # ## Return:
61
+ # - list of Log objects with updated attributes
62
+ def self.query(limit: nil, after: nil, before: nil, types: nil, reversal_ids: nil, user: nil)
63
+ after = StarkInfra::Utils::Checks.check_date(after)
64
+ before = StarkInfra::Utils::Checks.check_date(before)
65
+ StarkInfra::Utils::Rest.get_stream(
66
+ limit: limit,
67
+ after: after,
68
+ before: before,
69
+ types: types,
70
+ reversal_ids: reversal_ids,
71
+ user: user,
72
+ **resource
73
+ )
74
+ end
75
+
76
+ # # Retrieve paged Logs
77
+ #
78
+ # Receive a list of up to 100 Log objects previously created in the Stark Infra API and the cursor to the next page.
79
+ # Use this function instead of query if you want to manually page your requests.
80
+ #
81
+ # ## Parameters (optional):
82
+ # - cursor [string, default nil]: cursor returned on the previous page function call
83
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
84
+ # - after [Date, DateTime, Time or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
85
+ # - before [Date, DateTime, Time or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
86
+ # - types [list of strings, default nil]: filter retrieved objects by types. ex: 'success' or 'failed'
87
+ # - reversal_ids [list of strings, default nil]: list of PixReversal ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
88
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
89
+ #
90
+ # ## Return:
91
+ # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects
92
+ def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, reversal_ids: nil, user: nil)
93
+ after = StarkInfra::Utils::Checks.check_date(after)
94
+ before = StarkInfra::Utils::Checks.check_date(before)
95
+ return StarkInfra::Utils::Rest.get_page(
96
+ cursor: cursor,
97
+ limit: limit,
98
+ after: after,
99
+ before: before,
100
+ types: types,
101
+ reversal_ids: reversal_ids,
102
+ user: user,
103
+ **resource
104
+ )
105
+ end
106
+
107
+ def self.resource
108
+ reversal_maker = StarkInfra::PixReversal.resource[:resource_maker]
109
+ {
110
+ resource_name: 'PixReversalLog',
111
+ resource_maker: proc { |json|
112
+ Log.new(
113
+ id: json['id'],
114
+ created: json['created'],
115
+ type: json['type'],
116
+ errors: json['errors'],
117
+ reversal: StarkInfra::Utils::API.from_api_json(reversal_maker, json['reversal'])
118
+ )
119
+ }
120
+ }
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,203 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/resource')
4
+ require_relative('../utils/rest')
5
+ require_relative('../utils/checks')
6
+ require_relative('../utils/parse')
7
+
8
+
9
+ module StarkInfra
10
+ # # PixReversal object
11
+ #
12
+ # When you initialize a PixReversal, the entity will not be automatically
13
+ # created in the Stark Infra API. The 'create' function sends the objects
14
+ # to the Stark Infra API and returns the list of created objects.
15
+ #
16
+ # ## Parameters (required):
17
+ # - amount [integer]: amount in cents to be reversed from PixRequest. ex: 1234 (= R$ 12.34)
18
+ # - external_id [string]: url safe string that must be unique among all your PixReversals. Duplicated external ids will cause failures. By default, this parameter will block any PixReversal that repeats amount and receiver information on the same date. ex: "my-internal-id-123456"
19
+ # - end_to_end_id [string]: central bank's unique transaction ID. ex: "E79457883202101262140HHX553UPqeq"
20
+ # - reason [string]: reason why the PixRequest is being reversed. Options are "bankError", "fraud", "pixWithdrawError", "refund3ByEndCustomer"
21
+ #
22
+ # ## Parameters (optional):
23
+ # - tags [string, default nill]: [list of strings]: list of strings for reference when searching for PixReversals. ex: ["employees", "monthly"]
24
+ #
25
+ # ## Attributes (return-only):
26
+ # - id [string, default nil]: unique id returned when the PixReversal is created. ex: "5656565656565656".
27
+ # - return_id [string]: central bank's unique reversal transaction ID. ex: "D20018183202202030109X3OoBHG74wo".
28
+ # - bank_code [string]: code of the bank institution in Brazil. ex: "20018183" or "341"
29
+ # - fee [string]: fee charged by this PixReversal. ex: 200 (= R$ 2.00)
30
+ # - status [string]: current PixReversal status. ex: "registered" or "paid"
31
+ # - flow [string]: direction of money flow. ex: "in" or "out"
32
+ # - created [Datetime, default nil]: creation datetime for the PixReversal. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
33
+ # - updated [Datetime, default nil]: latest update datetime for the PixReversal. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
34
+
35
+ class PixReversal < StarkInfra::Utils::Resource;
36
+ attr_reader :amount, :external_id, :end_to_end_id, :reason, :tags, :id, :return_id, :bank_code, :fee, :status, :flow, :created, :updated
37
+ def initialize(
38
+ amount:, external_id:, end_to_end_id:, reason:, tags: nil, id: nil, return_id: nil, bank_code: nil, fee: nil,
39
+ status: nil, flow: nil, created: nil, updated: nil
40
+ )
41
+ created = StarkInfra::Utils::Checks.check_datetime(created)
42
+ updated = StarkInfra::Utils::Checks.check_datetime(updated)
43
+ super(id)
44
+ @amount = amount
45
+ @external_id = external_id
46
+ @end_to_end_id = end_to_end_id
47
+ @reason = reason
48
+ @tags = tags
49
+ @return_id = return_id
50
+ @bank_code = bank_code
51
+ @fee = fee
52
+ @staus = status
53
+ @flow = flow
54
+ @created = created
55
+ @updated = updated
56
+ end
57
+
58
+ # # Create PixRversals
59
+ #
60
+ # Send a list of PixRversal objects for creation in the Stark Infra API
61
+ #
62
+ # ## Parameters (required):
63
+ # - reversals [list of PixRversal objects]: list of PixRversal objects to be created in the API
64
+ #
65
+ # ## Parameters (optional):
66
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
67
+ #
68
+ # ## Return:
69
+ # - list of PixRversal objects with updated attributes
70
+ def self.create(reversals, user: nil)
71
+ StarkInfra::Utils::Rest.post(entities: reversals, user: user, **resource)
72
+ end
73
+
74
+ # # Retrieve a specific PixRversal
75
+ #
76
+ # Receive a single PixRversal object previously created in the Stark Bank API by passing its id
77
+ #
78
+ # ## Parameters (required):
79
+ # - id [string]: object unique id. ex: '5656565656565656'
80
+ #
81
+ # ## Parameters (optional):
82
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
83
+ #
84
+ # ## Return:
85
+ # - PixRversal object with updated attributes
86
+ def self.get(id, user: nil)
87
+ StarkInfra::Utils::Rest.get_id(id: id, user: user, **resource)
88
+ end
89
+
90
+ # # Retrieve PixRversals
91
+ #
92
+ # Receive a generator of PixRversal objects previously created in the Stark Infra API
93
+ #
94
+ # ## Parameters (optional):
95
+ # - fields [list of strings, default None]: parameters to be retrieved from PixRequest objects. ex: ["amount", "id"]
96
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
97
+ # - after [Date, DateTime, Time or string, default nil]: date filter for objects created or updated only after specified date. ex: Date.new(2020, 3, 10)
98
+ # - before [Date, DateTime, Time or string, default nil]: date filter for objects created or updated only before specified date. ex: Date.new(2020, 3, 10)
99
+ # - status [string, default nil]: filter for status of retrieved objects. ex: 'success' or 'failed'
100
+ # - tags [list of strings, default nil]: tags to filter retrieved objects. ex: ['tony', 'stark']
101
+ # - ids [list of strings, default nil]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
102
+ # - return_ids [list of strings, default None]: central bank's unique reversal transaction ID. ex: ["D20018183202202030109X3OoBHG74wo", "D20018183202202030109X3OoBHG72rd"].
103
+ # - external_ids [list of strings, default None]: url safe string that must be unique among all your PixReversals. Duplicated external IDs will cause failures. By default, this parameter will block any PixReversal that repeats amount and receiver information on the same date. ex: ["my-internal-id-123456", "my-internal-id-654321"]
104
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
105
+ #
106
+ # ## Return:
107
+ # - generator of PixRversal objects with updated attributes
108
+ def self.query(fields:nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, return_ids: nil, external_ids: nil, user: nil)
109
+ StarkInfra::Utils::Rest.get_stream(
110
+ fields: fields,
111
+ limit: limit,
112
+ after: StarkInfra::Utils::Checks.check_date(after),
113
+ before: StarkInfra::Utils::Checks.check_date(before),
114
+ status: status,
115
+ tags: tags,
116
+ ids: ids,
117
+ return_ids: return_ids,
118
+ external_ids: external_ids,
119
+ user: user,
120
+ **resource
121
+ )
122
+ end
123
+
124
+ # # Retrieve paged PixRversals
125
+ #
126
+ # Receive a list of up to 100 PixRversal objects previously created in the Stark Infra API and the cursor to the next page.
127
+ # Use this function instead of query if you want to manually page your requests.
128
+ #
129
+ # ## Parameters (optional):
130
+ # - cursor [string, default nil]: cursor returned on the previous page function call
131
+ # - fields [list of strings, default None]: parameters to be retrieved from PixRequest objects. ex: ["amount", "id"]
132
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
133
+ # - after [Date, DateTime, Time or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
134
+ # - before [Date, DateTime, Time or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
135
+ # - status [string, default nil]: filter for status of retrieved objects. ex: 'paid' or 'registered'
136
+ # - tags [list of strings, default nil]: tags to filter retrieved objects. ex: ['tony', 'stark']
137
+ # - ids [list of strings, default nil]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
138
+ # - return_ids [list of strings, default None]: central bank's unique reversal transaction ID. ex: ["D20018183202202030109X3OoBHG74wo", "D20018183202202030109X3OoBHG72rd"].
139
+ # - external_ids [list of strings, default None]: url safe string that must be unique among all your PixReversals. Duplicated external IDs will cause failures. By default, this parameter will block any PixReversal that repeats amount and receiver information on the same date. ex: ["my-internal-id-123456", "my-internal-id-654321"]
140
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
141
+ #
142
+ # ## Return:
143
+ # - list of PixRversal objects with updated attributes and cursor to retrieve the next page of PixRversal objects
144
+ def self.page(cursor: nil, fields:nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, return_ids: nil, external_ids: nil, user: nil)
145
+ return StarkInfra::Utils::Rest.get_page(
146
+ fields: fields,
147
+ cursor: cursor,
148
+ limit: limit,
149
+ after: StarkInfra::Utils::Checks.check_date(after),
150
+ before: StarkInfra::Utils::Checks.check_date(before),
151
+ status: status,
152
+ tags: tags,
153
+ ids: ids,
154
+ return_ids: return_ids,
155
+ external_ids: external_ids,
156
+ user: user,
157
+ **resource
158
+ )
159
+ end
160
+
161
+ def self.parse(content:, signature:, user: nil)
162
+ # # Create single verified PixReversal object from a content string
163
+ #
164
+ # Create a single PixReversal object from a content string received from a handler listening at a subscribed user endpoint.
165
+ # If the provided digital signature does not check out with the StarkInfra public key, a
166
+ # starkinfra.exception.InvalidSignatureException will be raised.
167
+ #
168
+ # ## Parameters (required):
169
+ # - content [string]: response content from revrsal received at user endpoint (not parsed)
170
+ # - signature [string]: base-64 digital signature received at response header "Digital-Signature"
171
+ #
172
+ # ## Parameters (optional):
173
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkinfra.user was set before function call
174
+ #
175
+ # ## Return:
176
+ # - Parsed PixReversal object
177
+ return StarkInfra::Utils::Parse.parse_and_verify(content: content, signature: signature, user: user, resource: resource)
178
+ end
179
+
180
+ def self.resource
181
+ {
182
+ resource_name: 'PixReversal',
183
+ resource_maker: proc { |json|
184
+ PixReversal.new(
185
+ amount: json['amount'],
186
+ external_id: json['external_id'],
187
+ end_to_end_id: json['end_to_end_id'],
188
+ reason: json['reason'],
189
+ tags: json['tags'],
190
+ id: json['id'],
191
+ return_id: json['return_id'],
192
+ bank_code: json['bank_code'],
193
+ fee: json['fee'],
194
+ status: json['status'],
195
+ flow: json['flow'],
196
+ created: json['created'],
197
+ updated: json['updated'],
198
+ )
199
+ }
200
+ }
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/resource')
4
+ require_relative('../utils/rest')
5
+ require_relative('../utils/checks')
6
+
7
+ module StarkInfra
8
+ # # PixStatement object
9
+ #
10
+ # The PixStatement object stores information about all the transactions that happened on
11
+ # a specific day at the workspace. It must be created by the user before it can be
12
+ # accessed by the user. This feature is only available for direct participants.
13
+ #
14
+ # ## Parameters (required):
15
+ # - after [Date, DateTime, Time or string]: transactions that happened at this date are stored in the PixStatement, must be the same as before. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
16
+ # - before [Date, DateTime, Time or string]: transactions that happened at this date are stored in the PixStatement, must be the same as after. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
17
+ # - type [string]: types of entities to include in statement. Options: ["interchange", "interchangeTotal", "transaction"]
18
+ #
19
+ # ## Attributes (return-only):
20
+ # - id [string, default nil]: unique id returned when the PixStatement is created. ex: "5656565656565656"
21
+ # - status [string, default nil]: current PixStatement status. ex: "success" or "failed"
22
+ # - transaction_count [integer]: number of transactions that happened during the day that the PixStatement was requested. ex 11
23
+ # - created [Datetime, default nil]: creation datetime for the PixStatement. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
24
+ # - updated [Datetime, default nil]: latest update datetime for the PixStatement. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
25
+
26
+ class PixStatement < StarkInfra::Utils::Resource
27
+ attr_reader :after, :before, :type, :id, :status, :transaction_count, :created, :updated
28
+ def initialize(after:, before:, type:, id: nil, status: nil, transaction_count: nil, created: nil, updated: nil)
29
+ super(id)
30
+ @after = StarkInfra::Utils::Checks.check_date(after)
31
+ @before = StarkInfra::Utils::Checks.check_date(before)
32
+ @type = type
33
+ @status = status
34
+ @transaction_count = transaction_count
35
+ @created = StarkInfra::Utils::Checks.check_datetime(created)
36
+ @updated = StarkInfra::Utils::Checks.check_datetime(updated)
37
+ end
38
+
39
+ # # Create a PixStatement object
40
+ #
41
+ # Create a PixStatements linked to your workspace in the Stark Infra API
42
+ #
43
+ # ## Parameters (requiered):
44
+ # - statement [PixStatement object]: PixStatement object to be created in the API.
45
+ #
46
+ # ## Parameters (optional):
47
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkinfra.user was set before function call
48
+ #
49
+ # ## Return:
50
+ # - PixStatement object with updated attributes.
51
+ def self.create(statement, user: nil)
52
+ StarkInfra::Utils::Rest.post_single(entity: statement, user: user, **resource)
53
+ end
54
+
55
+ # # Retrieve a specific PixStatment object
56
+ #
57
+ # Receive a single PixStatment object previously created in the Stark Infra API by passing its id
58
+ #
59
+ # ## Parameters (required):
60
+ # - id [string]: object unique id. ex: '5656565656565656'
61
+ #
62
+ # ## Parameters (optional):
63
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
64
+ #
65
+ # ## Return:
66
+ # - PixStatment object with updated attributes
67
+ def self.get(id, user: nil)
68
+ StarkInfra::Utils::Rest.get_id(id: id, user: user, **resource)
69
+ end
70
+
71
+ # # Retrieve PixStatement objects
72
+ #
73
+ # Receive a generator of PixStatements objects previously created in the Stark Infra API.
74
+ #
75
+ # ## Parameters (optional):
76
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
77
+ # - ids [list of strings, default nil]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
78
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
79
+ #
80
+ # ## Return:
81
+ # - generator of PixStatement objects with updated attributes
82
+ def self.query(limit: nil, ids: nil, user: nil)
83
+ StarkInfra::Utils::Rest.get_stream(
84
+ limit: limit,
85
+ ids: ids,
86
+ user: user,
87
+ **resource
88
+ )
89
+ end
90
+
91
+ # # Retrieve paged PixStatements
92
+ #
93
+ # Receive a list of up to 100 PixStatements objects previously created in the Stark infra API and the cursor to the next page.
94
+ # Use this function instead of query if you want to manually page your requests.
95
+ #
96
+ # ## Parameters (optional):
97
+ # - cursor [string, default nil]: cursor returned on the previous page function call
98
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
99
+ # - ids [list of strings, default nil]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
100
+ # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
101
+ #
102
+ # ## Return:
103
+ # - list of PixStatement objects with updated attributes
104
+ # - Cursor to retrieve the next page of PixStatement objects
105
+ def self.page(cursor: nil, limit: nil, ids: nil, user: nil)
106
+ return StarkInfra::Utils::Rest.get_page(
107
+ cursor: cursor,
108
+ limit: limit,
109
+ ids: ids,
110
+ user: user,
111
+ **resource
112
+ )
113
+ end
114
+
115
+ # # # Retrieve a .cvs PixStatement
116
+ #
117
+ # Retrieve a specific PixStatement by its ID in a .csv file.
118
+ #
119
+ # ## Parameters (required):
120
+ # - id [string]: object unique id. ex: "5656565656565656"
121
+ #
122
+ # ## Parameters (optional):
123
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkinfra.user was set before function call
124
+ #
125
+ # ## Return:
126
+ # - PixStatement .csv file
127
+ def self.csv(id, user: nil)
128
+ return StarkInfra::Utils::Rest.get_content(id: id, user: user, sub_resource_name: "csv", **resource)
129
+ end
130
+
131
+ def self.resource
132
+ {
133
+ resource_name: 'PixStatement',
134
+ resource_maker: proc { |json|
135
+ PixStatement.new(
136
+ after: json['after'],
137
+ before: json['before'],
138
+ type: json['type'],
139
+ id: json['id'],
140
+ status: json['status'],
141
+ transaction_count: json['transaction_count'],
142
+ created: json['created'],
143
+ updated: json['updated'],
144
+ )
145
+ }
146
+ }
147
+ end
148
+ end
149
+ end
data/lib/starkinfra.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('starkbank')
4
+ require_relative('key')
5
+ require_relative('workspace/workspace')
6
+ require_relative('pixrequest/pixrequest')
7
+ require_relative('pixrequest/log')
8
+ require_relative('pixreversal/pixreversal')
9
+ require_relative('pixreversal/log')
10
+ require_relative('pixbalance/pixbalance')
11
+ require_relative('pixstatement/pixstatement')
12
+ require_relative('event/event')
13
+
14
+ # SDK to facilitate Ruby integrations with Stark Infra
15
+ module StarkInfra
16
+ include StarkBank
17
+ @user = nil
18
+ @language = 'en-US'
19
+ class << self; attr_accessor :user, :language; end
20
+ end
data/lib/utils/api.rb ADDED
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('case')
4
+
5
+ module StarkInfra
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?(StarkInfra::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[StarkInfra::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[StarkInfra::Utils::Case.camel_to_snake(key)] = value
53
+ end
54
+
55
+ resource_maker.call(snakes)
56
+ end
57
+
58
+ def self.endpoint(resource_name)
59
+ kebab = StarkInfra::Utils::Case.camel_to_kebab(resource_name)
60
+ kebab.sub!('-log', '/log')
61
+ kebab.sub!('-attempt', '/attempt')
62
+ kebab
63
+ end
64
+
65
+ def self.last_name_plural(resource_name)
66
+ base = last_name(resource_name)
67
+
68
+ return base if base[-1].eql?('s')
69
+ return "#{base}s" if base[-2..-1].eql?('ey')
70
+ return "#{base[0...-1]}ies" if base[-1].eql?('y')
71
+
72
+ "#{base}s"
73
+ end
74
+
75
+ def self.last_name(resource_name)
76
+ StarkInfra::Utils::Case.camel_to_kebab(resource_name).split('-').last
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StarkInfra
4
+ module Utils
5
+ module Cache
6
+ @starkinfra_public_key = nil
7
+ class << self; attr_accessor :starkinfra_public_key; end
8
+ end
9
+ end
10
+ end
data/lib/utils/case.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StarkInfra
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