class BankStatementParser::Base
Base
class for statement parsers
Subclasses must implement the following instance methods
-
bool
handle_line
(String line)
Subclasses may override the following instance methods, but must remember to call the base class method from the override
-
void reset()
Attributes
bank_statement[RW]
Public Class Methods
new()
click to toggle source
Constructor
# File lib/bank_statement_parser/base.rb, line 40 def initialize reset end
Public Instance Methods
handle_line(line)
click to toggle source
Handle the specified line
# File lib/bank_statement_parser/base.rb, line 93 def handle_line(line) raise NotImplementedError end
parse(path)
click to toggle source
Parse the specified text file
# File lib/bank_statement_parser/base.rb, line 45 def parse path reset full_text = case path when String # Is the path a URI? if path =~ URI::regexp(%w(ftp http https)) begin open(path).read rescue OpenURI::HTTPError => e raise "Failed to read URI #{path}: #{e}" end else raise "Expected a text file path" unless path =~ /\.txt\z/ # Grab the full text file content (utf-8) File.read(path) end when File, IO, StringIO, Tempfile path.rewind path.read when Pathname, URI path.read else if path.respond_to?(:read) path.read else raise ArgumentError, "Expected String, IO, Pathname or URI" end end # Process each line in turn full_text.split("\n").each do |line| break unless handle_line(line) end # Sanity checking raise "Failed to find sort code" if sort_code.nil? raise "Failed to find account number" if account_number.nil? raise "Failed to find statement date" if statement_date.nil? raise "Failed to find account name" if name.nil? raise "Failed to find opening balance" if opening_balance.nil? raise "Failed to find closing balance" if closing_balance.nil? self end
Protected Instance Methods
account_number()
click to toggle source
# File lib/bank_statement_parser/base.rb, line 130 def account_number @bank_statement.account_number end
account_number=(account_number)
click to toggle source
# File lib/bank_statement_parser/base.rb, line 134 def account_number= account_number @bank_statement.account_number = account_number end
add_record(record)
click to toggle source
# File lib/bank_statement_parser/base.rb, line 162 def add_record record @bank_statement.records << record end
closing_balance()
click to toggle source
# File lib/bank_statement_parser/base.rb, line 154 def closing_balance @bank_statement.closing_balance end
closing_balance=(closing_balance)
click to toggle source
# File lib/bank_statement_parser/base.rb, line 158 def closing_balance= closing_balance @bank_statement.closing_balance = closing_balance end
logger()
click to toggle source
Convenience method to access the logger
# File lib/bank_statement_parser/base.rb, line 100 def logger BankStatementParser.logger end
name()
click to toggle source
@todo FIXME: Why can't we use Forwardable for these methods?
Partially works, but doesn't seem to be accessible from subclasses…
# File lib/bank_statement_parser/base.rb, line 114 def name @bank_statement.name end
name=(name)
click to toggle source
# File lib/bank_statement_parser/base.rb, line 118 def name= name @bank_statement.name = name end
opening_balance()
click to toggle source
# File lib/bank_statement_parser/base.rb, line 146 def opening_balance @bank_statement.opening_balance end
opening_balance=(opening_balance)
click to toggle source
# File lib/bank_statement_parser/base.rb, line 150 def opening_balance= opening_balance @bank_statement.opening_balance = opening_balance end
reset()
click to toggle source
Reset the parser
# File lib/bank_statement_parser/base.rb, line 105 def reset @bank_statement = BankStatement.new self end
sort_code()
click to toggle source
# File lib/bank_statement_parser/base.rb, line 122 def sort_code @bank_statement.sort_code end
sort_code=(sort_code)
click to toggle source
# File lib/bank_statement_parser/base.rb, line 126 def sort_code= sort_code @bank_statement.sort_code = sort_code end
statement_date()
click to toggle source
# File lib/bank_statement_parser/base.rb, line 138 def statement_date @bank_statement.statement_date end
statement_date=(statement_date)
click to toggle source
# File lib/bank_statement_parser/base.rb, line 142 def statement_date= statement_date @bank_statement.statement_date = statement_date end