diff --git a/app/services/import/revolut_business.rb b/app/services/import/revolut_business.rb new file mode 100644 index 00000000..f2d6886f --- /dev/null +++ b/app/services/import/revolut_business.rb @@ -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 diff --git a/bin/import b/bin/import index 147c7ab6..1be9a500 100755 --- a/bin/import +++ b/bin/import @@ -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 = ''