Class: CommerceBank

Inherits:
Object show all
Defined in:
lib/commercebank.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommerceBank

Returns a new instance of CommerceBank.



72
73
74
75
76
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
# File 'lib/commercebank.rb', line 72

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.parse('1/1/2000').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'])
end

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



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

def available
  @available
end

#currentObject (readonly)

Returns the value of attribute current.



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

def current
  @current
end

#pendingObject (readonly)

Returns the value of attribute pending.



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

def pending
  @pending
end

#registerObject (readonly)

Returns the value of attribute register.



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

def register
  @register
end

Instance Method Details

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

Yields:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/commercebank.rb', line 110

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)


128
129
130
131
132
133
134
135
# File 'lib/commercebank.rb', line 128

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