Created
October 25, 2012 19:29
-
-
Save gnab/3954877 to your computer and use it in GitHub Desktop.
Ruter MinSide Ruby wrapper for retrieving travel card information
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
# encoding: utf-8 | |
require 'mechanize' | |
class Ruter | |
class NotLoggedInError < StandardError; end | |
def self.log_in(username, password) | |
agent = Mechanize.new | |
agent.ca_file = '/usr/lib/ssl/certs/ca-certificates.crt' | |
page = agent.get('https://www2.ruter.no/minside/logg-inn/') | |
form = page.form_with(:action => '/minside/logg-inn/') | |
form.field_with(:name => /UserName$/).value = username | |
form.field_with(:name => /Password$/).value = password | |
submit = form.button_with(:value => "Logg inn") | |
@@page = form.click_button(submit) | |
end | |
def self.get_cards | |
throw NotLoggedInError.new unless defined?(@@page) | |
cards = @@page.search('.travelCard:not(.travelCardBlocked)') | |
cards.map do |card| | |
{ | |
serial: (card / 'h3:contains("Kortnummer") ~ span').text.strip, | |
name: (card / 'h2').text.strip, | |
expiry: (card / 'table td:contains("Utløper") ~ td').text.strip | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment