Skip to content

Commit

Permalink
Adding revolut business imports
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrobertson committed Sep 27, 2018
1 parent f73b024 commit ba4782b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/services/import/revolut_business.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Import::RevolutBusiness

BASE_URL = "https://b2b.revolut.com/api/1.0"

def initialize(access_token, ynab_account_id, from: nil)
@ynab_account_id = ynab_account_id
@from = from
@access_token = access_token
end

def import
transactions_to_create = []
transactions.each do |transaction|
transactions_to_create << {
id: "R:#{transaction[:id]}",
amount: (transaction[:legs].first[:amount] * 1000).to_i,
payee_name: transaction[:legs].first[:description],
date: DateTime.parse(transaction[:created_at]),
}
end

YNAB::BulkTransactionCreator.new(transactions_to_create, account_id: @ynab_account_id).create
end

private

def transactions
url = "/transactions"
url = "?from=#{@from}" if @from.present?
get(url)
end

def accounts
get('/accounts')
end

def get(url)
parse_response(RestClient.get(BASE_URL + url, { 'Authorization' => "Bearer #{@access_token}" }))
end

def parse_response(response)
JSON.parse(response.body, symbolize_names: true)
end
end
18 changes: 18 additions & 0 deletions bin/import
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ command :starling do |c|
end
end

command :revolut_business do |c|
c.syntax = 'Fintech To YNAB revolut_business [options]'
c.summary = ''
c.description = ''
c.option '--token STRING', String, 'Revolut Business API token'
c.option '--ynab_account_id STRING', String, 'Your YNAB account ID for this Starling account'
c.option '--from INTEGER', Integer, 'From when? (Unix Timestamp)'
c.action do |args, options|
options.default from: 1.week.ago.to_i
options.from = Time.at(options.from) if options.from.present?

raise ArgumentError.new("token is required!") unless options.token
raise ArgumentError.new("ynab_account_id is required!") unless options.ynab_account_id

Import::RevolutBusiness.new(options.token, options.ynab_account_id, from: options.from).import
end
end

command :teller do |c|
c.syntax = 'Fintech To YNAB teller [options]'
c.summary = ''
Expand Down

0 comments on commit ba4782b

Please sign in to comment.