require 'nokogiri'
require 'mechanize'
###
# Get information about your Orca Card using this class. See
# http://orcacard.com/ for more details about Orca Cards. (Hint, this probably
# isn't useful unless you live in Seattle).
#
# Example:
#
# client = OrcaClient.new
# client.login('username', 'password')
# oc.history_for_card.each do |info|
# p info
# end
class OrcaClient
def initialize
@agent = WWW::Mechanize.new
@page = nil
end
def login username, password
@page = @agent.get('https://www.orcacard.com/ERG-Seattle/p7_002ad.do?m=52')
@page = @page.form('loginform') { |form|
form.j_username = username
form.j_password = password
}.submit
end
def history_for_card number = /^\d+$/
@page = @page.link_with(:text => number).click
@page = @page.link_with(:text => /Transaction History/).click
# Why use XSLT? *shrug* just to see if I could
xslt = Nokogiri::XSLT(<<-eoxslt)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
---
<xsl:for-each select=".//table[@id = 'printableTable']//tr">
<xsl:for-each select="td">
<xsl:if test="normalize-space(.) != '-' and normalize-space(.) != ''" >
<xsl:choose>
<xsl:when test="position() = 1">
- - <xsl:value-of select="normalize-space(.)" />
</xsl:when>
<xsl:otherwise>
- <xsl:value-of select="normalize-space(.)" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
eoxslt
YAML.load(xslt.transform(@page.parser).content)
end
end