Class: Statements::Reader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/statements/reader.rb,
lib/statements/reader/st_george_savings.rb,
lib/statements/reader/st_george_credit_card.rb

Direct Known Subclasses

StGeorgeCreditCard, StGeorgeSavings

Defined Under Namespace

Classes: StGeorgeCreditCard, StGeorgeSavings

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pages) ⇒ Reader

Returns a new instance of Reader.



43
44
45
46
# File 'lib/statements/reader.rb', line 43

def initialize(pages)
  @pages = pages
  @document = pages.join("\n").freeze
end

Class Attribute Details

.classesObject (readonly)

Returns the value of attribute classes.



8
9
10
# File 'lib/statements/reader.rb', line 8

def classes
  @classes
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



39
40
41
# File 'lib/statements/reader.rb', line 39

def document
  @document
end

#pagesObject (readonly)

Returns the value of attribute pages.



39
40
41
# File 'lib/statements/reader.rb', line 39

def pages
  @pages
end

Class Method Details

.cell_patternObject

Raises:

  • (NotImplementedError)


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

def self.cell_pattern
  raise NotImplementedError
end

.for_file(file) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/statements/reader.rb', line 16

def self.for_file(file)
  file = file.to_s
  pages = (file =~ /\.pdf$/i) ? PdfReader.read(file) : File.read(file).split(/-{5,}/)
  classes.each do |klass|
    reader = klass.new(pages)
    return reader if reader.valid?
  end
  nil
end

.read_dir(dir) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/statements/reader.rb', line 26

def self.read_dir(dir)
  base = Pathname(dir).realpath
  Dir[base.join('**/*.{pdf,txt}')].each do |path|
    rel_path = Pathname(path).relative_path_from(base)
    begin
      doc = Document.find_or_initialize_by(path: rel_path.to_s)
      doc.scan base: base
    rescue => e
      puts "error: #{e.class.name} #{e.message}\n  #{e.backtrace.join "\n  "}"
    end
  end
end

Instance Method Details

#cell_patternObject



48
49
50
# File 'lib/statements/reader.rb', line 48

def cell_pattern
  self.class.cell_pattern
end

#search_for_transactionsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/statements/reader.rb', line 60

def search_for_transactions
  index = 0
  result = []
  pages.each.with_index do |page, page_index|
    page.scan(cell_pattern).each do |cells|
      result << Transaction.new(document_line: index += 1).tap do |transaction|
        args = [cells, transaction, page_index]
        arity = method(:parse_cells).arity
        parse_cells *args[0..(arity - 1)]
      end
    end
  end
  result
end

#transactionsObject



56
57
58
# File 'lib/statements/reader.rb', line 56

def transactions
  @transactions ||= search_for_transactions
end