Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created March 28, 2010 22:13
Show Gist options
  • Save lukeredpath/347074 to your computer and use it in GitHub Desktop.
Save lukeredpath/347074 to your computer and use it in GitHub Desktop.
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