Class: OFX::Parser Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Implementation class for OFX parsing. Prefer the top-level new entry point over instantiating this class directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, &block) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Parser.

Parameters:

  • resource (String, IO)

    file path or IO object containing OFX data

  • block (Proc)

    optional block; receives the parser instance (arity 1) or is evaluated in the parser’s context (arity != 1)



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ofx_kit/parser.rb', line 12

def initialize(resource, &block)
  @filename  = extract_filename(resource)
  content    = read_resource(resource)
  tokenizer  = build_tokenizer(content).new(content)
  @document  = Base::Document.new(headers: tokenizer.headers, body: tokenizer.body)
  @statements = Base::Builder.new(@document).statements

  return unless block_given?

  block.arity == 1 ? block.call(self) : instance_eval(&block)
end

Instance Attribute Details

#filenameArray<BankStatement, CreditCardStatement>, ... (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



26
27
28
# File 'lib/ofx_kit/parser.rb', line 26

def filename
  @filename
end

#statementsArray<BankStatement, CreditCardStatement>, ... (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



26
27
28
# File 'lib/ofx_kit/parser.rb', line 26

def statements
  @statements
end

Instance Method Details

#accountAccount?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the account for files containing a single statement.

Returns:

  • (Account, nil)

Raises:



36
37
38
39
40
41
42
# File 'lib/ofx_kit/parser.rb', line 36

def 
  if statements.length > 1
    raise MultipleStatementsError, 'File contains multiple statements. Use `accounts` to get all accounts.'
  end

  statements.first&.
end

#accountsArray<Account>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns all accounts across all statements.

Returns:

  • (Array<Account>)

    all accounts across all statements



45
46
47
# File 'lib/ofx_kit/parser.rb', line 45

def accounts
  statements.map(&:account)
end

#balanceBalance?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the balance for files containing a single statement.

Returns:

Raises:



65
66
67
68
69
70
71
# File 'lib/ofx_kit/parser.rb', line 65

def balance
  if statements.length > 1
    raise MultipleStatementsError, 'File contains multiple statements. Use `balances` to get all balances.'
  end

  statements.first&.balance
end

#balancesArray<Balance, nil>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns balances for each statement.

Returns:

  • (Array<Balance, nil>)

    balances for each statement



74
75
76
77
78
79
80
81
82
# File 'lib/ofx_kit/parser.rb', line 74

def balances
  if statements.length > 1 && OFX.config.multi_statement_warnings?
    warn "[OFX] `balances` is aggregating across #{statements.length} statements. " \
         'For per-account balance use `statements[i].balance`. ' \
         'To disable this warning: OFX.config.multi_statement_warnings = false'
  end

  statements.map(&:balance)
end

#headersHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns parsed OFX header fields.

Returns:

  • (Hash)

    parsed OFX header fields



29
30
31
# File 'lib/ofx_kit/parser.rb', line 29

def headers
  @document.headers
end

#summaryHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a structured summary of all statements, including transaction counts, credit/debit totals (in cents), and closing balance.

Returns:

  • (Hash)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ofx_kit/parser.rb', line 87

def summary
  {
    headers: headers.compact,
    statements: statements.each_with_object({}) do |stmt, h|
      acct = stmt.
      txns = stmt.transactions
      h[acct.] = {
        currency: acct.currency,
        transactions: { count: txns.length, net_cents: txns.net.fractional },
        credits: { count: txns.credits.length, total_cents: txns.total_credits.fractional },
        debits: { count: txns.debits.length, total_cents: txns.total_debits.fractional },
        balance_cents: stmt.balance&.amount&.fractional
      }
    end
  }
end

#transactionsTransactionCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns all transactions aggregated across all statements. Emits a warning when the file contains multiple statements.



52
53
54
55
56
57
58
59
60
# File 'lib/ofx_kit/parser.rb', line 52

def transactions
  if statements.length > 1 && OFX.config.multi_statement_warnings?
    warn "[OFX] `transactions` is aggregating across #{statements.length} statements. " \
         'For per-account transactions use `statements[i].transactions`. ' \
         'To disable this warning: OFX.config.multi_statement_warnings = false'
  end

  TransactionCollection.new(statements.flat_map { |s| s.transactions.to_a })
end