alexmchale-commerce-bank-client 0.6.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :minor: 6
2
+ :minor: 8
3
3
  :major: 0
4
- :patch: 2
4
+ :patch: 0
data/lib/commercebank.rb CHANGED
@@ -11,42 +11,8 @@ require 'date'
11
11
  require 'json'
12
12
  require 'htmlentities'
13
13
  require 'gmail'
14
+ require 'appconfig'
14
15
  require 'commercebank/monkey.rb'
15
- require 'commercebank/appconfig.rb'
16
-
17
- class Array
18
- def binary
19
- map {|e| yield(e) ? [e, nil] : [nil, e]}.transpose.map {|a| a.compact}
20
- end
21
- end
22
-
23
- class Object
24
- def to_cents
25
- (to_s.gsub(/[^-.0-9]/, '').to_f * 100).to_i
26
- end
27
- end
28
-
29
- class Date
30
- def days_in_month
31
- (Date.parse("12/31/#{strftime("%Y")}") << (12 - month)).day
32
- end
33
-
34
- def last_sunday
35
- d = self
36
- d -= 1 until d.wday == 0
37
- d
38
- end
39
- end
40
-
41
- class Hash
42
- def to_url
43
- map {|key, value| "#{CGI.escape key.to_s}=#{CGI.escape value.to_s}"}.join "&"
44
- end
45
-
46
- def to_cookie
47
- map {|key, value| "#{key}=#{value}"}.join('; ')
48
- end
49
- end
50
16
 
51
17
  class WebClient
52
18
  attr_reader :fields, :cookies
@@ -151,120 +117,24 @@ class CommerceBank
151
117
  end
152
118
  end
153
119
 
154
- { 'Pending' => @pending,
155
- 'Today' => today,
156
- 'Yesterday' => yesterday,
157
- 'This Week' => this_week,
158
- 'Last Week' => last_week,
159
- :order => [ 'Pending', 'Today', 'Yesterday', 'This Week', 'Last Week' ] }
120
+ yield 'Pending', @pending
121
+ yield 'Today', today
122
+ yield 'Yesterday', yesterday
123
+ yield 'This Week', this_week
124
+ yield 'Last Week', last_week
160
125
  end
161
126
 
162
127
  def monthly_summary(day_in_month = (Date.today - Date.today.day))
163
128
  first_of_month = day_in_month - day_in_month.day + 1
164
129
  last_of_month = first_of_month + day_in_month.days_in_month - 1
165
- entries = register.find_all {|entry| entry[:date] >= first_of_month && entry[:date] <= last_of_month}
166
- { day_in_month.strftime('%B') => entries }
167
- end
168
-
169
- def print_all
170
- summarize 'All' => register
171
- end
172
-
173
- def print_daily_summary
174
- print(summarize(daily_summary))
175
- end
176
-
177
- def text_daily_summary
178
- summarize daily_summary
179
- end
180
-
181
- def html_daily_summary
182
- summarize_html daily_summary
183
- end
184
-
185
- def gmail_daily_summary
186
- subject = "Daily Summary"
187
130
 
188
- username = @config['GMail Username']
189
- password = @config['GMail Password']
190
-
191
- GMail.new(username, password).send(username, subject, html_daily_summary, 'text/html')
192
- end
193
-
194
- def gmail_monthly_summary
195
- last_month = Date.today - Date.today.day
196
- subject = "#{last_month.strftime('%B')} Summary"
197
- summary = summarize_html(monthly_summary(last_month))
198
-
199
- username = @config['GMail Username']
200
- password = @config['GMail Password']
131
+ entries = register.find_all {|entry| entry[:date] >= first_of_month && entry[:date] <= last_of_month}
201
132
 
202
- GMail.new(username, password).send(username, subject, summary, 'text/html')
133
+ yield day_in_month.strftime('%B'), entries
203
134
  end
204
135
 
205
136
  private
206
137
 
207
- def summarize(entries)
208
- (entries[:order] || entries.keys).map do |label|
209
- next if entries[label].length == 0
210
-
211
- label.to_s + ":\n" + entries[label].map do |e|
212
- [
213
- e[:date].strftime('%02m/%02d/%04Y '),
214
- "%-100s " % e[:destination],
215
- "%10s " % e[:delta].to_dollars(:show_plus),
216
- e[:total] && ("%10s " % e[:total].to_dollars),
217
- "\n"
218
- ].compact.join
219
- end.join
220
- end.compact.join("\n")
221
- end
222
-
223
- def summarize_html(entries)
224
- html = ''
225
-
226
- (entries[:order] || entries.keys).each do |label|
227
- next if entries[label].length == 0
228
-
229
- use_total = entries[label].find {|e| e[:total]}
230
-
231
- html += '<h2 style="font-family: garamond, georgia, serif">' + label + '</h2>'
232
-
233
- html += '<table cellspacing="0" cellpadding="5" style="font-size: 12px; border-style: solid; border-width: 2px; border-color: #DDDDDD" width="100%">'
234
-
235
- html += '<tr style="font-weight: bold; background-color: #DDDDDD">'
236
- html += '<th style="text-align: left" width="75">Date</th>'
237
- html += '<th style="text-align: left">Destination</th>'
238
- html += '<th style="text-align: right" width="75">Amount</th>'
239
- html += '<th style="text-align: right" width="75">Total</th>' if use_total
240
- html += '</tr>'
241
-
242
- even = true
243
- entries[label].each do |e|
244
- even = !even
245
-
246
- delta = "%s%0.2f" % [ (e[:delta] >= 0 ? '+' : '-'), e[:delta].abs/100.0 ]
247
- total = "%0.2f" % (e[:total].to_i/100.0)
248
-
249
- row_style = {
250
- 'font-weight' => 'normal',
251
- 'background-color' => even ? '#DDDDDD' : '#FFFFFF'
252
- }.map {|k, v| "#{k}: #{v}"}.join('; ')
253
-
254
- html += sprintf '<tr style="%s">', row_style
255
- html += '<td style="text-align: left">' + e[:date].strftime('%m/%d/%Y') + '</td>'
256
- html += '<td style="text-align: left">' + e[:destination] + '</td>'
257
- html += '<td style="text-align: right">' + delta + '</td>'
258
- html += '<td style="text-align: right">' + total + '</td>' if use_total
259
- html += '</tr>'
260
- end
261
-
262
- html += '</table>'
263
- end
264
-
265
- html
266
- end
267
-
268
138
  def parse_balance(body)
269
139
  Hpricot.buffer_size = 262144
270
140
  doc = Hpricot.parse(body)
@@ -333,7 +203,3 @@ private
333
203
  end
334
204
  end
335
205
 
336
- if $0 == __FILE__
337
- cb = CommerceBank.new
338
- cb.gmail_daily_summary
339
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alexmchale-commerce-bank-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex McHale
@@ -27,7 +27,6 @@ files:
27
27
  - lib/commercebank.rb
28
28
  - lib/commercebank
29
29
  - lib/commercebank/monkey.rb
30
- - lib/commercebank/appconfig.rb
31
30
  - test/monkeypatch_test.rb
32
31
  - test/test_helper.rb
33
32
  - test/commerce_bank_client_test.rb
@@ -1,28 +0,0 @@
1
- require 'rubygems'
2
- require 'pp'
3
- require 'andand'
4
- require 'yaml'
5
-
6
- class AppConfig
7
- def initialize(path)
8
- @path = path
9
- end
10
-
11
- def [](field)
12
- field = field.to_s
13
- path = File.expand_path(@path)
14
- config = File.exists?(path) ? YAML.load(File.read path) : Hash.new
15
-
16
- unless config[field]
17
- print "Please enter the following:\n"
18
- print field, ": "
19
-
20
- config[field] = gets.to_s.chomp
21
-
22
- File.open(path, 'w') {|file| file.write(config.to_yaml)}
23
- File.chmod(0600, path)
24
- end
25
-
26
- config[field]
27
- end
28
- end