synapse_api 1.0.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.
@@ -0,0 +1,21 @@
1
+ module Synapse
2
+
3
+ class Transaction
4
+
5
+
6
+ attr_accessor :trans_id, :payload, :node_id, :user
7
+
8
+ def initialize(trans_id:, payload:, node_id:nil, user:nil)
9
+ @trans_id = trans_id
10
+ @payload = payload
11
+ @node_id = node_id
12
+ @user = user
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
@@ -0,0 +1,19 @@
1
+ module Synapse
2
+
3
+ class Transactions
4
+
5
+ attr_reader :page, :page_count, :limit, :payload, :trans_count
6
+
7
+ attr_accessor
8
+
9
+ def initialize(page:,limit:, trans_count:, payload:, page_count:)
10
+ @page = page
11
+ @limit = limit
12
+ @trans_count = trans_count
13
+ @payload = payload
14
+ @page_count = page_count
15
+ end
16
+ end
17
+ end
18
+
19
+
@@ -0,0 +1,823 @@
1
+ module Synapse
2
+ # Wrapper class for /users endpoints
3
+ class User
4
+
5
+ # Valid optional args for #get
6
+ VALID_QUERY_PARAMS = [:query, :page, :per_page, :type, :full_dehydrate, :ship, :force_refresh, :is_credit,
7
+ :subnetid, :foreign_transaction,:amount].freeze
8
+
9
+ attr_accessor :client, :user_id,:refresh_token, :oauth_key, :expires_in, :payload, :full_dehydrate
10
+
11
+ # @param user_id [String]
12
+ # @param refresh_token [String]
13
+ # @param client [Synapse::HTTPClient]
14
+ # @param payload [Hash]
15
+ # @param full_dehydrate [Boolean]
16
+ def initialize(user_id:,refresh_token:, client:,payload:, full_dehydrate:)
17
+ @user_id = user_id
18
+ @client = client
19
+ @refresh_token = refresh_token
20
+ @payload =payload
21
+ @full_dehydrate =full_dehydrate
22
+ end
23
+
24
+ # Updates users documents
25
+ # @see https://docs.synapsefi.com/docs/updating-existing-document
26
+ # @param payload [Hash]
27
+ # @return [Synapse::User]
28
+ def user_update(payload:)
29
+ path = get_user_path(user_id: self.user_id)
30
+ begin
31
+ response = client.patch(path, payload)
32
+ rescue Synapse::Error::Unauthorized
33
+ self.authenticate()
34
+ response =client.patch(path, payload)
35
+ end
36
+ User.new(user_id: response['_id'],
37
+ refresh_token: response['refresh_token'],
38
+ client: client,
39
+ full_dehydrate: false,
40
+ payload: response
41
+ )
42
+ end
43
+
44
+ # Queries the API for a node belonging to user
45
+ # @param node_id [String]
46
+ # @param full_dehydrate [String] (optional)
47
+ # if true, returns all trans data on node
48
+ # @param force_refresh [String] (optional) if true, force refresh
49
+ # will attempt updating the account balance and transactions on node
50
+ # @return [Synapse::Node]
51
+ def get_user_node(node_id:, **options)
52
+ options[:full_dehydrate] = "yes" if options[:full_dehydrate] == true
53
+ options[:full_dehydrate] = "no" if options[:full_dehydrate] == false
54
+ options[:force_refresh] = "yes" if options[:force_refresh] == true
55
+ options[:force_refresh] = "no" if options[:force_refresh] == false
56
+
57
+ path = node(node_id: node_id,
58
+ full_dehydrate: options[:full_dehydrate],
59
+ force_refresh: options[:force_refresh] )
60
+
61
+ begin
62
+ node = client.get(path)
63
+ rescue Synapse::Error::Unauthorized
64
+ self.authenticate()
65
+ node = client.get(path)
66
+ end
67
+
68
+ node = Node.new(node_id: node['_id'],
69
+ user_id: self.user_id,
70
+ payload: node,
71
+ full_dehydrate: options[:full_dehydrate] == "yes" ? true : false,
72
+ type: node["type"]
73
+ )
74
+ end
75
+
76
+ # Queries Synapse API for all nodes belonging to user
77
+ # @param page [String,Integer] (optional) response will default to 1
78
+ # @param per_page [String,Integer] (optional) response will default to 20
79
+ # @param type [String] (optional)
80
+ # @see https://docs.synapsepay.com/docs/node-resources node types
81
+ # @return [Array<Synapse::Nodes>]
82
+ def get_all_user_nodes(**options)
83
+ [options[:page], options[:per_page]].each do |arg|
84
+ if arg && (!arg.is_a?(Integer) || arg < 1)
85
+ raise ArgumentError, "#{arg} must be nil or an Integer >= 1"
86
+ end
87
+ end
88
+ path = get_user_path(user_id: self.user_id) + nodes_path(options)
89
+
90
+ begin
91
+ nodes = client.get(path)
92
+ rescue Synapse::Error::Unauthorized
93
+ self.authenticate()
94
+ nodes = client.get(path)
95
+ end
96
+
97
+ return [] if nodes["nodes"].empty?
98
+ response = nodes["nodes"].map { |node_data| Node.new(node_id: node_data['_id'],
99
+ user_id: node_data['user_id'],
100
+ payload: node_data, full_dehydrate: "no",
101
+ type: node_data["type"])}
102
+ nodes = Nodes.new(limit: nodes["limit"],
103
+ page: nodes["page"],
104
+ page_count: nodes["page_count"],
105
+ nodes_count: nodes["node_count"],
106
+ payload: response)
107
+ end
108
+
109
+ # Quaries Synapse oauth API for uto authenitcate user
110
+ # @params scope [Array<Strings>] (optional)
111
+ # @param idempotency_key [String] (optional)
112
+ # @see https://docs.synapsefi.com/docs/get-oauth_key-refresh-token
113
+ def authenticate(**options)
114
+ payload = {
115
+ "refresh_token" => self.refresh_token
116
+ }
117
+ payload["scope"] = options[:scope] if options[:scope]
118
+
119
+ path = oauth_path()
120
+
121
+ oauth_response = client.post(path, payload,options)
122
+ oauth_key = oauth_response['oauth_key']
123
+ oauth_expires = oauth_response['expires_in']
124
+ self.oauth_key = oauth_key
125
+ self.expires_in = oauth_expires
126
+ client.update_headers(oauth_key: oauth_key)
127
+
128
+ oauth_response
129
+ end
130
+
131
+ # For registering new fingerprint
132
+ # Supply 2FA device which pin should be sent to
133
+ # @param device [String]
134
+ # @param idempotency_key [String] (optional)
135
+ # @see https://docs.synapsefi.com/docs/get-oauth_key-refresh-token
136
+ # @return API response [Hash]
137
+ def select_2fa_device(device:, **options)
138
+ payload = {
139
+ "refresh_token": self.refresh_token,
140
+ "phone_number": device
141
+ }
142
+ path = oauth_path()
143
+ device_response = client.post(path, payload, options)
144
+ device_response
145
+ end
146
+
147
+ # Supply pin for 2FA confirmation
148
+ # @param pin [String]
149
+ # @param idempotency_key [String] (optional)
150
+ # @param scope [Array] (optional)
151
+ # @see https://docs.synapsefi.com/docs/get-oauth_key-refresh-token
152
+ # @return API response [Hash]
153
+ def confirm_2fa_pin(pin:, **options)
154
+ payload = {
155
+ "refresh_token": self.refresh_token,
156
+ "validation_pin": pin
157
+ }
158
+
159
+ payload["scope"] = options[:scope] if options[:scope]
160
+
161
+ path = oauth_path()
162
+
163
+ pin_response = client.post(path, payload, options)
164
+ oauth_key = pin_response['oauth_key']
165
+ oauth_expires = pin_response['expires_in']
166
+ self.oauth_key = oauth_key
167
+ self.expires_in = oauth_expires
168
+ client.update_headers(oauth_key: oauth_key)
169
+
170
+ pin_response
171
+ end
172
+
173
+ # Queries the Synapse API to get all transactions belonging to a user
174
+ # @return [Array<Synapse::Transactions>]
175
+ # @param page [Integer] (optional) response will default to 1
176
+ # @param per_page [Integer] (optional) response will default to 20
177
+ def get_user_transactions(**options)
178
+ [options[:page], options[:per_page]].each do |arg|
179
+ if arg && (!arg.is_a?(Integer) || arg < 1)
180
+ raise ArgumentError, "#{arg} must be nil or an Integer >= 1"
181
+ end
182
+ end
183
+
184
+ path = transactions_path(user_id: self.user_id, options: options)
185
+
186
+ begin
187
+ trans = client.get(path)
188
+ rescue Synapse::Error::Unauthorized
189
+ self.authenticate()
190
+ trans = client.get(path)
191
+ end
192
+
193
+
194
+ response = trans["trans"].map { |trans_data| Transaction.new(trans_id: trans_data['_id'],
195
+ payload: trans_data
196
+ )}
197
+ trans = Transactions.new(limit: trans["limit"],
198
+ page: trans["page"],
199
+ page_count: trans["page_count"],
200
+ trans_count: trans["trans_count"],
201
+ payload: response
202
+ )
203
+
204
+ trans
205
+ end
206
+
207
+ # Creates Synapse node
208
+ # @note Types of nodes [Card, IB/Deposit-US, Check/Wire Instructions]
209
+ # @param payload [Hash]
210
+ # @param idempotency_key [String] (optional)
211
+ # @see https://docs.synapsefi.com/docs/node-resources
212
+ # @return [Synapse::Node] or [Hash]
213
+ def create_node(payload:, **options)
214
+ path = get_user_path(user_id: self.user_id)
215
+ path = path + nodes_path
216
+
217
+ begin
218
+ response = client.post(path,payload, options)
219
+ rescue Synapse::Error::Unauthorized
220
+ self.authenticate()
221
+ response = client.post(path,payload, options)
222
+ end
223
+
224
+ if response["nodes"]
225
+ nodes = response["nodes"].map { |nodes_data| Node.new(user_id: self.user_id,
226
+ node_id: nodes_data["_id"],
227
+ full_dehydrate: false,
228
+ payload: response,
229
+ type: nodes_data["type"]
230
+ )}
231
+ nodes = Nodes.new(page: response["page"],
232
+ limit: response["limit"],
233
+ page_count: response["page_count"],
234
+ nodes_count: response["node_count"],
235
+ payload: nodes
236
+ )
237
+ else
238
+ access_token = response
239
+ end
240
+ access_token ? access_token : nodes
241
+ end
242
+
243
+ # Submit answer to a MFA question using access token from bank login attempt
244
+ # @return [Synapse::Node] or [Hash]
245
+ # @param payload [Hash]
246
+ # @param idempotency_key [String] (optional)
247
+ # @see https://docs.synapsefi.com/docs/add-ach-us-node-via-bank-logins-mfa
248
+ # Please be sure to call ach_mfa again if you have more security questions
249
+ def ach_mfa(payload:, **options)
250
+ path = get_user_path(user_id: self.user_id)
251
+ path = path + nodes_path
252
+
253
+ begin
254
+ response = client.post(path,payload, options)
255
+ rescue Synapse::Error::Unauthorized
256
+ self.authenticate()
257
+ response = client.post(path,payload, options)
258
+ end
259
+
260
+ if response["nodes"]
261
+ nodes = response["nodes"].map { |nodes_data| Node.new(user_id: self.user_id,
262
+ node_id: nodes_data["_id"],
263
+ full_dehydrate: false,
264
+ payload: response,
265
+ type: nodes_data["type"]
266
+ )}
267
+ nodes = Nodes.new(page: response["page"],
268
+ limit: response["limit"],
269
+ page_count: response["page_count"],
270
+ nodes_count: response["node_count"],
271
+ payload: nodes
272
+ )
273
+ else
274
+ access_token = response
275
+ end
276
+ access_token ? access_token : nodes
277
+ end
278
+
279
+ # Allows you to upload an Ultimate Beneficial Ownership document
280
+ # @param payload [Hash]
281
+ # @see https://docs.synapsefi.com/docs/generate-ubo-form
282
+ # @return API response
283
+ def create_ubo(payload:)
284
+ path = get_user_path(user_id: self.user_id)
285
+ path = path + "/ubo"
286
+
287
+ begin
288
+ response = client.patch(path,payload)
289
+ rescue Synapse::Error::Unauthorized
290
+ self.authenticate()
291
+ response = client.patch(path,payload)
292
+ end
293
+ response
294
+ end
295
+
296
+ # Gets user statement
297
+ # @param page [Integer]
298
+ # @param per_page [Integer]
299
+ # @see https://docs.synapsefi.com/docs/statements-by-user
300
+ # @return API response
301
+ def get_user_statement(**options)
302
+ path = get_user_path(user_id: self.user_id) + "/statements"
303
+ params = VALID_QUERY_PARAMS.map do |p|
304
+ options[p] ? "#{p}=#{options[p]}" : nil
305
+ end.compact
306
+
307
+ path += '?' + params.join('&') if params.any?
308
+
309
+ begin
310
+ statements = client.get(path)
311
+ rescue Synapse::Error::Unauthorized
312
+ self.authenticate()
313
+ statements = client.get(path)
314
+ end
315
+ statements
316
+ end
317
+
318
+ # Request to ship CARD-US
319
+ # @note Deprecated
320
+ # @param node_id [String]
321
+ # @param payload [Hash]
322
+ # @return [Synapse::Node] or [Hash]
323
+ def ship_card_node(node_id:, payload:)
324
+ path = node(user_id: self.user_id, node_id: node_id) + "?ship=YES"
325
+ begin
326
+ response = client.patch(path,payload)
327
+ rescue Synapse::Error::Unauthorized
328
+ self.authenticate()
329
+ response = client.patch(path,payload)
330
+ end
331
+ Node.new(user_id: self.user_id,
332
+ node_id: response["_id"],
333
+ full_dehydrate: false,
334
+ payload: response,
335
+ type: response["type"])
336
+ end
337
+
338
+ # Request to ship user debit card [Subnet]
339
+ # @param node_id [String]
340
+ # @param payload [Hash]
341
+ # @param subnet_id [String]
342
+ # @return [Synapse::Node] or [Hash]
343
+ def ship_card(node_id:, payload:, subnet_id:)
344
+ path = node(user_id: self.user_id, node_id: node_id) + "/subnets/#{subnet_id}/ship"
345
+
346
+ begin
347
+ response = client.patch(path,payload)
348
+ rescue Synapse::Error::Unauthorized
349
+ self.authenticate()
350
+ response = client.patch(path,payload)
351
+ end
352
+ Subnet.new(subnet_id: response["subnet_id"], payload: response, node_id: response["node_id"])
353
+ end
354
+
355
+ # Resets debit card number, cvv, and expiration date
356
+ # @note Deprecated
357
+ # @see https://docs.synapsefi.com/docs/reset-debit-card
358
+ # @param node_id [String]
359
+ # @return [Synapse::Node] or [Hash]
360
+ def reset_card_node(node_id:)
361
+ path = node(user_id: self.user_id, node_id: node_id) + "?reset=YES"
362
+ payload = {}
363
+ begin
364
+ response = client.patch(path,payload)
365
+ rescue Synapse::Error::Unauthorized
366
+ self.authenticate()
367
+ response = client.patch(path,payload)
368
+ end
369
+ Node.new(user_id: self.user_id,
370
+ node_id:response["_id"],
371
+ full_dehydrate: false,
372
+ payload: response,
373
+ type: response["type"]
374
+ )
375
+ end
376
+
377
+ # Creates a new transaction in the API belonging to the provided node
378
+ # @param node_id [String]
379
+ # @param payload [Hash]
380
+ # @param idempotency_key [String] (optional)
381
+ # @return [Synapse::Transaction]
382
+ def create_transaction(node_id: ,payload:, **options)
383
+ path = trans_path(user_id: self.user_id, node_id: node_id)
384
+ begin
385
+ transaction = client.post(path,payload, options)
386
+ rescue Synapse::Error::Unauthorized
387
+ self.authenticate()
388
+ transaction = client.post(path,payload, options)
389
+ end
390
+ transaction = Transaction.new(trans_id: transaction['_id'],
391
+ payload: transaction,
392
+ node_id: node_id
393
+ )
394
+ end
395
+
396
+ # Queries the API for a transaction belonging to the supplied node by transaction id
397
+ # @param node_id [String]
398
+ # @param trans_id [String] id of the transaction to find
399
+ # @return [Synapse::Transaction]
400
+ def get_node_transaction(node_id:, trans_id:)
401
+ path = node(user_id: self.user_id, node_id: node_id) + "/trans/#{trans_id}"
402
+
403
+ begin
404
+ trans = client.get(path)
405
+ rescue Synapse::Error::Unauthorized
406
+ self.authenticate()
407
+ trans = client.get(path)
408
+ end
409
+ Transaction.new(trans_id: trans['_id'],
410
+ payload: trans,
411
+ node_id: node_id
412
+ )
413
+ end
414
+
415
+
416
+ # Queries the API for all transactions belonging to the supplied node
417
+ # @param node_id [String] node to which the transaction belongs
418
+ # @param page [Integer] (optional) response will default to 1
419
+ # @param per_page [Integer] (optional) response will default to 20
420
+ # @return [Array<Synapse::Transaction>]
421
+ def get_all_node_transaction(node_id:, **options)
422
+ [options[:page], options[:per_page]].each do |arg|
423
+ if arg && (!arg.is_a?(Integer) || arg < 1)
424
+ raise ArgumentError, "#{arg} must be nil or an Integer >= 1"
425
+ end
426
+ end
427
+
428
+ path = node(user_id: self.user_id, node_id: node_id) + "/trans"
429
+
430
+ params = VALID_QUERY_PARAMS.map do |p|
431
+ options[p] ? "#{p}=#{options[p]}" : nil
432
+ end.compact
433
+
434
+ path += '?' + params.join('&') if params.any?
435
+
436
+ begin
437
+ trans = client.get(path)
438
+ rescue Synapse::Error::Unauthorized
439
+ self.authenticate()
440
+ trans = client.get(path)
441
+ end
442
+ response = trans["trans"].map { |trans_data| Transaction.new(trans_id: trans_data['_id'],
443
+ payload: trans_data,
444
+ node_id: node_id
445
+ )}
446
+ Transactions.new(limit: trans["limit"],
447
+ page: trans["page"],
448
+ page_count: trans["page_count"],
449
+ trans_count: trans["trans_count"],
450
+ payload: response
451
+ )
452
+ end
453
+
454
+ # Verifies microdeposits for a node
455
+ # @param node_id [String]
456
+ # @param payload [Hash]
457
+ def verify_micro_deposit(node_id:,payload:)
458
+ path = node(user_id: self.user_id, node_id: node_id)
459
+ begin
460
+ response = client.patch(path, payload)
461
+ rescue Synapse::Error::Unauthorized
462
+ self.authenticate()
463
+ response = client.patch(path, payload)
464
+ end
465
+ Node.new(user_id: self.user_id,
466
+ node_id: response["_id"],
467
+ full_dehydrate: false,
468
+ payload: response,
469
+ type: response["type"]
470
+ )
471
+ end
472
+
473
+ # Reinitiate microdeposits on a node
474
+ # @param node_id [String]
475
+ def reinitiate_micro_deposit(node_id:)
476
+ payload = {}
477
+ path = node(user_id: self.user_id, node_id: node_id) + "?resend_micro=YES"
478
+ begin
479
+ response = client.patch(path, payload)
480
+ rescue Synapse::Error::Unauthorized
481
+ self.authenticate()
482
+ response = client.patch(path, payload)
483
+ end
484
+ Node.new(user_id: self.user_id,
485
+ node_id: response["_id"],
486
+ full_dehydrate: false,
487
+ payload: response,
488
+ type: response["type"])
489
+ end
490
+
491
+ # Generate tokenized info for Apple Wallet
492
+ # @param node_id [String]
493
+ # @param payload [Hash]
494
+ # @see https://docs.synapsefi.com/docs/generate-applepay-token
495
+ def generate_apple_pay_token(node_id:,payload:)
496
+ path = node(user_id: self.user_id, node_id: node_id) + "/applepay"
497
+ begin
498
+ response = client.patch(path, payload)
499
+ rescue Synapse::Error::Unauthorized
500
+ self.authenticate()
501
+ response = client.patch(path, payload)
502
+ end
503
+ response
504
+ end
505
+
506
+ # Update supp_id, nickname, etc. for a node
507
+ # @param node_id [String]
508
+ # @param payload [Hash]
509
+ # @see https://docs.synapsefi.com/docs/update-info
510
+ # @return [Synapse::Node]
511
+ def update_node(node_id:, payload:)
512
+ path = node(user_id: self.user_id, node_id: node_id)
513
+
514
+ begin
515
+ update = client.patch(path, payload)
516
+ rescue Synapse::Error::Unauthorized
517
+ self.authenticate()
518
+ update = client.patch(path, payload)
519
+ end
520
+ Node.new(node_id: node_id,
521
+ user_id: self.user_id,
522
+ payload: update,
523
+ full_dehydrate: false,
524
+ type: update["type"]
525
+ )
526
+ end
527
+
528
+ # @param node_id [String]
529
+ def delete_node(node_id:)
530
+ path = node(user_id: self.user_id, node_id: node_id)
531
+
532
+ begin
533
+ delete = client.delete(path)
534
+ rescue Synapse::Error::Unauthorized
535
+ self.authenticate()
536
+ delete = client.delete(path)
537
+ end
538
+ delete
539
+ end
540
+
541
+ # Initiates dummy transactions to a node
542
+ # @param node_id [String]
543
+ # @param is_credit [String]
544
+ # @param foreign_transaction [String]
545
+ # @param subnetid [String]
546
+ # @param type [String]
547
+ # @see https://docs.synapsefi.com/docs/trigger-dummy-transactions
548
+ def dummy_transactions(node_id:, **options)
549
+ path = node(user_id: self.user_id, node_id: node_id) + "/dummy-tran"
550
+ params = VALID_QUERY_PARAMS.map do |p|
551
+ options[p] ? "#{p}=#{options[p]}" : nil
552
+ end.compact
553
+ path += '?' + params.join('&') if params.any?
554
+
555
+ begin
556
+ response = client.get(path)
557
+ rescue Synapse::Error::Unauthorized
558
+ self.authenticate()
559
+ response = client.get(path)
560
+ end
561
+ response
562
+ end
563
+
564
+ # Adds comment to the transactions
565
+ # @param node_id [String]
566
+ # @param trans_id [String]
567
+ # @param payload [Hash]
568
+ # @return [Synapse::Transaction]
569
+ def comment_transaction(node_id:,trans_id:,payload:)
570
+ path = trans_path(user_id: self.user_id, node_id: node_id) + "/#{trans_id}"
571
+
572
+ begin
573
+ trans = client.patch(path, payload)
574
+ rescue Synapse::Error::Unauthorized
575
+ self.authenticate()
576
+ trans = client.patch(path, payload)
577
+ end
578
+ Transaction.new(trans_id: trans['_id'], payload: trans)
579
+ end
580
+
581
+ # Cancels transaction if it has not already settled
582
+ # @param node_id
583
+ # @param trans_id
584
+ # @return API response [Hash]
585
+ def cancel_transaction(node_id:, trans_id:)
586
+ path = trans_path(user_id: self.user_id, node_id: node_id) + "/#{trans_id}"
587
+ begin
588
+ response = client.delete(path)
589
+ rescue Synapse::Error::Unauthorized
590
+ self.authenticate()
591
+ response = client.delete(path)
592
+ end
593
+ response
594
+ end
595
+
596
+ # Dispute a transaction for a user
597
+ # @param node_id
598
+ # @param trans_id
599
+ # @see https://docs.synapsefi.com/docs/dispute-card-transaction
600
+ # @return API response [Hash]
601
+ def dispute_card_transactions(node_id:, trans_id:, payload:)
602
+ path = trans_path(user_id: user_id, node_id: node_id) + "/#{trans_id}"
603
+ path += "/dispute"
604
+ begin
605
+ dispute = client.patch(path, payload)
606
+ rescue Synapse::Error::Unauthorized
607
+ self.authenticate()
608
+ dispute = client.patch(path, payload)
609
+ end
610
+ dispute
611
+ end
612
+
613
+ # Creates subnet for a node debit card or act/rt number
614
+ # @param node_id [String]
615
+ # @param payload [Hash]
616
+ # @param idempotency_key [String] (optional)
617
+ # @return [Synapse::Subnet]
618
+ def create_subnet(node_id:,payload:, **options)
619
+ path = subnet_path(user_id: self.user_id, node_id: node_id)
620
+ begin
621
+ subnet = client.post(path,payload, options)
622
+ rescue Synapse::Error::Unauthorized
623
+ self.authenticate()
624
+ subnet = client.post(path,payload, options)
625
+ end
626
+ Subnet.new(subnet_id: subnet['_id'], payload: subnet, node_id: node_id)
627
+ end
628
+
629
+ # Updates subnet debit card and act/rt number
630
+ # @param node_id [String]
631
+ # @param payload [Hash]
632
+ # @param subnet_id [String]
633
+ # @return [Synapse::Subnet]
634
+ def update_subnet(node_id:, payload:, subnet_id:, **options)
635
+ path = subnet_path(user_id: self.user_id, node_id: node_id, subnet_id: subnet_id)
636
+ begin
637
+ subnet = client.patch(path,payload)
638
+ rescue Synapse::Error::Unauthorized
639
+ self.authenticate()
640
+ subnet = client.patch(path,payload)
641
+ end
642
+ Subnet.new(subnet_id: subnet['_id'], payload: subnet, node_id: node_id)
643
+ end
644
+
645
+
646
+ # Gets all node subnets
647
+ # @param node_id [String]
648
+ # @param page [Integer]
649
+ # @param per_page [Integer]
650
+ # @see https://docs.synapsefi.com/docs/all-node-subnets
651
+ def get_all_subnets(node_id:,**options)
652
+ [options[:page], options[:per_page]].each do |arg|
653
+ if arg && (!arg.is_a?(Integer) || arg < 1)
654
+ raise ArgumentError, "#{arg} must be nil or an Integer >= 1"
655
+ end
656
+ end
657
+
658
+ path = node(user_id: self.user_id, node_id: node_id) + "/subnets"
659
+ params = VALID_QUERY_PARAMS.map do |p|
660
+ options[p] ? "#{p}=#{options[p]}" : nil
661
+ end.compact
662
+ path += '?' + params.join('&') if params.any?
663
+
664
+ begin
665
+ subnets = client.get(path)
666
+ rescue Synapse::Error::Unauthorized
667
+ self.authenticate()
668
+ subnets = client.get(path)
669
+ end
670
+
671
+ response = subnets["subnets"].map { |subnets_data| Subnet.new(subnet_id: subnets_data['_id'],
672
+ payload: subnets,
673
+ node_id: node_id
674
+ )}
675
+ Subnets.new(limit: subnets["limit"],
676
+ page: subnets["page"],
677
+ page_count: subnets["page_count"],
678
+ subnets_count: subnets["subnets_count"],
679
+ payload: response,
680
+ node_id: node_id
681
+ )
682
+ end
683
+
684
+ # Queries a node for a specific subnet by subnet_id
685
+ # @param node_id [String] id of node
686
+ # @param subnet_id [String,void] (optional) id of a subnet to look up
687
+ # @param full_dehydrate [String](optional)
688
+ # @return [Synapse::Subnet]
689
+ def get_subnet(node_id:,subnet_id:,**options)
690
+ path = node(user_id: self.user_id, node_id: node_id) + "/subnets/#{subnet_id}"
691
+
692
+ params = VALID_QUERY_PARAMS.map do |p|
693
+ options[p] ? "#{p}=#{options[p]}" : nil
694
+ end.compact
695
+ path += '?' + params.join('&') if params.any?
696
+
697
+ begin
698
+ subnet = client.get(path)
699
+ rescue Synapse::Error::Unauthorized
700
+ self.authenticate()
701
+ subnet = client.get(path)
702
+ end
703
+ subnet = Subnet.new(subnet_id: subnet['_id'], payload: subnet, node_id: node_id)
704
+ subnet
705
+ end
706
+
707
+ # Gets statement by node
708
+ # @param page [Integer]
709
+ # @param per_page [Integer]
710
+ # @see https://docs.synapsefi.com/docs/statements-by-user
711
+ # @return API response [Hash]
712
+ def get_node_statements(node_id:,**options)
713
+ [options[:page], options[:per_page]].each do |arg|
714
+ if arg && (!arg.is_a?(Integer) || arg < 1)
715
+ raise ArgumentError, "#{arg} must be nil or an Integer >= 1"
716
+ end
717
+ end
718
+
719
+ path = node(user_id: self.user_id, node_id: node_id) + "/statements"
720
+ params = VALID_QUERY_PARAMS.map do |p|
721
+ options[p] ? "#{p}=#{options[p]}" : nil
722
+ end.compact
723
+ path += '?' + params.join('&') if params.any?
724
+
725
+ begin
726
+ statements = client.get(path)
727
+ rescue Synapse::Error::Unauthorized
728
+ self.authenticate()
729
+ statements = client.get(path)
730
+ end
731
+
732
+ statements
733
+ end
734
+
735
+ # Gets statement by node on demand
736
+ # @param payload [Hash]
737
+ # @see https://docs.synapsefi.com/reference#generate-node-statements
738
+ # @return API response [Hash]
739
+ def generate_node_statements(node_id:,payload:)
740
+
741
+ path = node(user_id: self.user_id, node_id: node_id) + "/statements"
742
+
743
+
744
+ begin
745
+ statements = client.post(path,payload)
746
+ rescue Synapse::Error::Unauthorized
747
+ self.authenticate()
748
+ statements = client.post(path,payload)
749
+ end
750
+
751
+ statements
752
+ end
753
+
754
+ private
755
+
756
+ def oauth_path()
757
+ "/oauth/#{self.user_id}"
758
+ end
759
+
760
+ def get_user_path(user_id:, **options)
761
+ path = "/users/#{user_id}"
762
+ params = VALID_QUERY_PARAMS.map do |p|
763
+ options[p] ? "#{p}=#{options[p]}" : nil
764
+ end.compact
765
+ path += '?' + params.join('&') if params.any?
766
+ path
767
+ end
768
+
769
+ def transactions_path(user_id:, **options)
770
+ path = "/users/#{user_id}/trans"
771
+ params = VALID_QUERY_PARAMS.map do |p|
772
+ options[p] ? "#{p}=#{options[p]}" : nil
773
+ end.compact
774
+
775
+ path += '?' + params.join('&') if params.any?
776
+ path
777
+ end
778
+
779
+ def nodes_path( **options )
780
+ path = "/nodes"
781
+
782
+ params = VALID_QUERY_PARAMS.map do |p|
783
+ options[p] ? "#{p}=#{options[p]}" : nil
784
+ end.compact
785
+
786
+ path += '?' + params.join('&') if params.any?
787
+
788
+ path
789
+ end
790
+
791
+ def node(node_id:, **options)
792
+ path = "/users/#{self.user_id}/nodes/#{node_id}"
793
+ params = VALID_QUERY_PARAMS.map do |p|
794
+ options[p] ? "#{p}=#{options[p]}" : nil
795
+ end.compact
796
+
797
+ path += '?' + params.join('&') if params.any?
798
+
799
+ path
800
+ end
801
+
802
+ def trans_path(user_id:, node_id:)
803
+ path = "/users/#{user_id}/nodes/#{node_id}/trans"
804
+ path
805
+ end
806
+
807
+ def subnet_path(user_id:, node_id:, subnet_id: nil)
808
+ if subnet_id
809
+ path = "/users/#{user_id}/nodes/#{node_id}/subnets/#{subnet_id}"
810
+ else
811
+ path = "/users/#{user_id}/nodes/#{node_id}/subnets"
812
+ end
813
+ path
814
+ end
815
+ end
816
+ end
817
+
818
+
819
+
820
+
821
+
822
+
823
+