Skip to content

Instantly share code, notes, and snippets.

@jasonmadigan
Created October 9, 2009 09:00
Show Gist options
  • Save jasonmadigan/205875 to your computer and use it in GitHub Desktop.
Save jasonmadigan/205875 to your computer and use it in GitHub Desktop.
Firefox is slow to start, so here's a WITCard balance checker
#!/usr/bin/env ruby
# coding: utf-8
require 'rubygems'
require 'mechanize'
USERNAME = '<USERNAME>'
PASSWORD = '<PASSWORD>'
mech = Mechanize.new
login_page = mech.get('https://icmserver.wit.ie/balance/balanceentry.jsp')
login_form = login_page.forms[0]
login_fields = login_form.fields
username_field = login_fields[0]
password_field = login_fields[1]
username_field.value = USERNAME
password_field.value = PASSWORD
login_form.radiobuttons.last.click # Staff
# Login
login_form.submit
transactions_page = mech.page
transactions_doc = Nokogiri::HTML(transactions_page.send(:html_body)) # html_body() is private
balance_span = transactions_doc.search('div.balance-text').first.inner_html
balance = balance_span.split("&#8364\;")[1]
puts "Your balance is €#{balance}"
#!/usr/bin/env ruby
# coding: utf-8
require 'rubygems'
require 'mechanize'
# Constants
CARD_TYPES = {
:laser => 'LASER',
:mastercard => 'MC',
:visa => 'VISA'
}
BASE_URL = 'https://icmserver.wit.ie/balance/balanceentry.jsp'
ACCEPT_URL = 'https://icmserver.wit.ie/balance/transferaccept.jsp'
# Settings
USERNAME = '<Username>'
PASSWORD = '<Password>'
CARD_NUMBER = '<Credit/Debitcard number>'
CARD_TYPE = CARD_TYPES[:mastercard]
CVN = '<Verification Number>'
EXPIRATION_MONTH = 11 # For Jan, use 01, Feb: 02 etc.
EXPIRATION_YEAR = 13
CARD_HOLDER_NAME = '<Cardholder Name>'
TOPUP_AMOUNT = 1000 # In cent
mech = Mechanize.new
login_page = mech.get(BASE_URL)
login_form = login_page.forms[0]
login_fields = login_form.fields
username_field = login_fields[0]
password_field = login_fields[1]
username_field.value = USERNAME
password_field.value = PASSWORD
login_form.radiobuttons.last.click # Staff
# Login
login_form.submit
view_transfer = mech.page.links[2]
view_transfer.click
transfer_page = mech.page
transfer_form = transfer_page.forms.first
transfer_form.field_with(:name => 'cardHolderName').value = CARD_HOLDER_NAME
transfer_form.field_with(:name => 'cardCVN').value = CVN
transfer_form.field_with(:name => 'cardNumber').value = CARD_NUMBER
transfer_form.field_with(:name => 'amount').value = TOPUP_AMOUNT
transfer_form.field_with(:name => 'cardType').value = CARD_TYPE
transfer_form.field_with(:name => 'cardExpMonth').value = EXPIRATION_MONTH
transfer_form.field_with(:name => 'cardExpYear').value = EXPIRATION_YEAR
transfer_form.checkbox_with(:name => 'ClickTandC').check
transfer_form.submit
confirmation_page = mech.page
# A GET to some other page instead of just submitting a form?
# Who the hell knows.
accepted_page = mech.get(ACCEPT_URL)
accepted_doc = Nokogiri::HTML(accepted_page.send(:html_body))
bal_text = accepted_doc.search('p.bodytextlarge').inner_html
balance = bal_text.split("&#8364\;")[1].match(/(\d.)/)
puts "Your new balance is €#{balance[0]}.#{balance[1]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment