Skip to content

Commit ba4782b

Browse files
Adding revolut business imports
1 parent f73b024 commit ba4782b

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Import::RevolutBusiness
2+
3+
BASE_URL = "https://b2b.revolut.com/api/1.0"
4+
5+
def initialize(access_token, ynab_account_id, from: nil)
6+
@ynab_account_id = ynab_account_id
7+
@from = from
8+
@access_token = access_token
9+
end
10+
11+
def import
12+
transactions_to_create = []
13+
transactions.each do |transaction|
14+
transactions_to_create << {
15+
id: "R:#{transaction[:id]}",
16+
amount: (transaction[:legs].first[:amount] * 1000).to_i,
17+
payee_name: transaction[:legs].first[:description],
18+
date: DateTime.parse(transaction[:created_at]),
19+
}
20+
end
21+
22+
YNAB::BulkTransactionCreator.new(transactions_to_create, account_id: @ynab_account_id).create
23+
end
24+
25+
private
26+
27+
def transactions
28+
url = "/transactions"
29+
url = "?from=#{@from}" if @from.present?
30+
get(url)
31+
end
32+
33+
def accounts
34+
get('/accounts')
35+
end
36+
37+
def get(url)
38+
parse_response(RestClient.get(BASE_URL + url, { 'Authorization' => "Bearer #{@access_token}" }))
39+
end
40+
41+
def parse_response(response)
42+
JSON.parse(response.body, symbolize_names: true)
43+
end
44+
end

bin/import

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ command :starling do |c|
5858
end
5959
end
6060

61+
command :revolut_business do |c|
62+
c.syntax = 'Fintech To YNAB revolut_business [options]'
63+
c.summary = ''
64+
c.description = ''
65+
c.option '--token STRING', String, 'Revolut Business API token'
66+
c.option '--ynab_account_id STRING', String, 'Your YNAB account ID for this Starling account'
67+
c.option '--from INTEGER', Integer, 'From when? (Unix Timestamp)'
68+
c.action do |args, options|
69+
options.default from: 1.week.ago.to_i
70+
options.from = Time.at(options.from) if options.from.present?
71+
72+
raise ArgumentError.new("token is required!") unless options.token
73+
raise ArgumentError.new("ynab_account_id is required!") unless options.ynab_account_id
74+
75+
Import::RevolutBusiness.new(options.token, options.ynab_account_id, from: options.from).import
76+
end
77+
end
78+
6179
command :teller do |c|
6280
c.syntax = 'Fintech To YNAB teller [options]'
6381
c.summary = ''

0 commit comments

Comments
 (0)