-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseNordea.rb
100 lines (82 loc) · 2.57 KB
/
parseNordea.rb
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env ruby
# encoding: utf-8
require 'CSV'
require 'json'
require 'date'
require "./helpers"
#=========================================#
# REMEMBER to save the CSV file as UTF-8! #
#=========================================#
# This should spit out: The name of the purchase place, the date after "Den"
# (optionally the time as well), and the amount
# After that maybe assign specific tags
module Nordea
@filenames = ["nordea_sandra.csv"]
def self.prepare_CSV
rows = []
columns_to_remove = ["Bogført", "Rentedato", "Saldo"]
switch_keys = { "Tekst" => "Name", "Beløb" => "Amount" }
@filenames.each do |filename|
CSV.foreach(filename, col_sep: ';', headers: true) do |row|
row = Helpers.change_key_name row, switch_keys
if row["Name"].match("Den") && row["Name"].match("køb")
columns_to_remove.each do |column|
row.delete column
end
row = row.to_hash
rows.push row
else
# No dates, no interest
end
end
end
return rows
end
def self.cleanup_data(row)
row["Name"] = row["Name"][13..-1] # remove "Electron køb,"
if row["Name"].start_with?(" . ")
row["Name"] = row["Name"][3..-1]
end
row["Name"].squeeze!(" ") # remove excessive space within strings
if row["Amount"].start_with?("-")
row["Amount"] = row["Amount"][1..-1]
end
# convert to float to be able to round off
row["Amount"] = row["Amount"].gsub(',', '.').to_f.round(1)
# convert back to string to be able to use Formatador's coloring
row["Amount"] = row["Amount"].to_s
end
def self.parse_date(row, rows)
index = row["Name"].index("Den")
date = row["Name"][index+3..-1].strip! # plus 3 to not include "Den"
# remove the time from date
if date.length > 5
date = date[0..4]
end
row["Name"] = row["Name"][0..index-1].rstrip!
date = Date.strptime(date, "%d.%m") # to be able to sort by date
row.merge!("Date" => date)
end
def self.read_tags(filename)
tag_file = File.read(filename)
tags = JSON.parse(tag_file)
end
def self.match_tags(filename, row)
tags = read_tags(filename)
tags.each do |tag, value|
value.each do |v| # each value is an array
row["Name"].match(v) { row.merge!("Tag" => tag) }
end
end
end
def self.prepare_rows
tags_filename = "tags.json"
rows = prepare_CSV
rows.each do |row|
Nordea.cleanup_data row
Nordea.parse_date row, rows
Nordea.match_tags tags_filename, row
end
rows.sort_by! { |hash| hash["Date"] }
end
end