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, UnrecognizedData

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 Attribute Summary collapse

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 guessed reading the transactions in the file, this defaults to ‘dd/mm/yyyy’ if guessing method fails.

Raises:



40
41
42
43
44
45
46
47
# File 'lib/qif/reader.rb', line 40

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(UnrecognizedData, "Provided data doesn't seems to represent a QIF file") unless @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 Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



20
21
22
# File 'lib/qif/reader.rb', line 20

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


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

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 (?!).



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/qif/reader.rb', line 80

def guess_date_format
  begin
    line = @data.gets
    break if line.nil?

    date = line[1..-1]
    guessed_format = Qif::DateFormat::SUPPORTED_DATEFORMAT.find { |format_string, format|
      test_date_with_format?(date, format_string, format)
    }
  end until guessed_format

  @data.rewind

  guessed_format ? guessed_format.first : @fallback_format
end

#sizeObject Also known as: length

Return the number of transactions in the qif file.



73
74
75
76
# File 'lib/qif/reader.rb', line 73

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.



52
53
54
55
# File 'lib/qif/reader.rb', line 52

def transactions
  read_all_transactions
  transaction_cache
end