Skip to content

Instantly share code, notes, and snippets.

@nikz
Created December 1, 2008 00:53
Show Gist options
  • Save nikz/30572 to your computer and use it in GitHub Desktop.
Save nikz/30572 to your computer and use it in GitHub Desktop.
require 'mechanize'
# scrapes Xero for our bank balances and stuff
class XeroScraper
XERO_USERNAME = "xero username"
XERO_PASSWORD = "xero password"
class Account
attr_accessor :name, :balance, :unreconciled_count
@accounts = []
end
def self.save_account_data!
# lol
BankAccount.delete_all
self.new.accounts.each do |a|
BankAccount.create!(:name => a.name,
:balance => a.balance,
:unreconciled_count => a.unreconciled_count,
:account => ::Account.find(:first))
# w00t
end
end
def initialize
@agent = WWW::Mechanize.new
end
def accounts
login
dashboard = @agent.get("https://go.xero.com/Dashboard/")
@accounts = dashboard.search("div.bank").collect do |bank_div|
returning Account.new do |a|
a.name = bank_div.search("div.account a.name").inner_html[/([A-z0-9\s_-]+)<br \/>/, 1]
a.balance = bank_div.search("div.details div span#BalanceAtXero").inner_html.gsub(/[^0-9\.]/, '').to_f
a.unreconciled_count = bank_div.search("div.details div.no-top-padding").inner_html[/([0-9]+)/, 1].to_i
end
end
end
private
def login
page = @agent.get("https://go.xero.com/Login/Login.aspx?bhcp=1")
login_form = page.form("frmMain")
login_form['UserName'] = XERO_USERNAME
login_form['Password'] = XERO_PASSWORD
@agent.submit(login_form, login_form.buttons.first)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment