synapse_fi 0.0.1

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