Created
March 28, 2010 22:13
-
-
Save lukeredpath/347074 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'mash' | |
module CoopBanking | |
class Statement | |
COLUMNS = [:date, :description, :credit, :debit, :balance] | |
attr_reader :transactions | |
def initialize(transactions) | |
@transactions = transactions | |
end | |
def self.parse(io) | |
statement = Nokogiri::HTML(io).xpath("//td[@class='transData'][text()='BROUGHT FORWARD']/ancestor::table[1]") | |
transactions = statement.xpath("//td[@class='dataRowL']/parent::tr").map do |row| | |
data = row.xpath("./td/text()").map do |r| | |
value = r.to_s.strip | |
case r.parent.attribute('class').value | |
when /data/ | |
value | |
when /trans/ | |
value.squeeze(' ') | |
when /money/ | |
value.gsub(/[\D]/, '').to_f / 100 | |
end | |
end | |
Mash.new(Hash[*COLUMNS.zip(data).flatten]) | |
end | |
new(transactions) | |
end | |
end | |
end | |
# example: | |
# statement = CoopBanking::Statement.parse(ARGF) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment