Skip to content

Instantly share code, notes, and snippets.

@whatupdave
Created June 5, 2013 19:50
Show Gist options
  • Save whatupdave/5716749 to your computer and use it in GitHub Desktop.
Save whatupdave/5716749 to your computer and use it in GitHub Desktop.
Stripe month to date report
require "stripe"
require "time"
Stripe.api_key = ENV['STRIPE_KEY']
start_of_month = Time.parse(Time.now.strftime('1-%b-%Y 00:00:00 UTC'))
def created_after(object, time)
offset = 0
objects = object.all(count:100, offset: offset, created: { gte: time.to_i }).to_a
while objects.size == 100
offset += 100
objects += object.all(count:100, offset: offset, created: { gte: time.to_i }).to_a
end
objects.sort_by{|o| o.created }
end
customers = created_after(Stripe::Customer, start_of_month)
week_customers = customers.group_by{|c| Time.at(c.created).utc.day / 7 }
puts "#{customers.size} new Customers"
week_customers.each do |week, customers|
puts " Week #{week + 1}: #{customers.size}"
end
charges = created_after(Stripe::Charge, start_of_month)
sum = charges.inject(0) {|sum, c| sum + c.amount }
week_charges = charges.group_by{|c| Time.at(c.created).utc.day / 7 }
puts "\n#{charges.size} charges ($#{"%.02f" % (sum / 100)})"
week_charges.each do |week, charges|
sum = charges.inject(0) {|sum, c| sum + c.amount }
puts " Week #{week + 1}: $#{"%.02f" % (sum / 100)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment