Class: SaltParser::Qif::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/qif/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
10
11
12
# 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
  
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



4
5
6
# File 'lib/qif/parser.rb', line 4

def accounts
  @accounts
end

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/qif/parser.rb', line 4

def body
  @body
end

#date_formatObject (readonly)

Returns the value of attribute date_format.



4
5
6
# File 'lib/qif/parser.rb', line 4

def date_format
  @date_format
end

#headersObject (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_transactionsObject



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_accountObject



18
19
20
21
22
23
24
# File 'lib/qif/parser.rb', line 18

def 
  @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_hashObject



14
15
16
# File 'lib/qif/parser.rb', line 14

def to_hash
  { :accounts => accounts.to_hash }
end