I bank with Lloyds TSB - I have my current account and credit card with them. Like most online banking services though, they're not to up-to-date on APIs and the like. After looking around online, I found that there were a couple of scripts that people had built, but I didn't have much luck with them myself. So I decided to build my own screen scraper.
I know the code in this is pretty messy, and as ever, it's untested. I tried to refactor it and got to the end, but then it turned out to be broken and I couldn't be bothered to fix it. So I've left it for now.
The file example.rb
provides a very simple example of how the code works, but here's a step by step:
- Ensure the gem is installed, and then include it in your Ruby file, or in your Gemfile where appropriate:
$ gem install lloydstsb
`require 'lloydstsb'
- Create a hash with three symbol keys,
:username
,:password
and:memorable_word
, each unsurprisingly corresponding to different authentication details used
@settings = {
username: "123456789",
password: "a secure password",
memorable_word: "banking"
}
- Instantiate a new instance of the
LloydsTSB::Customer
object, passing in the hash from the previous step - this is used to perform the authentication required.
customer = LloydsTSB::Customer.new(@settings)
- Call the
accounts
method of the object you just made - it'll take a few seconds, and will return a number ofLloydsTSB::Account
objects. Play with the response as you wish.
puts customer.name
customer.accounts
customer.accounts.first.transactions
A LloydsTSB::Customer is created with LloydsTSB::Customer.new
with a hash of settings passed in. It has the following attributes:
- agent (Mechanize::Agent) - the Mechanize agent used to browse around the online banking system. This will be pointing at the "Your accounts" page.
- name (string) - the name of the customer
- accounts (array) - an array of LloydsTSB::Account objects representing accounts held by the customer
A LloydsTSB::Account instance has the following attributes:
- name (string) - the name of the account
- balance (integer) - the balance of the account, whether positive or negative. *(NB: The true meaning of balance is affected by whether the account is a :credit_card or a :bank_account)
- limit (integer) - the credit limit for the account - this is an overdraft limit for a current account, or the spending limit on a credit card
- transactions (array) - an array containing a number of
LloydsTSB::Transaction
object - this will be the 20(?) most recent transactions on the account - details (hash)__ - the identifying information for the account as a hash. For a bank account, this will have keys :account_number and :sort_code, with :card_number for credit cards
- type (symbol) - the type of the account, either
:credit_card
or:bank_account
A LloydsTSB::Account has many LloydsTSB::Transaction instances in its transactions property. Each transaction has the following attributes:
- date (Date) - the date of the transaction as shown on the statement
- narrative (string) - a description of the transaction, most likely the name of the merchant
- type (symbol) - the type of transaction, usually an acronym - a list is available on the Lloyds TSB site
- direction (symbol) - either
:credit
or:debit
, depending on what the transaction is - amount (integer) - The amount of the transaction, obviously...
- unique_reference (string)_ - a hash to identify this transaction (fairly) uniquely...useful if you want to see whether a transaction is new or not
- I haven't tested this with savings account, so it may well mess the script up and cause exceptions. I'll need to open a savings account to test this.
- It will only show a limited number of transactions - it doesn't navigate through the different pages
Use this for what you will, as long as it isn't evil. If you make any changes or cool improvements, please let me know at [email protected].