synapse_pay_rest 0.0.15 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +7 -2
  3. data/Gemfile.lock +10 -6
  4. data/LICENSE +20 -0
  5. data/README.md +80 -22
  6. data/lib/synapse_pay_rest.rb +65 -21
  7. data/lib/synapse_pay_rest/api/nodes.rb +93 -19
  8. data/lib/synapse_pay_rest/api/transactions.rb +103 -0
  9. data/lib/synapse_pay_rest/api/users.rb +101 -41
  10. data/lib/synapse_pay_rest/client.rb +49 -0
  11. data/lib/synapse_pay_rest/error.rb +8 -2
  12. data/lib/synapse_pay_rest/http_client.rb +94 -27
  13. data/lib/synapse_pay_rest/models/node/ach_us_node.rb +111 -0
  14. data/lib/synapse_pay_rest/models/node/base_node.rb +192 -0
  15. data/lib/synapse_pay_rest/models/node/eft_ind_node.rb +19 -0
  16. data/lib/synapse_pay_rest/models/node/eft_node.rb +27 -0
  17. data/lib/synapse_pay_rest/models/node/eft_np_node.rb +19 -0
  18. data/lib/synapse_pay_rest/models/node/iou_node.rb +27 -0
  19. data/lib/synapse_pay_rest/models/node/node.rb +99 -0
  20. data/lib/synapse_pay_rest/models/node/reserve_us_node.rb +23 -0
  21. data/lib/synapse_pay_rest/models/node/synapse_ind_node.rb +22 -0
  22. data/lib/synapse_pay_rest/models/node/synapse_node.rb +25 -0
  23. data/lib/synapse_pay_rest/models/node/synapse_np_node.rb +22 -0
  24. data/lib/synapse_pay_rest/models/node/synapse_us_node.rb +23 -0
  25. data/lib/synapse_pay_rest/models/node/unverified_node.rb +73 -0
  26. data/lib/synapse_pay_rest/models/node/wire_int_node.rb +23 -0
  27. data/lib/synapse_pay_rest/models/node/wire_node.rb +38 -0
  28. data/lib/synapse_pay_rest/models/node/wire_us_node.rb +23 -0
  29. data/lib/synapse_pay_rest/models/transaction/transaction.rb +212 -0
  30. data/lib/synapse_pay_rest/models/user/base_document.rb +346 -0
  31. data/lib/synapse_pay_rest/models/user/document.rb +71 -0
  32. data/lib/synapse_pay_rest/models/user/physical_document.rb +29 -0
  33. data/lib/synapse_pay_rest/models/user/question.rb +45 -0
  34. data/lib/synapse_pay_rest/models/user/social_document.rb +7 -0
  35. data/lib/synapse_pay_rest/models/user/user.rb +593 -0
  36. data/lib/synapse_pay_rest/models/user/virtual_document.rb +77 -0
  37. data/lib/synapse_pay_rest/version.rb +2 -1
  38. data/samples.md +391 -219
  39. data/synapse_pay_rest.gemspec +13 -12
  40. metadata +78 -24
  41. data/lib/synapse_pay_rest/api/trans.rb +0 -38
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47dd33491a54e460f6d819149c967607c7a93aaa
4
- data.tar.gz: 409fe90ce07bb9eb0c99546cdb5d7c5d48c0e5e1
3
+ metadata.gz: e33376ac91a6d2f4824d884b3bec22a975285713
4
+ data.tar.gz: af00f3c6553a319bb4765d6299f1fa2c73c25860
5
5
  SHA512:
6
- metadata.gz: 5b63b1214ab80b3e6647e4d9dab8c83c8dfde99f4902532f556a84538842b8643ddc3cf511c58d22946fb9c1b10d31abbb8ec454908519c6b8f8656db0718fa3
7
- data.tar.gz: a08d5a5e82b14cbd9bd3cfb6116284e04833a2d8a33025c7fd077367ee1f938376817f1cc3388069cf1209776148a46d736f8eeeabd50e9bbb34c8d9df6fbad1
6
+ metadata.gz: afdb617d06b9c41c7c909ea2818fbaf7c38f4e5dc51b0dde52c01e785f263518b2c1d81fdb09c8a6ace160e47a8213090c4cad6ca381f320993c257742f75aa8
7
+ data.tar.gz: 5e9851313194faa3d6d806614247bf163f1253953f14cea6f004278797f76a627eeccd41c449b386a9f259713d8a4d7a968483070a47620b08f4c2c1f9e00eb5
data/.gitignore CHANGED
@@ -9,6 +9,7 @@
9
9
  /test/tmp/
10
10
  /test/version_tmp/
11
11
  /tmp/
12
+ coverage
12
13
 
13
14
  ## Specific to RubyMotion:
14
15
  .dat*
@@ -20,6 +21,7 @@ build/
20
21
  /_yardoc/
21
22
  /doc/
22
23
  /rdoc/
24
+ *.html
23
25
 
24
26
  ## Environment normalisation:
25
27
  /.bundle/
@@ -35,6 +37,9 @@ build/
35
37
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
38
  .rvmrc
37
39
 
38
-
39
40
  .DS_Store
40
- .env
41
+
42
+ ## ENV variables and other secrets
43
+ .env
44
+ test.txt
45
+ *.yml
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synapse_pay_rest (0.0.13)
5
- rest-client
4
+ synapse_pay_rest (2.0)
5
+ rest-client (~> 2.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -12,8 +12,11 @@ GEM
12
12
  domain_name (0.5.20160826)
13
13
  unf (>= 0.0.5, < 1.0.0)
14
14
  dotenv (2.1.1)
15
- http-cookie (1.0.2)
15
+ faker (1.6.6)
16
+ i18n (~> 0.5)
17
+ http-cookie (1.0.3)
16
18
  domain_name (~> 0.5)
19
+ i18n (0.7.0)
17
20
  mime-types (3.1)
18
21
  mime-types-data (~> 3.2015)
19
22
  mime-types-data (3.2016.0521)
@@ -39,9 +42,10 @@ PLATFORMS
39
42
 
40
43
  DEPENDENCIES
41
44
  bundler (~> 1.10)
42
- dotenv
43
- minitest
44
- minitest-reporters
45
+ dotenv (~> 2.1.1)
46
+ faker (~> 1.6.6)
47
+ minitest (~> 5.8.2)
48
+ minitest-reporters (~> 1.1.5)
45
49
  rake (~> 10.0)
46
50
  synapse_pay_rest!
47
51
 
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 SynapsePay LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,34 +1,92 @@
1
- ## SynapsePayRest-Ruby
1
+ # SynapsePayRest-Ruby
2
2
 
3
- Simple API wrapper for SynapsePay REST V3 API. This wrapper aims to handle the headers for each API request and appropriate error handling. Not all API endpoints are supported.
3
+ Native API library for SynapsePay REST v3.x
4
4
 
5
- ## Code Example
5
+ Originally developed as a simple wrapper to handle the headers and endpoint URLs for each API request, as of v2.0.0 it now handles creation of User, Node, Transaction and related objects to remove the necessity of dealing with raw payload and response JSON.
6
6
 
7
- Check out [samples.md](https://github.com/synapsepay/SynapsePayRest-Ruby/blob/master/samples.md) and our [API documentation](http://docs.synapsepay.com/v3.1) for examples.
7
+ Not all API endpoints are supported.
8
+
9
+ **Pre-2.0.0 users**
10
+
11
+ There are significant changes but backwards compatibility has been mostly maintained by building on top of the base API wrapper. You can still use the previous classes but note the following changes:
12
+
13
+ - `ArgumentError` will be raised for missing payloads or other required arguments, where `RuntimeError` was raised previously.
14
+ - `development_mode` now defaults to true (gem previously defaulted to production).
15
+ - KYC 1.0 methods for uploading documents have been deprecated. Please contact SynapsePay if you need to update to KYC 2.0.
16
+ - API errors will now raise `SynapsePayRest::Error`s instead returning a JSON hash (and sometimes obfuscating the API error message).
8
17
 
9
18
  ## Installation
10
19
 
11
- gem install synapse_pay_rest
20
+ Add this line to your application's Gemfile:
12
21
 
13
- ## License
22
+ ```ruby
23
+ gem 'synapse_pay_rest'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ ```bash
29
+ $ bundle
30
+ ```
31
+
32
+ Or install it yourself by executing:
33
+
34
+ ```bash
35
+ $ gem install synapse_pay_rest
36
+ ```
37
+
38
+ ## Documentation
39
+
40
+ - [Samples demonstrating common operations](samples.md)
41
+ - [Gem docs](http://www.rubydoc.info/gems/synapse_pay_rest)
42
+ - [API docs](http://docs.synapsepay.com/v3.1)
43
+
44
+ ## Contributing
14
45
 
15
- The MIT License (MIT)
46
+ Just open a pull request. Please document and test any public constants/methods. Open an issue or email steven@synapsepay.com if you have any questions.
16
47
 
17
- Copyright (c) 2014 SynapsePay LLC
48
+ ## Running the Test Suite
18
49
 
19
- Permission is hereby granted, free of charge, to any person obtaining a copy of
20
- this software and associated documentation files (the "Software"), to deal in
21
- the Software without restriction, including without limitation the rights to
22
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
- the Software, and to permit persons to whom the Software is furnished to do so,
24
- subject to the following conditions:
50
+ Make sure these values are set as enviroment variables (using [dotenv](https://github.com/bkeepers/dotenv) for example):
25
51
 
26
- The above copyright notice and this permission notice shall be included in all
27
- copies or substantial portions of the Software.
52
+ ```
53
+ CLIENT_ID=your_sandbox_client_id
54
+ CLIENT_SECRET=your_sandbox_client_secret
55
+ ```
56
+
57
+ To run all tests, execute:
58
+
59
+ ```bash
60
+ rake
61
+ ```
62
+
63
+ To run a specific test or file, install the [m](https://github.com/qrush/m) gem and execute:
64
+
65
+ ```bash
66
+ m path/to/file:line_number
67
+ ```
68
+
69
+ ## Todos
70
+
71
+ - Smartly update the existing instances with response data instead of re-instantiating for every response. I started with this approach but it gets pretty complicated with the interconnected models of User/BaseDocument/Document. Also, certain values are only known if the user created an object, but not when the object is built from API response data. This is solveable but requires more dev time and testing.
72
+ - Various factory helper methods should be private but are public. Would be good to refactor in a way that they can be private.
73
+ - `User`/`Node`/`Transaction` have similar REST methods that could probably be factored into a superclass or module.
74
+ - Use mixins instead of inheritance for the shared behavior of `Node`s and `Document`s. The parent classes are never instantiated anyways.
75
+ - Refactor the redundant code in each `Node` type's version of `#payload_for_create`.
76
+ - Better way to handle bank MFA nodes? Virtual doc KBA?
77
+ - Add an option for logging responses in addition to requests.
78
+ - Organize tests better.
79
+ - Use mocked responses whenever possible in tests.
80
+ - More examples.
81
+ - More Error types (bad MFA answer, for example).
82
+ - Add some methods to BaseDocument to return doc by type (e.g. `base_document.ssn_doc`).
83
+
84
+ Specific todos are marked throughout in this format:
85
+
86
+ ```ruby
87
+ # @todo Description of todo.
88
+ ```
89
+
90
+ ## License
28
91
 
29
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
92
+ [MIT License](LICENSE)
@@ -1,25 +1,69 @@
1
- require "synapse_pay_rest/http_client"
2
- require "synapse_pay_rest/error"
3
- require "synapse_pay_rest/version"
4
- require "synapse_pay_rest/api/users"
5
- require "synapse_pay_rest/api/nodes"
6
- require "synapse_pay_rest/api/trans"
1
+ # client classes
2
+ require 'synapse_pay_rest/client'
3
+ require 'synapse_pay_rest/http_client'
7
4
 
5
+ # base API classes
6
+ require 'synapse_pay_rest/api/users'
7
+ require 'synapse_pay_rest/api/nodes'
8
+ require 'synapse_pay_rest/api/transactions'
9
+
10
+ # general library classes
11
+ require 'synapse_pay_rest/error'
12
+ require 'synapse_pay_rest/version'
13
+
14
+ # user-related classes
15
+ require 'synapse_pay_rest/models/user/user'
16
+ require 'synapse_pay_rest/models/user/base_document'
17
+ require 'synapse_pay_rest/models/user/document'
18
+ require 'synapse_pay_rest/models/user/physical_document'
19
+ require 'synapse_pay_rest/models/user/social_document'
20
+ require 'synapse_pay_rest/models/user/virtual_document'
21
+ require 'synapse_pay_rest/models/user/question'
22
+
23
+ # node-related classes
24
+
25
+ # ancestor of all BaseNodes
26
+ require 'synapse_pay_rest/models/node/base_node'
27
+
28
+ # BaseNode subclasses
29
+ # synapse
30
+ require 'synapse_pay_rest/models/node/synapse_node'
31
+ require 'synapse_pay_rest/models/node/synapse_us_node.rb'
32
+ require 'synapse_pay_rest/models/node/synapse_ind_node.rb'
33
+ require 'synapse_pay_rest/models/node/synapse_np_node.rb'
34
+ # ach
35
+ require 'synapse_pay_rest/models/node/ach_us_node.rb'
36
+ require 'synapse_pay_rest/models/node/unverified_node.rb'
37
+ # eft
38
+ require 'synapse_pay_rest/models/node/eft_node'
39
+ require 'synapse_pay_rest/models/node/eft_ind_node.rb'
40
+ require 'synapse_pay_rest/models/node/eft_np_node.rb'
41
+ # wire
42
+ require 'synapse_pay_rest/models/node/wire_node.rb'
43
+ require 'synapse_pay_rest/models/node/wire_us_node.rb'
44
+ require 'synapse_pay_rest/models/node/wire_int_node.rb'
45
+ # reserve
46
+ require 'synapse_pay_rest/models/node/reserve_us_node.rb'
47
+ # iou
48
+ require 'synapse_pay_rest/models/node/iou_node.rb'
49
+
50
+ # BaseNode factory
51
+ require 'synapse_pay_rest/models/node/node'
52
+
53
+ # transaction-related classes
54
+ require 'synapse_pay_rest/models/transaction/transaction'
55
+
56
+ # Namespace for all SynapsePayRest classes and modules
8
57
  module SynapsePayRest
9
- class Client
10
- attr_accessor :client, :users, :nodes, :trans
11
-
12
- def initialize(options: raise("options is required"), user_id: nil)
13
- base_url = if options['development_mode']
14
- 'https://sandbox.synapsepay.com/api/3'
15
- else
16
- 'https://synapsepay.com/api/3'
17
- end
18
-
19
- @client = HTTPClient.new options, base_url, user_id: user_id
20
- @users = Users.new @client
21
- @nodes = Nodes.new @client
22
- @trans = Trans.new @client
23
- end
58
+ # Modifies the default method to print a warning when deprecated constants
59
+ # used and returns the new constant.
60
+ #
61
+ # @param [Constant]
62
+ # @return [Constant] the corresponding constant that is not deprecated.
63
+ def self.const_missing(const_name)
64
+ super unless const_name == :Trans
65
+ warn caller.first + " DEPRECATION WARNING: the class SynapsePayRest::#{const_name}"\
66
+ 'is deprecated. Use SynapsePayRest::Transactions instead.'
67
+ Transactions
24
68
  end
25
69
  end
@@ -1,45 +1,119 @@
1
1
  module SynapsePayRest
2
+ # Wrapper class for /nodes endpoints
2
3
  class Nodes
4
+ # Valid optional args for #get
5
+ # @todo Should refactor this to HTTPClient
6
+ VALID_QUERY_PARAMS = [:page, :per_page, :type].freeze
7
+
8
+ # @!attribute [rw] client
9
+ # @return [SynapsePayRest::HTTPClient]
3
10
  attr_accessor :client
4
11
 
12
+ # @param [SynapsePayRest::HTTPClient]
5
13
  def initialize(client)
6
14
  @client = client
7
15
  end
8
16
 
9
- def create_node_path(node_id: nil)
10
- path = ['/users', client.user_id, 'nodes' ]
11
- path << node_id if node_id
12
- path.join('/')
17
+ # Sends a GET request to /nodes endpoint. Queries a specific node_id if
18
+ # node_id supplied, else queries all nodes. Returns the response.
19
+ #
20
+ # @param node_id [String] id of the node
21
+ # @param page [String,Integer] (optional) response will default to 1
22
+ # @param per_page [String,Integer] (optional) response will default to 20
23
+ # @param type [String] (optional)
24
+ # @see https://docs.synapsepay.com/docs/node-resources node types
25
+ #
26
+ # @raise [SynapsePayRest::Error] may return subclasses of error based on
27
+ # HTTP response from API
28
+ #
29
+ # @return [Hash] API response
30
+ #
31
+ # @todo should use CGI or RestClient's param builder instead of
32
+ # rolling our own, probably error-prone and untested
33
+ # https://github.com/rest-client/rest-client#usage-raw-url
34
+ def get(node_id: nil, **options)
35
+ params = VALID_QUERY_PARAMS.map do |p|
36
+ options[p] ? "#{p}=#{options[p]}" : nil
37
+ end.compact
38
+
39
+ path = create_node_path(node_id: node_id)
40
+ path += '?' + params.join('&') if params.any?
41
+ client.get(path)
13
42
  end
14
43
 
15
- def add(payload: raise("payload is required"))
44
+ # Sends a POST request to /nodes endpoint, to create a new node for the
45
+ # current user, and returns the response.
46
+ #
47
+ # @param payload [Hash] format depends on node type
48
+ # @see https://docs.synapsepay.com/docs/node-resources payload structure
49
+ #
50
+ # @raise [SynapsePayRest::Error] may return subclasses of error based on
51
+ # HTTP response from API
52
+ #
53
+ # @return [Hash] API response
54
+ def post(payload:)
16
55
  path = create_node_path
17
56
  client.post(path, payload)
18
57
  end
19
58
 
20
- def get(node_id: nil, page: nil)
21
- # If node_id is nil then create_node_path will handle it just fine
59
+ # Alias for #post (legacy name)
60
+ alias_method :add, :post
61
+
62
+ # Sends a PATCH request to /nodes endpoint to update a node, and returns the
63
+ # response. Only used to verify microdeposits for ACH-US nodes currently.
64
+ #
65
+ # @param node_id [String] id of the node
66
+ # @param payload [Hash]
67
+ # @see https://docs.synapsepay.com/docs/verify-micro-deposit payload structure
68
+ #
69
+ # @raise [SynapsePayRest::Error] may return subclasses of error based on
70
+ # HTTP response from API
71
+ #
72
+ # @return [Hash] API response
73
+ def patch(node_id:, payload:)
22
74
  path = create_node_path(node_id: node_id)
23
- path += "?page=#{page}" if page
24
- client.get(path)
75
+ @client.patch(path, payload)
25
76
  end
26
77
 
27
- # separate this into different methods
28
- def verify(node_id: nil, payload: raise("payload is required"))
78
+ # Sends a DELETE request to /node endpoint to remove a node, and returns the response.
79
+ #
80
+ # @param node_id [String] id of the node
81
+ #
82
+ # @raise [SynapsePayRest::Error] may return subclasses of error based on
83
+ # HTTP response from API
84
+ #
85
+ # @return [Hash] API response
86
+ def delete(node_id:)
87
+ path = create_node_path(node_id: node_id)
88
+ client.delete(path)
89
+ end
90
+
91
+ # Verifies microdeposits (via #patch) for a node if a node_id supplied, else
92
+ # submits answers to bank login MFA questions (via #post).
93
+ #
94
+ # @param node_id [String] id of the node
95
+ # @param payload [Hash] see #patch and #post for payload format
96
+ # @deprecated Use #update for microdeposit verification or #post for MFA answers.
97
+ def verify(node_id: nil, payload:)
29
98
  if node_id
99
+ warn caller.first + " DEPRECATION WARNING: #{self.class}##{__method__} is deprecated. Use #patch instead."
100
+
30
101
  # verify microdeposits
31
- path = create_node_path(node_id: node_id)
32
- response = @client.patch(path, payload)
102
+ patch(node_id: node_id, payload: payload)
33
103
  else
34
- # verify MFA question(s)
35
- path = create_node_path
36
- response = @client.post(path, payload)
104
+ warn caller.first + " DEPRECATION WARNING: #{self.class}##{__method__} is deprecated. Use #post instead."
105
+
106
+ # verify MFA questions
107
+ post(payload: payload)
37
108
  end
38
109
  end
39
110
 
40
- def delete(node_id: raise("node_id is required"))
41
- path = create_node_path(node_id: node_id)
42
- client.delete(path)
111
+ private
112
+
113
+ def create_node_path(node_id: nil)
114
+ path = ['/users', client.user_id, 'nodes' ]
115
+ path << node_id if node_id
116
+ path.join('/')
43
117
  end
44
118
  end
45
119
  end
@@ -0,0 +1,103 @@
1
+ module SynapsePayRest
2
+ # Wrapper class for /trans endpoints
3
+ #
4
+ # @todo Implement idempotency keys
5
+ class Transactions
6
+
7
+ # Valid optional args for #get
8
+ # @todo Refactor to HTTPClient
9
+ VALID_QUERY_PARAMS = [:page, :per_page].freeze
10
+
11
+ # @!attribute [rw] client
12
+ # @return [SynapsePayRest::HTTPClient]
13
+ attr_accessor :client
14
+
15
+ # @param client [SynapsePayRest::HTTPClient]
16
+ def initialize(client)
17
+ @client = client
18
+ end
19
+
20
+ # Sends a GET request to /trans endpoint. Queries a specific transaction_id
21
+ # if trans_id supplied, else queries all transactions. Returns the response.
22
+ #
23
+ # @param node_id [String] id of the from node
24
+ # @param trans_id [String,void] (optional) id of a transaction to look up
25
+ # @param page [String,Integer] (optional) response will default to 1
26
+ # @param per_page [String,Integer] (optional) response will default to 20
27
+ #
28
+ # @raise [SynapsePayRest::Error] may return subclasses of error based on
29
+ # HTTP response from API
30
+ #
31
+ # @return [Hash] API response
32
+ #
33
+ # @todo Probably should use CGI or RestClient's param builder instead of
34
+ # rolling our own, probably error-prone and untested version
35
+ # https://github.com/rest-client/rest-client#usage-raw-url
36
+ def get(node_id:, trans_id: nil, **options)
37
+ path = create_transaction_path(node_id: node_id, trans_id: trans_id)
38
+
39
+ params = VALID_QUERY_PARAMS.map do |p|
40
+ options[p] ? "#{p}=#{options[p]}" : nil
41
+ end.compact
42
+
43
+ path += '?' + params.join('&') if params.any?
44
+ client.get(path)
45
+ end
46
+
47
+ # Sends a POST request to /trans endpoint to create a new transaction.
48
+ # Returns the response.
49
+ #
50
+ # @param node_id [String] id of the from node
51
+ # @param payload [Hash]
52
+ # @see https://docs.synapsepay.com/docs/create-transaction payload structure
53
+ #
54
+ # @raise [SynapsePayRest::Error] may return subclasses of error based on
55
+ # HTTP response from API
56
+ #
57
+ # @return [Hash] API response
58
+ def create(node_id:, payload:)
59
+ path = create_transaction_path(node_id: node_id)
60
+ client.post(path, payload)
61
+ end
62
+
63
+ # Sends a PATCH request to /trans endpoint to update a transaction.
64
+ # Returns the response.
65
+ #
66
+ # @param node_id [String] id of the from node
67
+ # @param trans_id [String] id of a transaction to update
68
+ # @param payload [Hash]
69
+ # @see https://docs.synapsepay.com/docs/update-transaction payload structure
70
+ #
71
+ # @raise [SynapsePayRest::Error] may return subclasses of error based on
72
+ # HTTP response from API
73
+ #
74
+ # @return [Hash] API response
75
+ def update(node_id:, trans_id:, payload:)
76
+ path = create_transaction_path(node_id: node_id, trans_id: trans_id)
77
+ client.patch(path, payload)
78
+ end
79
+
80
+ # Sends a DELETE request to /trans endpoint to cancel a transaction.
81
+ # Returns the response.
82
+ #
83
+ # @param node_id [String] id of the from node
84
+ # @param trans_id [String] id of a transaction to delete
85
+ #
86
+ # @raise [SynapsePayRest::Error] may return subclasses of error based on
87
+ # HTTP response from API
88
+ #
89
+ # @return [Hash] API response
90
+ def delete(node_id:, trans_id:)
91
+ path = create_transaction_path(node_id: node_id, trans_id: trans_id)
92
+ client.delete(path)
93
+ end
94
+
95
+ private
96
+
97
+ def create_transaction_path(node_id:, trans_id: nil)
98
+ path = ['/users', client.user_id, 'nodes', node_id, 'trans' ]
99
+ path << trans_id if trans_id
100
+ return path.join('/')
101
+ end
102
+ end
103
+ end