core_pro.rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 47e1af473fc3afb70990f259b46c112f2afd2a76b0eaee62ff6e23d6a3cc48ed
4
+ data.tar.gz: cd00c1b26e77b9e65f5bb2622820622920c803c24ef11154212739df2e88d96e
5
+ SHA512:
6
+ metadata.gz: c839debe81fa0792059ff1a5ef41443f5d720cd4d31bec182cf700ec85f4c6a20931277d28942bb95578991aaea157a6141e50088e2d1edc71dfd3fdbb42c3dc
7
+ data.tar.gz: 73e433fae0e4488ec487396df6de906807c2e5327d27bc727a61256d670efd609b9d663ba0f3bffa6adbf5d2bb8eb383085842f6ac8884689ae283c449750e06
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Stas Suscov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # CorePro API SDK
2
+
3
+ Ruby SDK for working with [CorePro](https://docs.corepro.io)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'core_pro'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install core_pro
20
+
21
+ ## Usage
22
+
23
+ A subset of the CorePro resources are provided with this SDK:
24
+
25
+ * `CorePro::Program`
26
+ * `CorePro::Customer`
27
+ * `CorePro::Account`
28
+ * `CorePro::ExternalAccount`
29
+ * `CorePro::Transfer`
30
+ * `CorePro::Transaction`
31
+
32
+ These resources have implemented the following methods to allow API operations:
33
+ * `#all`
34
+ * `#find`
35
+ * `#create`
36
+ * `#onboard(kyc_vendor)` (only for `CorePro::Customer`)
37
+
38
+ Here's an example on how to create a customer, an account,
39
+ and initiate a transfer:
40
+ ```ruby
41
+ require 'core_pro'
42
+
43
+ customer = CorePro::Customer.create(
44
+ firstName: 'James',
45
+ lastName: 'Bond',
46
+ isSubjectToBackupWithholding: false,
47
+ isOptedInToBankCommunication: false,
48
+ isDocumentsAccepted: true
49
+ ).reload
50
+
51
+ customer.onboard(:socure)
52
+
53
+ old_account = CorePro::Account.find(customer.customerId, <OLD_ACCOUNT_ID>)
54
+
55
+ account = CorePro::Account.create(
56
+ customerId: customer.customerId,
57
+ name: 'Test',
58
+ productId: <PRODUCT_ID>
59
+ )
60
+
61
+ transfer = CorePro::Transfer.create(
62
+ amount: 1.0,
63
+ customerId: customer.customerId,
64
+ fromId: old_account.accountId,
65
+ toId: account.accountId
66
+ )
67
+
68
+ transactions = CorePro::Transaction.all(
69
+ customerId,
70
+ accountId,
71
+ '2021-12-21',
72
+ '2021-12-22'
73
+ )
74
+ ```
75
+
76
+ ### Configuration
77
+
78
+ The API keys will be loaded from your environment variables:
79
+
80
+ * `COREPRO_KEY`
81
+ * `COREPRO_SECRET`
82
+ * `COREPRO_ENDPOINT` defaults to `https://api.corepro.io`
83
+
84
+ ## Development
85
+
86
+ To install this gem onto your local machine, run `bundle exec rake install`. To
87
+ release a new version, update the version number in `version.rb`, and then run
88
+ `bundle exec rake release`, which will create a git tag for the version, push
89
+ git commits and tags, and push the `.gem` file to
90
+ [rubygems.org](https://rubygems.org).
91
+
92
+ ## Contributing
93
+
94
+ Bug reports and pull requests are welcome on GitHub at
95
+ https://github.com/HeyBetter/core_pro. This project is intended to be a safe,
96
+ welcoming space for collaboration, and contributors are expected to adhere to
97
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT
102
+ License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CorePro
4
+ VERSION = '0.0.1'
5
+ end
data/lib/core_pro.rb ADDED
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http/rest_client'
4
+
5
+ # CorePro HTTP API Client
6
+ module CorePro
7
+ # Base endpoint resources class
8
+ class Resource < OpenStruct
9
+ extend HTTP::RestClient::DSL
10
+
11
+ endpoint(ENV['COREPRO_ENDPOINT'] || 'https://api.corepro.io')
12
+ content_type 'application/json'
13
+ basic_auth(user: ENV['COREPRO_KEY'], pass: ENV['COREPRO_SECRET'])
14
+
15
+ # Resource collection finder, uses the default limit
16
+ #
17
+ # @param filters [Array] URI filters to pass to the endpoint.
18
+ # @param params [Hash] URI parameters to pass to the endpoint.
19
+ # @return [Array] of [Object] instances
20
+ def self.all(filters = [], params = {})
21
+ objectify(request(:get, uri(:list, *filters), params: params))
22
+ end
23
+
24
+ # Resource finder
25
+ #
26
+ # @param id [String] resource indentifier
27
+ # @param params [Hash] URI parameters to pass to the endpoint.
28
+ # @return [Object] instance
29
+ def self.find(*id_or_ids)
30
+ objectify(request(:get, uri(:get, *id_or_ids)))
31
+ end
32
+
33
+ # Resource creation helper
34
+ #
35
+ # @param params [Hash] request parameters to pass to the endpoint as JSON.
36
+ # @return [Object] instance
37
+ def self.create(params = {})
38
+ objectify(request(:post, uri(:create), json: params))
39
+ end
40
+
41
+ # Resource update helper
42
+ #
43
+ # @param id [String] resource indentifier
44
+ # @param params [Hash] request parameters to pass to the endpoint as JSON.
45
+ # @return [Object] instance
46
+ def self.update(id, params = {})
47
+ objectify(request(:patch, uri(:update, id), json: params))
48
+ end
49
+
50
+ # Resource constructor wrapper
51
+ #
52
+ # @param payload [Hash] response payload to build a resource.
53
+ # @return [Object] instance or a list of instances.
54
+ def self.objectify(payload)
55
+ if payload['data'].is_a?(Array)
56
+ return payload['data'].map { |data| new(data) }
57
+ end
58
+
59
+ return new(payload['data']) if payload['data'].is_a?(Hash)
60
+
61
+ payload
62
+ end
63
+
64
+ # Returns the ID name based on the resource type
65
+ #
66
+ # @return [String]
67
+ def resource_id
68
+ resource_name = self.class.name.to_s.split('::').last
69
+ resource_name[0] = resource_name[0].downcase
70
+ "#{resource_name}Id"
71
+ end
72
+
73
+ # Helper to reload a resource
74
+ #
75
+ # @return [CorePro::Resource]
76
+ def reload
77
+ self.class.find(send(resource_id))
78
+ end
79
+
80
+ # Extracts the error message from the response
81
+ #
82
+ # @param response [HTTP::Response] the server response
83
+ # @param parsed_response [Object] the parsed server response
84
+ #
85
+ # @return [String]
86
+ def self.extract_error(response, parsed_response)
87
+ parsed_response['errors'] || super(response, parsed_response)
88
+ end
89
+
90
+ # Validate error response
91
+ #
92
+ # Looks at the response code by default.
93
+ #
94
+ # @param response [HTTP::Response] the server response
95
+ # @param parsed_response [Object] the parsed server response
96
+ #
97
+ # @return [TrueClass] if status code is not a successful standard value
98
+ def self.error_response?(response, parsed_response)
99
+ !(200..299).cover?(response.code) && parsed_response['errors'].any?
100
+ end
101
+ end
102
+
103
+ # Customer endpoint resource
104
+ class Program < Resource
105
+ path '/program'
106
+ end
107
+
108
+ # Customer endpoint resource
109
+ class Customer < Resource
110
+ path '/customer'
111
+
112
+ # Performs a customer KYC/onboarding
113
+ #
114
+ # See: https://docs.corepro.io/reference#4-run-kyc-on-the-customer
115
+ #
116
+ # @return [Customer] instance
117
+ def onboard(kyc_vendor)
118
+ onboard_uri = self.class.uri(:onboard, kyc_vendor, :all)
119
+
120
+ self.class.objectify(
121
+ self.class.request(
122
+ :post,
123
+ onboard_uri,
124
+ json: { customerId: customerId }
125
+ )
126
+ )
127
+ end
128
+ end
129
+
130
+ # Accounts endpoint resource
131
+ class Account < Resource
132
+ path '/account'
133
+ end
134
+
135
+ # External account endpoint resource
136
+ class ExternalAccount < Resource
137
+ path '/externalAccount'
138
+ end
139
+
140
+ # Transfer endpoint resource
141
+ class Transfer < Resource
142
+ path '/transfer'
143
+ end
144
+
145
+ # Transaction endpoint resource
146
+ class Transaction < Resource
147
+ path '/transaction'
148
+ end
149
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe CorePro::Resource do
6
+ describe '#all', vcr: 'core_pro' do
7
+ let(:customers) do
8
+ CorePro::Customer.all({})
9
+ end
10
+
11
+ it do
12
+ expect(customers).not_to be_empty
13
+ expect(customers.size).to eq(2)
14
+
15
+ customer = customers.first
16
+
17
+ expect(customer.firstName).to eq('James')
18
+ expect(customer.customerId).to eq(12_345)
19
+
20
+ expect(customer).to be_a(CorePro::Customer)
21
+ end
22
+ end
23
+
24
+ describe '#onboard', vcr: 'core_pro' do
25
+ let(:customer) do
26
+ CorePro::Customer.find(12_345)
27
+ end
28
+
29
+ it do
30
+ expect(customer).not_to be_nil
31
+
32
+ updated_customer = customer.onboard(:socure)
33
+
34
+ expect(updated_customer.customerId).to eq(12_345)
35
+ expect(updated_customer.kyc['status']).to eq('Verified')
36
+ expect(updated_customer.ofac['status']).to eq('Verified')
37
+ end
38
+ end
39
+
40
+ describe '#create for a new customer', vcr: 'core_pro' do
41
+ let(:new_customer) do
42
+ CorePro::Customer.create(
43
+ firstName: 'James',
44
+ lastName: 'Bond',
45
+ isSubjectToBackupWithholding: false,
46
+ isOptedInToBankCommunication: false,
47
+ isDocumentsAccepted: true,
48
+ birthDate: '1987-07-27',
49
+ taxId: '498931947',
50
+ emailAddress: 'stas@startuplandia.io',
51
+ phones: [
52
+ {
53
+ number: '+16502530000',
54
+ phoneType: 'Mobile'
55
+ }
56
+ ],
57
+ addresses: [
58
+ addressLine1: '4017 Buffalo Ave',
59
+ addressType: 'Residence',
60
+ city: 'Buffalo',
61
+ country: 'US',
62
+ postalCode: '94043',
63
+ state: 'NY'
64
+ ]
65
+ ).reload
66
+ end
67
+
68
+ it do
69
+ expect(new_customer).not_to be_nil
70
+ expect(new_customer.firstName).to eq('James')
71
+ expect(new_customer.lastName).to eq('Bond')
72
+ expect(new_customer.customerId).not_to be_nil
73
+
74
+ expect(new_customer).to be_a(CorePro::Customer)
75
+ end
76
+ end
77
+
78
+ describe '#create with errors', vcr: 'core_pro_errors' do
79
+ let(:new_customer) do
80
+ CorePro::Customer.create(
81
+ lastName: 'Bond',
82
+ isSubjectToBackupWithholding: false,
83
+ isOptedInToBankCommunication: false,
84
+ isDocumentsAccepted: true
85
+ ).reload
86
+ end
87
+
88
+ it do
89
+ expect { new_customer }.to raise_error(
90
+ HTTP::RestClient::ResponseError,
91
+ /First Name is a required field/
92
+ )
93
+ end
94
+ end
95
+
96
+ describe '#all for accounts', vcr: 'core_pro' do
97
+ let(:accounts) do
98
+ CorePro::Account.all([12_345], {})
99
+ end
100
+
101
+ it do
102
+ expect(accounts).not_to be_empty
103
+ expect(accounts.size).to eq(1)
104
+
105
+ account = accounts.first
106
+
107
+ expect(account.name).to eq('Test')
108
+ expect(account.customerId).to eq(12_345)
109
+ expect(account.externalAccountId).to eq(9_876)
110
+
111
+ expect(account).to be_a(CorePro::Account)
112
+ end
113
+ end
114
+
115
+ describe '#create for a new account', vcr: 'core_pro' do
116
+ let(:new_account) do
117
+ CorePro::Account.create(
118
+ name: 'Test',
119
+ customerId: 12_345,
120
+ productId: 98_765
121
+ )
122
+ end
123
+
124
+ it do
125
+ expect(new_account).not_to be_nil
126
+
127
+ expect(new_account.name).to eq('Test')
128
+ expect(new_account.customerId).to eq(12_345)
129
+ expect(new_account.accountId).not_to be_nil
130
+
131
+ expect(new_account).to be_a(CorePro::Account)
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'simplecov'
5
+ require 'vcr'
6
+
7
+ SimpleCov.start do
8
+ add_group 'Lib', 'lib'
9
+ add_group 'Tests', 'spec'
10
+ end
11
+ SimpleCov.minimum_coverage 90
12
+
13
+ VCR.configure do |config|
14
+ config.configure_rspec_metadata!
15
+ config.cassette_library_dir = 'vcr_cassettes'
16
+ config.hook_into :webmock
17
+ config.default_cassette_options = {
18
+ record: :new_episodes
19
+ }
20
+
21
+ %w[COREPRO_ENDPOINT COREPRO_KEY COREPRO_SECRET].each do |pkey|
22
+ ENV[pkey] ||= 'secret'
23
+ config.filter_sensitive_data("<#{pkey}>") { ENV[pkey] }
24
+ end
25
+
26
+ config.filter_sensitive_data('<AUTH_TOKEN>') do |interaction|
27
+ auth_headers = interaction.request.headers['Authorization'] || []
28
+ auth_headers.first.to_s.split.last
29
+ end
30
+ end
31
+
32
+ require 'core_pro'
33
+
34
+ RSpec.configure do |config|
35
+ # Disable RSpec exposing methods globally on `Module` and `main`
36
+ config.disable_monkey_patching!
37
+ config.filter_run_when_matching :focus
38
+
39
+ config.expect_with :rspec do |c|
40
+ c.syntax = :expect
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: core_pro.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stas SUȘCOV
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http-rest_client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: vcr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Ruby SDK for working with CorePro web services.
140
+ email:
141
+ - stas@nerd.ro
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - LICENSE.txt
147
+ - README.md
148
+ - lib/core_pro.rb
149
+ - lib/core_pro/version.rb
150
+ - spec/core_pro_spec.rb
151
+ - spec/spec_helper.rb
152
+ homepage: https://github.com/HeyBetter/core_pro
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '2.7'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubygems_version: 3.2.22
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: CorePro Ruby SDK
175
+ test_files: []