Class: HsbcPdfStatementParser::Parser
- Inherits:
-
Object
- Object
- HsbcPdfStatementParser::Parser
- Defined in:
- lib/hsbc_pdf_statement_parser/parser.rb
Instance Method Summary collapse
-
#closing_balance ⇒ Object
Returns the closing balance of the statement read from the table on the first page.
-
#initialize(filename) ⇒ Parser
constructor
Creates a new parser from a PDF file.
-
#opening_balance ⇒ Object
Returns the opening balance of the statement read from the table on the first page.
-
#payments_in ⇒ Object
Returns the total value of payments in during the statement read from the table on the first page (ie: not calculated).
-
#payments_out ⇒ Object
Returns the total value of payments out during the statement read from the table on the first page (ie: not calculated).
-
#transactions ⇒ Object
Returns an array of the transactions in the document as hashes.
Constructor Details
#initialize(filename) ⇒ Parser
Creates a new parser from a PDF file.
Parameters
- filename
-
the filename to parse
8 9 10 11 12 |
# File 'lib/hsbc_pdf_statement_parser/parser.rb', line 8 def initialize( filename ) @reader = PDF::Reader.new( filename ) end |
Instance Method Details
#closing_balance ⇒ Object
Returns the closing balance of the statement read from the table on the first page.
68 69 70 71 72 |
# File 'lib/hsbc_pdf_statement_parser/parser.rb', line 68 def closing_balance @_closing_balance ||= scan_figure( 'Closing Balance' ) end |
#opening_balance ⇒ Object
Returns the opening balance of the statement read from the table on the first page.
61 62 63 64 65 |
# File 'lib/hsbc_pdf_statement_parser/parser.rb', line 61 def opening_balance @_opening_balance ||= scan_figure( 'Opening Balance' ) end |
#payments_in ⇒ Object
Returns the total value of payments in during the statement read from the table on the first page (ie: not calculated)
75 76 77 78 79 |
# File 'lib/hsbc_pdf_statement_parser/parser.rb', line 75 def payments_in @_payments_in ||= scan_figure( 'Payments In' ) end |
#payments_out ⇒ Object
Returns the total value of payments out during the statement read from the table on the first page (ie: not calculated)
82 83 84 85 86 |
# File 'lib/hsbc_pdf_statement_parser/parser.rb', line 82 def payments_out @_payments_out ||= scan_figure( 'Payments Out' ) end |
#transactions ⇒ Object
Returns an array of the transactions in the document as hashes.
Hash keys
- :date
-
the date of the transaction _(Date)_
- :type
-
the type of the transaction, eg ‘VISA’, ‘DD’, ‘ATM’, etc _(String)_
- :details
-
the details of the transaction. This can span multiple lines _(String)_
- :out
-
the amount of the transaction, if a debit _(Float, nil)_
- :in
-
the amount of the transaction, if a credit _(Float, nil)_
- :change
-
the amount of the transacation: negative if a debit, positive if a credit _(Float)_
24 25 26 27 28 29 30 31 32 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 |
# File 'lib/hsbc_pdf_statement_parser/parser.rb', line 24 def transactions @_transactions ||= begin current_transaction = nil current_date = nil transactions = [] document_text .scan( /BALANCE\s?BROUGHT\s?FORWARD(?:.*?)\n(.*?)BALANCE\s?CARRIED\s?FORWARD/im ) .map{ |text| parse_page( text[0] )} .flatten .each do |line| # store the current date current_date = line[:date] unless line[:date].nil? # if we have a type, start a new transaction unless line[:type].nil? transactions << current_transaction unless current_transaction.nil? current_transaction = line.merge( date: current_date ) next end # merge things in current_transaction.merge!( line.select{ |k,v| v }, { details: "#{current_transaction[:details]}\n#{line[:details]}" }) end # dump the final transaction + return transactions << current_transaction unless current_transaction.nil? transactions end end |