Class: Qif::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/qif/reader.rb

Overview

The Qif::Reader class reads a qif file and provides access to the transactions as Qif::Transaction objects.

Usage:

reader = Qif::Reader.new(open('/path/to/qif'), 'dd/mm/yyyy')
reader.each do |transaction|
  puts transaction.date.strftime('%d/%m/%Y')
  puts transaction.amount.to_s
end

Defined Under Namespace

Classes: UnknownAccountType

Constant Summary collapse

SUPPORTED_ACCOUNTS =
{
  "!Type:Bank" => "Bank account transactions",
  "!Type:Cash" => "Cash account transactions",
  "!Type:CCard" => "Credit card account transactions",
  "!Type:Oth A" => "Asset account transactions",
  "!Type:Oth L" => "Liability account transactions"
}

Instance Method Summary collapse

Constructor Details

#initialize(data, format = nil) ⇒ Reader

Create a new Qif::Reader object. The data argument must be either an IO object or a String containing the Qif file data.

The optional format argument specifies the date format in the file. Giving a format will force it, otherwise the format will guissed reading the transactions in the file, this defaults to ‘dd/mm/yyyy’ if guessing method fails.

Raises:



34
35
36
37
38
39
40
# File 'lib/qif/reader.rb', line 34

def initialize(data, format = nil)
  @data = data.respond_to?(:read) ? data : StringIO.new(data.to_s)
  @format = DateFormat.new(format || guess_date_format || 'dd/mm/yyyy')
  read_header
  raise(UnknownAccountType, "Unknown account type. Should be one of followings :\n#{SUPPORTED_ACCOUNTS.keys.inspect}") unless SUPPORTED_ACCOUNTS.keys.collect(&:downcase).include? @header.downcase
  reset
end

Instance Method Details

#each(&block) ⇒ Object

Call a block with each Qif::Transaction from the Qif file. This method yields each transaction as it reads the file so it is better to use this than #transactions for large qif files.

reader.each do |transaction|
  puts transaction.amount
end


57
58
59
60
61
62
63
# File 'lib/qif/reader.rb', line 57

def each(&block)    
  reset

  while transaction = next_transaction
    yield transaction
  end
end

#guess_date_formatObject

Guess the file format of dates, reading the beginning of file, or return nil if no dates are found (?!).



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/qif/reader.rb', line 73

def guess_date_format
  begin
    line = @data.gets
    break if line.nil?
    date = line.strip.scan(/^D(\d{1,2}).(\d{1,2}).(\d{2,4})/).flatten
    if date.count == 3 
      guessed_format = date[0].to_i.between?(1, 12) ? (date[1].to_i.between?(1, 12) ? nil : 'mm/dd') : 'dd/mm'
      guessed_format += '/' + 'y'*date[2].length if guessed_format
    end
  end until guessed_format
  @data.rewind
  guessed_format
end

#sizeObject Also known as: length

Return the number of transactions in the qif file.



66
67
68
69
# File 'lib/qif/reader.rb', line 66

def size
  read_all_transactions
  transaction_cache.size
end

#transactionsObject

Return an array of Qif::Transaction objects from the Qif file. This method reads the whole file before returning, so it may not be suitable for very large qif files.



45
46
47
48
# File 'lib/qif/reader.rb', line 45

def transactions
  read_all_transactions
  transaction_cache
end