Class: SaltParser::Qif::Parser
- Inherits:
-
Object
- Object
- SaltParser::Qif::Parser
- Defined in:
- lib/qif/parser.rb
Instance Attribute Summary collapse
-
#accounts ⇒ Object
readonly
Returns the value of attribute accounts.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#date_format ⇒ Object
readonly
Returns the value of attribute date_format.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #build_transaction_hash(rows) ⇒ Object
- #build_transactions ⇒ Object
- #check_dates(transactions_array) ⇒ Object
-
#initialize(options = {}) ⇒ Parser
constructor
A new instance of Parser.
- #parse_account ⇒ Object
- #parse_date(row) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
Instance Attribute Details
#accounts ⇒ Object (readonly)
Returns the value of attribute accounts.
4 5 6 |
# File 'lib/qif/parser.rb', line 4 def accounts @accounts end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
4 5 6 |
# File 'lib/qif/parser.rb', line 4 def body @body end |
#date_format ⇒ Object (readonly)
Returns the value of attribute date_format.
4 5 6 |
# File 'lib/qif/parser.rb', line 4 def date_format @date_format end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
4 5 6 |
# File 'lib/qif/parser.rb', line 4 def headers @headers end |
Instance Method Details
#build_transaction_hash(rows) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# 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 ⇒ Object
26 27 28 29 30 31 32 33 34 |
# 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) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# 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 ⇒ Object
18 19 20 21 22 23 24 |
# 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) ⇒ Object
46 47 48 49 50 51 52 |
# 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 ⇒ Object
14 15 16 |
# File 'lib/qif/parser.rb', line 14 def to_hash { :accounts => accounts.to_hash } end |