From 0cf038baffa455c03c09d578682d0f2006f2ea5a Mon Sep 17 00:00:00 2001 From: SHIBATA Hiroshi Date: Wed, 7 Mar 2012 09:08:45 +0900 Subject: [PATCH] init --- .gitignore | 1 + README.md | 12 +++++++++ init.rb | 2 ++ lib/invoice/heroku/client.rb | 5 ++++ lib/invoice/heroku/command/invoice.rb | 39 +++++++++++++++++++++++++++ 5 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 init.rb create mode 100644 lib/invoice/heroku/client.rb create mode 100644 lib/invoice/heroku/command/invoice.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8f6101 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.rvmrc \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..41fa455 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# heroku-invoice + +publish invoice on your heroku account. + +## Installation + + $ heroku plugins:install https://github.com/hsbt/heroku-invoice.git + +## Usage + + $ heroku invoice --app myapp + $ heroku invoice:pdf --app myapp diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..0a5d160 --- /dev/null +++ b/init.rb @@ -0,0 +1,2 @@ +require "invoice/heroku/client" +require "invoice/heroku/command/invoice" diff --git a/lib/invoice/heroku/client.rb b/lib/invoice/heroku/client.rb new file mode 100644 index 0000000..60e90d1 --- /dev/null +++ b/lib/invoice/heroku/client.rb @@ -0,0 +1,5 @@ +class Heroku::Client + def get_invoice(y, m) + get("invoices/show/#{y}/#{m}").to_s + end +end diff --git a/lib/invoice/heroku/command/invoice.rb b/lib/invoice/heroku/command/invoice.rb new file mode 100644 index 0000000..c9a22db --- /dev/null +++ b/lib/invoice/heroku/command/invoice.rb @@ -0,0 +1,39 @@ +class Heroku::Command::Invoice < Heroku::Command::Base + def index + write_body(filename) + end + + def pdf + require 'tempfile' + + t = Tempfile.new + write_body(t.path) + system "wkhtmltopdf #{t.path} #{filename.sub(/\.html\z/, '.pdf')}" + t.flush + end + +private + + def write_body(name) + Zlib::GzipReader.wrap(StringIO.new(get_body)) do |gz| + File.open(name, 'w'){|f| f.print gz.read} + end + end + + def get_body + heroku.get_invoice(optparse).net_http_res.body + end + + def filename + "%04d%02d.html" % optparse + end + + def optparse + if args[0] + args[0].scan(/(\d{4})(\d{2})/).first.map(&:to_i) + else + t = Time.now + [t.year, t.month] + end + end +end