Created
January 25, 2010 14:03
-
-
Save lukeredpath/285878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require File.join(File.dirname(__FILE__), *%w[freeagent freeagent]) | |
| require 'ostruct' | |
| require 'erb' | |
| require 'tempfile' | |
| MY_USER_ID = 2383 | |
| freeagent = FreeAgent::Company.new(ENV['FA_COMPANY'], ENV['FA_USERNAME'], ENV["FA_PASSWORD"]) | |
| class ExpensesReport | |
| def initialize(expenses, label) | |
| @expenses = expenses | |
| @label = label | |
| end | |
| def to_s | |
| output = @label.upcase + "\n" + separator + "\n" | |
| grouped_expenses.map do |category, expenses| | |
| output << "#{category.ljust(60)}#{(-expenses.sum(&:gross_value)).to_s.rjust(10)}\n" | |
| end | |
| output << separator + "\n" | |
| output | |
| end | |
| def to_html | |
| vars = OpenStruct.new(:expenses => @expenses, :label => @label) | |
| ERB.new(File.read(template_path)).result(vars.send(:binding)) | |
| end | |
| private | |
| def separator | |
| 70.times.inject('') { |buff, x| buff + '-' } | |
| end | |
| def grouped_expenses | |
| @grouped_expenses ||= @expenses.group_by(&:expense_type) | |
| end | |
| def template_path | |
| File.join(File.dirname(__FILE__), *%w[freeagent templates expense_report.erb.html]) | |
| end | |
| end | |
| report = case ARGV[0] | |
| when 'last' | |
| ExpensesReport.new(freeagent.expenses(MY_USER_ID, | |
| :from => Date.today.last_month.beginning_of_month, | |
| :to => Date.today.last_month.end_of_month | |
| ).find_all, "#{Date::MONTHNAMES[Date.today.last_month.month]} #{Date.today.last_month.year}") | |
| else | |
| ExpensesReport.new(freeagent.expenses(MY_USER_ID, | |
| :from => Date.today.beginning_of_month, | |
| :to => Date.today.end_of_month | |
| ).find_all, "#{Date::MONTHNAMES[Date.today.month]} #{Date.today.year}") | |
| end | |
| temp_path = "/tmp/expense_report_#{Time.now.to_i}.html" | |
| File.open(temp_path, 'w+') do |io| | |
| io.write(report.to_html) | |
| end | |
| system("open -a Safari #{temp_path}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment