Class: ExtrasDeCont::Rules::Revolut

Inherits:
Base
  • Object
show all
Defined in:
lib/extras_de_cont/rules/revolut.rb

Overview

Rules for parsing Revolut bank statements.

Constant Summary collapse

SECTION_HEADERS =
[
  "Pending from ",
  "Account transactions from ",
  "Reverted from ",
  "Deposit transactions from "
].freeze
DOCUMENT_NOISE_HEADERS =
[
  "Balance summary",
  "The balance on your statement might differ",
  "Report lost or stolen card",
  "+",
  "Get help directly in app",
  "Scan the QR code",
  "RON Statement",
  "Generated on the ",
  "Revolut Bank UAB",
  "© "
].freeze
DATE_PREFIX =
/\A(?<date>[A-Z][a-z]{2} \d{1,2}, \d{4})\b/
AMOUNT =
/-?\d[\d,]*\.\d{2} [A-Z]{3}/

Instance Method Summary collapse

Instance Method Details

#parse(text) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/extras_de_cont/rules/revolut.rb', line 33

def parse(text)
  transactions = []
  current_table = nil
  current_lines = []

  each_normalized_line(text) do |line|
    if table_header?(line)
      flush_transaction(current_lines, transactions, current_table)
      current_table = extract_table(line)
      next
    end

    if document_noise?(line)
      flush_transaction(current_lines, transactions, current_table)
      next
    end

    if section_header?(line)
      flush_transaction(current_lines, transactions, current_table)
      current_table = nil
      next
    end

    next if ignorable_line?(line)
    next if current_table.nil?

    if line.match?(DATE_PREFIX)
      flush_transaction(current_lines, transactions, current_table)
      current_lines = [line]
    elsif current_lines.any?
      current_lines << line
    end
  end

  flush_transaction(current_lines, transactions, current_table)
  transactions
end