Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Mar 7, 2012
0 parents commit 0cf038b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.rvmrc
12 changes: 12 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions init.rb
@@ -0,0 +1,2 @@
require "invoice/heroku/client"
require "invoice/heroku/command/invoice"
5 changes: 5 additions & 0 deletions 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
39 changes: 39 additions & 0 deletions 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

0 comments on commit 0cf038b

Please sign in to comment.