Module: CoopScraper::CurrentAccount

Extended by:
Base
Defined in:
lib/coop_scraper/current_account.rb

Class Method Summary collapse

Methods included from Base

coop_date_to_time

Class Method Details

.determine_trntype(details) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coop_scraper/current_account.rb', line 32

def determine_trntype(details)
  case details
  when /^DEBIT INTEREST$/
    :interest
  when /^LINK +[0-9]{2}:[0-9]{2}[A-Z]{3}[0-9]{2}$/
    :atm
  when /^SERVICE CHARGE$/
    :service_charge
  when /^TFR [0-9]{14}$/
    :transfer
  else
    nil
  end
end

.extract_account_details(doc) ⇒ Object



11
12
13
# File 'lib/coop_scraper/current_account.rb', line 11

def (doc)
  doc.at_xpath("//h4[contains(., 'CURRENT ACCOUNT') or contains(., 'PRIVILEGE') or contains(., 'PRIV SAVINGS')]").inner_text
end

.extract_account_number(doc) ⇒ Object



15
16
17
# File 'lib/coop_scraper/current_account.rb', line 15

def (doc)
  (doc).match(/([0-9]{8})/)[1]
end

.extract_closing_balance(doc) ⇒ Object



66
67
68
69
70
71
# File 'lib/coop_scraper/current_account.rb', line 66

def extract_closing_balance(doc)
  final_transaction = extract_transaction_rows(doc).last.css('td.moneyData').last.inner_text
  amount = final_transaction.match(/[0-9.]+/).to_s
  sign = final_transaction.match(/[CD]R/).to_s
  sign == "CR" ? amount : "-#{amount}"
end

.extract_sort_code(doc) ⇒ Object



19
20
21
# File 'lib/coop_scraper/current_account.rb', line 19

def extract_sort_code(doc)
  (doc).match(/([0-9]{2}-[0-9]{2}-[0-9]{2})/)[1].tr('-', '')
end

.extract_statement_date(doc) ⇒ Object



23
24
25
# File 'lib/coop_scraper/current_account.rb', line 23

def extract_statement_date(doc)
  coop_date_to_time(doc.at("td[text()^='Date']").inner_text)
end

.extract_statement_start_date(doc) ⇒ Object



73
74
75
# File 'lib/coop_scraper/current_account.rb', line 73

def extract_statement_start_date(doc)
  coop_date_to_time(extract_transaction_rows(doc).first.at('td.dataRowL').inner_text)
end

.extract_transaction_rows(doc) ⇒ Object



27
28
29
30
# File 'lib/coop_scraper/current_account.rb', line 27

def extract_transaction_rows(doc)
  a_td = doc.at('td.transData')
  a_td.parent.parent.css('tr')
end

.extract_transactions(doc) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/coop_scraper/current_account.rb', line 47

def extract_transactions(doc)
  transactions = []
  a_td = doc.at('td.transData')
  transaction_rows = extract_transaction_rows(doc)
  first_row = transaction_rows.shift
  transaction_rows.each do |statement_row|
    date = statement_row.at('td.dataRowL').inner_text
    details = statement_row.at('td.transData').inner_text.strip
    credit = statement_row.css('td.moneyData')[0].inner_text.match(/[0-9.]+/)
    debit = statement_row.css('td.moneyData')[1].inner_text.match(/[0-9.]+/)
    amount = credit.nil? ? "-#{debit}" : credit.to_s
    options = {}
    trntype = determine_trntype(details)
    options[:trntype] = trntype unless trntype.nil?
    transactions << OFX::Statement::Transaction.new(amount, coop_date_to_time(date), details, options)
  end
  transactions
end

.generate_statement(html_statement_io, server_response_time) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/coop_scraper/current_account.rb', line 77

def generate_statement(html_statement_io, server_response_time)
  doc = Nokogiri::HTML(html_statement_io)
  statement = OFX::Statement::CurrentAccount.new
  
  statement.server_response_time = server_response_time
  statement. = (doc)
  statement.sort_code = extract_sort_code(doc)
  statement.date = extract_statement_date(doc)
  statement.ledger_balance = extract_closing_balance(doc)
  
  extract_transactions(doc).each { |transaction| statement << transaction }
  
  statement.start_date = extract_statement_start_date(doc)
  statement.end_date = extract_statement_date(doc)
  statement
end