Class: OFX::Parser Private
- Inherits:
-
Object
- Object
- OFX::Parser
- 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
- #filename ⇒ Array<BankStatement, CreditCardStatement>, ... readonly private
- #statements ⇒ Array<BankStatement, CreditCardStatement>, ... readonly private
Instance Method Summary collapse
-
#account ⇒ Account?
private
Returns the account for files containing a single statement.
-
#accounts ⇒ Array<Account>
private
All accounts across all statements.
-
#balance ⇒ Balance?
private
Returns the balance for files containing a single statement.
-
#balances ⇒ Array<Balance, nil>
private
Balances for each statement.
-
#headers ⇒ Hash
private
Parsed OFX header fields.
-
#initialize(resource, &block) ⇒ Parser
constructor
private
A new instance of Parser.
-
#summary ⇒ Hash
private
Returns a structured summary of all statements, including transaction counts, credit/debit totals (in cents), and closing balance.
-
#transactions ⇒ TransactionCollection
private
Returns all transactions aggregated across all statements.
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.
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
#filename ⇒ Array<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.
26 27 28 |
# File 'lib/ofx_kit/parser.rb', line 26 def filename @filename end |
#statements ⇒ Array<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.
26 27 28 |
# File 'lib/ofx_kit/parser.rb', line 26 def statements @statements end |
Instance Method Details
#account ⇒ 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 the account for files containing a single statement.
36 37 38 39 40 41 42 |
# File 'lib/ofx_kit/parser.rb', line 36 def account if statements.length > 1 raise MultipleStatementsError, 'File contains multiple statements. Use `accounts` to get all accounts.' end statements.first&.account end |
#accounts ⇒ Array<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.
45 46 47 |
# File 'lib/ofx_kit/parser.rb', line 45 def accounts statements.map(&:account) end |
#balance ⇒ Balance?
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.
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 |
#balances ⇒ Array<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.
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 |
#headers ⇒ Hash
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.
29 30 31 |
# File 'lib/ofx_kit/parser.rb', line 29 def headers @document.headers end |
#summary ⇒ Hash
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.
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.account txns = stmt.transactions h[acct.account_id] = { 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 |
#transactions ⇒ TransactionCollection
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 |