Class: CoopScraper::CreditCard

Inherits:
Object
  • Object
show all
Extended by:
Base
Defined in:
lib/coop_scraper/credit_card.rb

Class Method Summary collapse

Methods included from Base

coop_date_to_time

Class Method Details

.determine_trntype(details) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/coop_scraper/credit_card.rb', line 29

def determine_trntype(details)
  case details
  when /^OVERLIMIT CHARGE$/
    :service_charge
  when /^MERCHANDISE INTEREST AT [0-9]+\.[0-9]+% PER MTH$/
    :interest
  else
    nil
  end
end

.extract_account_number(doc) ⇒ Object



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

def (doc)
  doc.at("h4[text()*='TRAVEL CARD']").inner_text.match(/([0-9]{16})/)[1]
end

.extract_available_credit(doc) ⇒ Object



25
26
27
# File 'lib/coop_scraper/credit_card.rb', line 25

def extract_available_credit(doc)
  doc.at("td[text()='Available Credit'] ~ td").inner_text.match(/[0-9.]+/).to_s
end

.extract_statement_balance(doc) ⇒ Object



19
20
21
22
23
# File 'lib/coop_scraper/credit_card.rb', line 19

def extract_statement_balance(doc)
  amount, sign = doc.at("td[text()='Statement Balance'] ~ td").inner_text.match(/([0-9.]+) *(DR)?/).captures
  amount = "-#{amount}" if sign == "DR"
  amount
end

.extract_statement_date(doc) ⇒ Object



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

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

.extract_transaction(statement_row, date) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/coop_scraper/credit_card.rb', line 67

def extract_transaction(statement_row, date)
  details = statement_row.at('td.transData').inner_text.strip
  credit = statement_row.css('td.moneyData').first.inner_text.match(/[0-9.]+/)
  debit = statement_row.css('td.moneyData').last.inner_text.match(/[0-9.]+/)
  amount = credit.nil? ? "-#{debit}" : credit.to_s
  {:date => date, :amount => amount, :details => details}
end

.extract_transactions(doc, statement) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/coop_scraper/credit_card.rb', line 40

def extract_transactions(doc, statement)
  transactions = []
  current_transaction = {}
  doc.search('tbody.contents tr').each do |statement_row|
    date = coop_date_to_time(statement_row.at('td.dataRowL').inner_text)
    unless date.nil?
      current_transaction = extract_transaction(statement_row, date)
      transactions << current_transaction
    else
      transaction = extract_transaction(statement_row, statement.date)
      if transaction[:details].match(/OVERLIMIT CHARGE/)
        transactions << transaction
      else
        conversion_details = transaction[:details]
        current_transaction[:conversion] = conversion_details unless conversion_details.match(/ESTIMATED INTEREST/)
      end
    end
  end
  transactions.collect do |t| 
    options = {}
    options[:memo] = t[:conversion] if t[:conversion]
    trntype = determine_trntype(t[:details])
    options[:trntype] = trntype unless trntype.nil?
    OFX::Statement::Transaction.new(t[:amount], t[:date], t[:details], options)
  end
end

.generate_statement(html_statement_io, server_response_time) ⇒ Object



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

def generate_statement(html_statement_io, server_response_time)
  doc = Nokogiri::HTML(html_statement_io)
  statement = OFX::Statement::CreditCard.new
  
  statement.server_response_time = server_response_time
  statement. = (doc)
  statement.date = extract_statement_date(doc)
  statement.ledger_balance = extract_statement_balance(doc)
  statement.available_credit = extract_available_credit(doc)

  extract_transactions(doc, statement).each { |transaction| statement << transaction }

  statement.start_date = statement.transactions.first.date
  statement.end_date = statement.date
  statement
end