Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First pass at support for multiple accounts

  • Loading branch information...
commit a18f1665b772491d166f10ebd42cce49000e4747 1 parent ddfcbc3
runpaint authored
Showing with 34 additions and 41 deletions.
  1. +12 −8 bin/nw
  2. +22 −33 lib/natwest.rb
20 bin/nw
View
@@ -26,14 +26,18 @@ credentials = YAML.load(File.read(CONFIG)) rescue {}
end
end
-Natwest::Account.new.tap do |nw|
+Natwest::Customer.new.tap do |nw|
nw.login credentials
- puts "#{nw.account_number} (#{nw.sort_code}) " +
- "balance: #{nw.balance}; available: #{nw.available}"
- puts "\nRecent Transactions:"
- nw.recent_transactions.each do |trans|
- amount = trans[:credit] ? "+#{trans[:credit]}" : "-#{trans[:debit]}"
- puts "#{trans[:date]}: #{amount}"
- puts "\t" + trans[:details]
+ nw.accounts.each do |acc|
+ puts '###'
+ puts "#{acc.name} [#{acc.number}; #{acc.sort_code}] " +
+ "balance: #{acc.balance}; available: #{acc.available}"
+ puts "\nRecent Transactions:"
+ acc.transactions.each do |trans|
+ amount = trans[:credit] ? "+#{trans[:credit]}" : "-#{trans[:debit]}"
+ puts "#{trans[:date]}: #{amount}"
+ puts "\t" + trans[:details]
+ end
+ puts
end
end
55 lib/natwest.rb
View
@@ -64,7 +64,7 @@ def expected(credential, type)
end
end
- class Account
+ class Customer
include Login
NO_DETAILS = 'No further transaction details held'
attr_accessor :page
@@ -73,40 +73,29 @@ def initialize
@ua = Mechanize.new {|ua| ua.user_agent_alias = 'Windows IE 7'}
end
- def meta_row(field=nil)
- assert(logged_in?, "Not logged in")
- @meta_row ||= page.parser.
- css('table#ctl00_mainContent_Accounts > tbody > tr').
- first
- return @meta_row unless field
- @meta_row.css("td > span.#{field} > span").first.inner_text.tr(' ','')
- end
-
- def account_number
- meta_row('AccountNumber').to_i
- end
-
- def sort_code
- meta_row('SortCode')
- end
-
- def balance
- meta_row.css('td')[3].inner_text
- end
-
- def available
- meta_row.css('td')[4].inner_text
- end
-
- def recent_transactions
- page.parser.css('table.InnerAccountTable > tbody > tr').map do |tr|
- transaction = Hash[[:date, :details, :credit, :debit].
- zip((cells = tr.css('td')).map(&:inner_text))]
- unless (further = cells[1]['title']) == NO_DETAILS
- transaction[:details] += " (#{further.squeeze(' ')})"
+ def accounts
+ page.parser.css('table.AccountTable > tbody > tr').each_slice(2).map do |meta, statement|
+ Account.new.tap do |acc|
+ acc.name = meta.at('td > span.AccountName').inner_text
+ acc.number = meta.at('td > span.AccountNumber').inner_text.gsub(/[^\d]/,'')
+ acc.sort_code = meta.at('td > span.SortCode').inner_text.gsub(/[^\d-]/,'')
+ acc.balance = meta.css('td')[-2].inner_text
+ acc.available = meta.css('td')[-1].inner_text
+ acc.transactions =
+ statement.css('table.InnerAccountTable > tbody > tr').map do |tr|
+ transaction = Hash[[:date, :details, :credit, :debit].
+ zip((cells = tr.css('td')).map(&:inner_text))]
+ unless (further = cells[1]['title']) == NO_DETAILS
+ transaction[:details] += " (#{further.squeeze(' ')})"
+ end
+ Hash[transaction.map{|k,v| [k, v == ' - ' ? nil : v]}]
+ end
end
- Hash[transaction.map{|k,v| [k, v == ' - ' ? nil : v]}]
end
end
end
+
+ class Account
+ attr_accessor :name, :number, :sort_code, :balance, :available, :transactions
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.