Skip to content

Instantly share code, notes, and snippets.

@silpol
Forked from terotil/nda2csv.rb
Created January 8, 2017 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silpol/5b0ec3fb6dee7104f419fde00b8a2935 to your computer and use it in GitHub Desktop.
Save silpol/5b0ec3fb6dee7104f419fde00b8a2935 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
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