Created
January 10, 2013 21:03
-
-
Save terotil/4505776 to your computer and use it in GitHub Desktop.
Processor for Nordea machine readable bank statements (NDA format). Functionally minimal, just enough to outline a friendly interface and be able to export main transaction records to CSV, which is further processable to OFX using https://github.com/terotil/ofxify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
module Nordea | |
module NDA | |
class Record | |
def self.parse(str) | |
self.new(str).normalized | |
end | |
def initialize(str) | |
@str = str | |
end | |
def record_type | |
@str[1..2] | |
end | |
def normalized | |
case record_type | |
when '10' | |
Transaction.new(@str) | |
else | |
self | |
end | |
end | |
end | |
class Transaction < Record | |
def number | |
@str[6..11].to_i | |
end | |
def date | |
Date.strptime(@str[30..35], '%y%m%d') | |
end | |
def identifier | |
"%03d/%d/%02d" % [date.month, date.year, number] | |
end | |
def description | |
@str[52..86].strip | |
end | |
def amount | |
"%.2f" % (@str[87..105].to_f / 100) | |
end | |
def other | |
@str[108..142].strip.gsub('{', 'ä') | |
end | |
def level | |
@str[187..187].to_i | |
end | |
end | |
class File < ::File | |
def each_record | |
each_line do |line| | |
yield Record.parse(line) | |
end | |
end | |
end | |
end | |
end | |
Nordea::NDA::File.new(ARGV.first, 'r').each_record do |r| | |
next unless r.record_type == '10' && r.level == 0 | |
puts [(r.date+0.5).strftime("%F %T"), r.other, r.identifier, r.amount].join(',') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment