Class: BarclaysToYnab::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/barclays-to-ynab.rb

Instance Method Summary collapse

Instance Method Details

#convert(path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/barclays-to-ynab.rb', line 7

def convert(path)
  new_path = path + '.ynab.csv'
  transactions = SmarterCSV.process(path)
  headers = %w(Date Payee Memo Inflow Outflow Category)

  File.open(new_path, 'w') do |f|
    f.puts headers.to_csv
    transactions.each do |transaction|

      transaction_hash = {
        'Date' => transaction[:date],
        'Payee' => transaction[:memo].split('  ').first
      }

      if transaction[:amount] > 0
        transaction_hash['Inflow'] = transaction[:amount].abs
      else
        transaction_hash['Outflow'] = transaction[:amount].abs
      end

      f.puts headers.map { |h| transaction_hash[h] }.to_csv
    end
  end

  new_path
end