lib/commercebank.rb in alexmchale-commerce-bank-client-0.6.2 vs lib/commercebank.rb in alexmchale-commerce-bank-client-0.8.0
- old
+ new
@@ -9,47 +9,13 @@
require 'time'
require 'date'
require 'json'
require 'htmlentities'
require 'gmail'
+require 'appconfig'
require 'commercebank/monkey.rb'
-require 'commercebank/appconfig.rb'
-class Array
- def binary
- map {|e| yield(e) ? [e, nil] : [nil, e]}.transpose.map {|a| a.compact}
- end
-end
-
-class Object
- def to_cents
- (to_s.gsub(/[^-.0-9]/, '').to_f * 100).to_i
- end
-end
-
-class Date
- def days_in_month
- (Date.parse("12/31/#{strftime("%Y")}") << (12 - month)).day
- end
-
- def last_sunday
- d = self
- d -= 1 until d.wday == 0
- d
- end
-end
-
-class Hash
- def to_url
- map {|key, value| "#{CGI.escape key.to_s}=#{CGI.escape value.to_s}"}.join "&"
- end
-
- def to_cookie
- map {|key, value| "#{key}=#{value}"}.join('; ')
- end
-end
-
class WebClient
attr_reader :fields, :cookies
def initialize
@cookies = Hash.new
@@ -149,124 +115,28 @@
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
- { 'Pending' => @pending,
- 'Today' => today,
- 'Yesterday' => yesterday,
- 'This Week' => this_week,
- 'Last Week' => last_week,
- :order => [ 'Pending', 'Today', 'Yesterday', 'This Week', 'Last Week' ] }
+ yield 'Pending', @pending
+ yield 'Today', today
+ yield 'Yesterday', yesterday
+ yield 'This Week', this_week
+ yield 'Last Week', last_week
end
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}
- { day_in_month.strftime('%B') => entries }
- end
- def print_all
- summarize 'All' => register
+ yield day_in_month.strftime('%B'), entries
end
- def print_daily_summary
- print(summarize(daily_summary))
- end
-
- def text_daily_summary
- summarize daily_summary
- end
-
- def html_daily_summary
- summarize_html daily_summary
- end
-
- def gmail_daily_summary
- subject = "Daily Summary"
-
- username = @config['GMail Username']
- password = @config['GMail Password']
-
- GMail.new(username, password).send(username, subject, html_daily_summary, 'text/html')
- end
-
- def gmail_monthly_summary
- last_month = Date.today - Date.today.day
- subject = "#{last_month.strftime('%B')} Summary"
- summary = summarize_html(monthly_summary(last_month))
-
- username = @config['GMail Username']
- password = @config['GMail Password']
-
- GMail.new(username, password).send(username, subject, summary, 'text/html')
- end
-
private
- def summarize(entries)
- (entries[:order] || entries.keys).map do |label|
- next if entries[label].length == 0
-
- label.to_s + ":\n" + entries[label].map do |e|
- [
- e[:date].strftime('%02m/%02d/%04Y '),
- "%-100s " % e[:destination],
- "%10s " % e[:delta].to_dollars(:show_plus),
- e[:total] && ("%10s " % e[:total].to_dollars),
- "\n"
- ].compact.join
- end.join
- end.compact.join("\n")
- end
-
- def summarize_html(entries)
- html = ''
-
- (entries[:order] || entries.keys).each do |label|
- next if entries[label].length == 0
-
- use_total = entries[label].find {|e| e[:total]}
-
- html += '<h2 style="font-family: garamond, georgia, serif">' + label + '</h2>'
-
- html += '<table cellspacing="0" cellpadding="5" style="font-size: 12px; border-style: solid; border-width: 2px; border-color: #DDDDDD" width="100%">'
-
- html += '<tr style="font-weight: bold; background-color: #DDDDDD">'
- html += '<th style="text-align: left" width="75">Date</th>'
- html += '<th style="text-align: left">Destination</th>'
- html += '<th style="text-align: right" width="75">Amount</th>'
- html += '<th style="text-align: right" width="75">Total</th>' if use_total
- html += '</tr>'
-
- even = true
- entries[label].each do |e|
- even = !even
-
- delta = "%s%0.2f" % [ (e[:delta] >= 0 ? '+' : '-'), e[:delta].abs/100.0 ]
- total = "%0.2f" % (e[:total].to_i/100.0)
-
- row_style = {
- 'font-weight' => 'normal',
- 'background-color' => even ? '#DDDDDD' : '#FFFFFF'
- }.map {|k, v| "#{k}: #{v}"}.join('; ')
-
- html += sprintf '<tr style="%s">', row_style
- html += '<td style="text-align: left">' + e[:date].strftime('%m/%d/%Y') + '</td>'
- html += '<td style="text-align: left">' + e[:destination] + '</td>'
- html += '<td style="text-align: right">' + delta + '</td>'
- html += '<td style="text-align: right">' + total + '</td>' if use_total
- html += '</tr>'
- end
-
- html += '</table>'
- end
-
- html
- end
-
def parse_balance(body)
Hpricot.buffer_size = 262144
doc = Hpricot.parse(body)
summaryRows = doc/"table.summaryTable"/"tr"
current = (summaryRows[3]/"td")[1].inner_html.to_cents
@@ -331,9 +201,5 @@
}
end.compact
end
end
-if $0 == __FILE__
- cb = CommerceBank.new
- cb.gmail_daily_summary
-end