class SaltParser::Qif::Parser
Attributes
accounts[R]
body[R]
date_format[R]
headers[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/qif/parser.rb, line 6 def initialize(options = {}) @headers = options[:headers].try(:chomp) @body = options[:body] @date_format = options[:date_format] @accounts = Qif::Accounts.new parse_account end
Public Instance Methods
build_transaction_hash(rows)
click to toggle source
# File lib/qif/parser.rb, line 36 def build_transaction_hash(rows) hash = {} rows.map do |row| type = Qif::Transaction::SUPPORTED_FIELDS[row[0].try(:upcase)] next unless type hash[type] = type == :date ? parse_date(row[1..-1]) : row[1..-1].strip end hash[:date].nil? ? {} : hash end
build_transactions()
click to toggle source
# File lib/qif/parser.rb, line 26 def build_transactions transactions_array = body.split("^").reject(&:blank?) check_dates(transactions_array) transactions_array.each_with_object([]) do |transaction, transactions| transaction_hash = build_transaction_hash(transaction.split("\n")) transactions << Qif::Transaction.new(transaction_hash) unless transaction_hash.empty? end end
check_dates(transactions_array)
click to toggle source
# File lib/qif/parser.rb, line 54 def check_dates(transactions_array) dates = transactions_array.map{ |transaction| transaction.split("\n") } .flatten .select{ |line| line.match(/^D/) } .map{ |line| line[1..-1] } dates.each do |row| unless Chronic.parse(row) raise SaltParser::Error::UnsupportedDateFormat.new end end end
parse_account()
click to toggle source
# File lib/qif/parser.rb, line 18 def parse_account @accounts << Qif::Account.new({ :name => Qif::Accounts::SUPPORTED_ACCOUNTS[headers]["name"], :type => Qif::Accounts::SUPPORTED_ACCOUNTS[headers]["type"], :transactions => build_transactions }) end
parse_date(row)
click to toggle source
# File lib/qif/parser.rb, line 46 def parse_date(row) date = Date.strptime(row, date_format) raise ArgumentError if date.year < 1900 date.as_json rescue ArgumentError => error Chronic.parse(row, :endian_precedence => [:middle, :little]).to_date.as_json end
to_hash()
click to toggle source
# File lib/qif/parser.rb, line 14 def to_hash { :accounts => accounts.to_hash } end