Class: CommerceBank

Inherits:
Object show all
Defined in:
lib/commercebank.rb,
lib/commerce-bank-client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommerceBank

Returns a new instance of CommerceBank.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/commercebank.rb', line 77

def initialize
  @config = AppConfig.new('~/.commerce.yaml')

  client = WebClient.new

  client.get('/')

  client.get('/CBI/login.aspx', 'MAINFORM')

  client.fields['txtUserID'] = @config.get('username')
  response = client.post('/CBI/login.aspx', 'MAINFORM')

  # If a question was asked, answer it then get the password page.
  question = response.body.scan(/Your security question:&nbsp;&nbsp;(.*?)<\/td>/i).first.andand.first
  if question
    client.fields['txtChallengeAnswer'] = @config.get(question)
    client.fields['saveComputer'] = 'rdoBindDeviceNo'
    response = client.post('/CBI/login.aspx', 'MAINFORM')
  end

  raise "could not reach the password page" unless client.fields['__EVENTTARGET'] == 'btnLogin'

  client.fields['txtPassword'] = @config.get('password')
  response = client.post('/CBI/login.aspx')

  response = client.get('/CBI/Accounts/CBI/Activity.aspx', 'MAINFORM')
  (@current, @available) = parse_balance(response.body)
  @pending = parse_pending(response.body)

  client.fields['Anthem_UpdatePage'] = 'true'
  client.fields['txtFilterFromDate:textBox'] = (Time.now - 30.days).strftime('%m/%d/%Y')
  client.fields['txtFilterToDate:textBox'] = Time.now.strftime('%m/%d/%Y')
  response = client.post('/CBI/Accounts/CBI/Activity.aspx?Anthem_CallBack=true')

  raw_data = JSON.parse(response.body)
  @register = parse_register(raw_data['controls']['pnlPosted'])

  @register.each do |entry|
    entry[:images].map! do |image_url|
      url = "https://banking.commercebank.com/CBI/Accounts/CBI/#{image_url[:url]}"
      client.get(url).andand.body
    end
    entry[:images].compact!
  end
end

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



75
76
77
# File 'lib/commercebank.rb', line 75

def available
  @available
end

#currentObject (readonly)

Returns the value of attribute current.



75
76
77
# File 'lib/commercebank.rb', line 75

def current
  @current
end

#pendingObject (readonly)

Returns the value of attribute pending.



75
76
77
# File 'lib/commercebank.rb', line 75

def pending
  @pending
end

#registerObject (readonly)

Returns the value of attribute register.



75
76
77
# File 'lib/commercebank.rb', line 75

def register
  @register
end

Instance Method Details

#daily_summary {|'Pending', @pending| ... } ⇒ Object

Yields:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/commercebank.rb', line 123

def daily_summary
  today, yesterday, this_week, last_week = [], [], [], []

  register.each do |entry|
    if    entry[:date] == Date.today                               then today << entry
    elsif entry[:date] == (Date.today - 1)                         then yesterday << entry
    elsif entry[:date] >= Date.today.last_sunday                   then this_week << entry
    elsif entry[:date] >= (Date.today.last_sunday - 1).last_sunday then last_week << entry
    end
  end

  yield 'Pending', @pending
  yield 'Today', today
  yield 'Yesterday', yesterday
  yield 'This Week', this_week
  yield 'Last Week', last_week
end

#monthly_summary(day_in_month = (Date.today - Date.today.day)) {|day_in_month.strftime('%B'), entries| ... } ⇒ Object

Yields:

  • (day_in_month.strftime('%B'), entries)


141
142
143
144
145
146
147
148
# File 'lib/commercebank.rb', line 141

def monthly_summary(day_in_month = (Date.today - Date.today.day))
  first_of_month = day_in_month - day_in_month.day + 1
  last_of_month = first_of_month + day_in_month.days_in_month - 1

  entries = register.find_all {|entry| entry[:date] >= first_of_month && entry[:date] <= last_of_month}

  yield day_in_month.strftime('%B'), entries
end