Revisions

gist: 113139 Download_button fork
public
Public Clone URL: git://gist.github.com/113139.git
Embed All Files: show embed
orca_client.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
59
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