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