Skip to content

Instantly share code, notes, and snippets.

@balvig
Created January 31, 2014 09:11
Show Gist options
  • Save balvig/8728786 to your computer and use it in GitHub Desktop.
Save balvig/8728786 to your computer and use it in GitHub Desktop.
class YnabConverter
require 'csv'
require 'fileutils'
def initialize(input_path, output_path = 'output.csv')
@input_path = input_path
@output_path = output_path
end
def convert!
rewrite_csv
remove_input_file
end
private
def rewrite_csv
file = File.readlines(@input_path, encoding: 'ISO-8859-1:UTF-8')
CSV.open(@output_path, "wb") do |output|
output << %w{ Date Payee Category Memo Outflow Inflow }
file[2..-1].each do |row|
output << NordeaCsv.new(row).to_ynab
end
end
end
def remove_input_file
FileUtils.rm(@input_path)
end
class NordeaCsv
def initialize(row)
@row = row
end
def to_ynab
[date, payee, category, memo, outflow, inflow]
end
private
def input
@input ||= CSV.parse(@row, col_sep: ';').first
end
def date
input[0]
end
def payee
input[1].gsub('Dankort-nota ','').gsub(/[\s\d]+$/,'').squeeze(' ')
end
def category
nil
end
def memo
nil
end
def transaction
input[3].delete('.').gsub(',','.').to_f
end
def outflow
transaction < 0 ? transaction.abs : nil
end
def inflow
transaction > 0 ? transaction : nil
end
end
end
YnabConverter.new('/Users/jens/Downloads/poster.csv').convert!
@balvig
Copy link
Author

balvig commented Jan 31, 2014

Jeg downloader bare "en excel fil" (som der står) fra Nordea, den ender i min downloads folder og scriptet konverterer og sletter originalen (så den ikke er "i vejen" næste gang jeg downloader)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment