Class: Natwest::Customer

Inherits:
Object
  • Object
show all
Includes:
Login
Defined in:
lib/natwest.rb

Constant Summary collapse

NO_DETAILS =
'No further transaction details held'

Instance Attribute Summary collapse

Attributes included from Login

#customer_number, #password, #pin, #ua

Instance Method Summary collapse

Methods included from Login

#logged_in?, #login

Constructor Details

#initializeCustomer

Returns a new instance of Customer.



72
73
74
# File 'lib/natwest.rb', line 72

def initialize
  @ua = Mechanize.new {|ua| ua.user_agent_alias = 'Windows IE 7'}
end

Instance Attribute Details

#pageObject

Returns the value of attribute page.



70
71
72
# File 'lib/natwest.rb', line 70

def page
  @page
end

Instance Method Details

#accountsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/natwest.rb', line 76

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
  end
end