Skip to content

Commit 47333be

Browse files
authored
Merge pull request NEXL-LTS#1 from NEXL-LTS/version_0_0_1
Version 0 0 1
2 parents 7116a1b + 1a08aa7 commit 47333be

29 files changed

+965
-0
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Metrics/LineLength:
2+
Max: 100
3+
Metrics/BlockLength:
4+
Max: 50

Gemfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6+
7+
gem 'byebug'
8+
gem 'faker'
9+
gem 'rspec'
10+
gem 'rubocop'
11+
gem 'simplecov', require: false, group: :test

Gemfile.lock

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
ast (2.4.0)
5+
byebug (11.0.0)
6+
concurrent-ruby (1.1.5)
7+
diff-lcs (1.3)
8+
docile (1.3.1)
9+
faker (1.9.3)
10+
i18n (>= 0.7)
11+
i18n (1.6.0)
12+
concurrent-ruby (~> 1.0)
13+
jaro_winkler (1.5.2)
14+
json (2.2.0)
15+
parallel (1.14.0)
16+
parser (2.6.0.0)
17+
ast (~> 2.4.0)
18+
powerpack (0.1.2)
19+
psych (3.1.0)
20+
rainbow (3.0.0)
21+
rspec (3.8.0)
22+
rspec-core (~> 3.8.0)
23+
rspec-expectations (~> 3.8.0)
24+
rspec-mocks (~> 3.8.0)
25+
rspec-core (3.8.0)
26+
rspec-support (~> 3.8.0)
27+
rspec-expectations (3.8.2)
28+
diff-lcs (>= 1.2.0, < 2.0)
29+
rspec-support (~> 3.8.0)
30+
rspec-mocks (3.8.0)
31+
diff-lcs (>= 1.2.0, < 2.0)
32+
rspec-support (~> 3.8.0)
33+
rspec-support (3.8.0)
34+
rubocop (0.65.0)
35+
jaro_winkler (~> 1.5.1)
36+
parallel (~> 1.10)
37+
parser (>= 2.5, != 2.5.1.1)
38+
powerpack (~> 0.1)
39+
psych (>= 3.1.0)
40+
rainbow (>= 2.2.2, < 4.0)
41+
ruby-progressbar (~> 1.7)
42+
unicode-display_width (~> 1.4.0)
43+
ruby-progressbar (1.10.0)
44+
simplecov (0.16.1)
45+
docile (~> 1.1)
46+
json (>= 1.8, < 3)
47+
simplecov-html (~> 0.10.0)
48+
simplecov-html (0.10.2)
49+
unicode-display_width (1.4.1)
50+
51+
PLATFORMS
52+
ruby
53+
54+
DEPENDENCIES
55+
byebug
56+
faker
57+
rspec
58+
rubocop
59+
simplecov
60+
61+
BUNDLED WITH
62+
1.16.6

lib/transferwise-client/account.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module TransferwiseClient
2+
# Create account
3+
class Account < Response
4+
def initialize(response)
5+
super(response)
6+
end
7+
end
8+
end

lib/transferwise-client/client.rb

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
module TransferwiseClient
2+
# Create quote
3+
class Client
4+
def self.connect(auth_key)
5+
# Test connection
6+
TransferwiseClient::Client.new(auth_key)
7+
end
8+
9+
attr_reader :profiles
10+
11+
def initialize(auth_key)
12+
@http_request = HttpRequest.new(auth_key)
13+
end
14+
15+
# POST requests
16+
def create_account(account_request)
17+
return nil unless account_request.valid?
18+
19+
transferwise_response = @http_request.send_post_request(account_request)
20+
Account.new(ResponseFactory.new(transferwise_response).response.to_h)
21+
end
22+
23+
def create_quote(quote_request)
24+
return nil unless quote_request.valid?
25+
26+
transferwise_response = @http_request.send_post_request(quote_request)
27+
Quote.new(ResponseFactory.new(transferwise_response).response.to_h)
28+
end
29+
30+
def temporary_quote(source, target, source_amount)
31+
transferwise_response = @http_request.send_get_request(
32+
"quotes?source=#{source}&target=#{target}&sourceAmount=#{source_amount}&rateType=FIXED"
33+
)
34+
Quote.new(ResponseFactory.new(transferwise_response).response.to_h)
35+
end
36+
37+
def create_transfer(transfer_request)
38+
return nil unless transfer_request.valid?
39+
40+
transferwise_response = @http_request.send_post_request(transfer_request)
41+
Transfer.new(ResponseFactory.new(transferwise_response).response.to_h)
42+
end
43+
44+
def fund(fund_request)
45+
return nil unless fund_request.valid?
46+
47+
transferwise_response = @http_request.send_post_request(fund_request)
48+
ResponseFactory.new(transferwise_response).response
49+
end
50+
51+
# GET requests
52+
def get_transfer(transfer_id)
53+
transferwise_response = @http_request.send_get_request("transfers/#{transfer_id}")
54+
Transfer.new(ResponseFactory.new(transferwise_response).response.to_h)
55+
end
56+
57+
def get_quote(quote_id)
58+
transferwise_response = @http_request.send_get_request("quotes/#{quote_id}")
59+
Quote.new(ResponseFactory.new(transferwise_response).response.to_h)
60+
end
61+
62+
def get_account_statement(borderless_account_id, currency,
63+
start_date = 4.week.ago.utc.iso8601,
64+
end_date = Time.now.utc.iso8601)
65+
transferwise_response = @http_request.send_get_request(
66+
"borderless-accounts/#{borderless_account_id}/statement.json?currency=#{currency}&" \
67+
"intervalStart=#{start_date}&intervalEnd=#{end_date}"
68+
)
69+
70+
ResponseFactory.new(transferwise_response).response.transactions
71+
end
72+
73+
def get_borderless_account(profile_id)
74+
transferwise_response = @http_request.send_get_request(
75+
"borderless-accounts?profileId=#{profile_id}"
76+
)
77+
ResponseFactory.new(transferwise_response).response[0]
78+
end
79+
80+
def get_account_credits(borderless_account_id, currency, start_date = 4.week.ago.utc.iso8601,
81+
end_date = Time.now.utc.iso8601)
82+
get_account_statement(borderless_account_id, currency, start_date, end_date)
83+
.select do |transaction|
84+
transaction['type'] == 'CREDIT'
85+
end
86+
end
87+
88+
def get_account_balances(profile_id)
89+
get_borderless_account(profile_id).balances
90+
end
91+
end
92+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module TransferwiseClient
2+
# Configure transferwise client
3+
class Configuration
4+
attr_accessor :endpoint
5+
attr_accessor :profile_id
6+
attr_accessor :auth_key
7+
8+
def initialize
9+
@endpoint = 'sandbox'
10+
end
11+
12+
def url
13+
return 'https://api.transferwise.com/v1' if @endpoint == 'live'
14+
15+
'https://api.sandbox.transferwise.tech/v1'
16+
end
17+
18+
def validation_url
19+
'https://api.transferwise.com/v1/validators'
20+
end
21+
end
22+
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module TransferwiseClient
2+
# send request
3+
class HttpRequest
4+
def initialize(auth_key = nil)
5+
@auth_key = auth_key
6+
end
7+
8+
def send_post_request(request)
9+
url = URI("#{endpoint}/#{request.path}")
10+
http_post(url, request.to_h.to_json)
11+
end
12+
13+
def send_get_request(path)
14+
url = URI("#{endpoint}/#{path}")
15+
http_get(url)
16+
end
17+
18+
def send_validation_request(validation_params)
19+
validation_params.map do |params|
20+
url = URI("#{validation_url}/#{params[:path]}")
21+
url.query = URI.encode_www_form(params[:params])
22+
http_get(url)
23+
end
24+
end
25+
26+
private
27+
28+
def http_post(url, body)
29+
http = Net::HTTP.new(url.host, url.port)
30+
http.use_ssl = true
31+
http_request = Net::HTTP::Post.new(url)
32+
http_request['Content-Type'] = 'application/json'
33+
http_request['Authorization'] = "Bearer #{@auth_key}"
34+
http_request.body = body
35+
http.request(http_request)
36+
end
37+
38+
def http_get(url)
39+
http = Net::HTTP.new(url.host, url.port)
40+
http.use_ssl = true
41+
http_request = Net::HTTP::Get.new(url)
42+
http_request['Content-Type'] = 'application/json'
43+
http_request['Authorization'] = "Bearer #{@auth_key}" if @auth_key
44+
http.request(http_request)
45+
end
46+
47+
def endpoint
48+
TransferwiseClient.configuration.url
49+
end
50+
51+
def validation_url
52+
TransferwiseClient.configuration.validation_url
53+
end
54+
end
55+
end

lib/transferwise-client/quote.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module TransferwiseClient
2+
# Create quote
3+
class Quote < Response
4+
def initialize(response)
5+
super(response)
6+
end
7+
end
8+
end

lib/transferwise-client/request.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module TransferwiseClient
2+
# Quote request class
3+
class Request < Hash
4+
def path
5+
raise 'Path not implemented.'
6+
end
7+
8+
def validation_params
9+
{}
10+
end
11+
12+
def to_h
13+
raise 'to_h not implemented.'
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)