Created
October 29, 2013 23:11
-
-
Save woodrow/7224368 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
require 'stripe' | |
Stripe.api_key = 'API_KEY' | |
customers_by_coupon = {} | |
customer_count = nil | |
batch_count = 100 | |
batch_offset = 0 | |
# we can only load customer objects in batches of 100, so do that in this loop | |
while !customer_count || batch_offset < customer_count | |
puts "Loading customers #{batch_offset}..#{batch_offset + batch_count}" | |
customer_hash = Stripe::Customer.all(:count => batch_count, :offset => batch_offset) | |
customer_count = customer_hash['count'] | |
customer_batch = customer_hash['data'] | |
batch_offset += customer_batch.length | |
# filter out customers who have a coupon applied to them | |
customers_with_discounts = customer_batch.select{|c| c['discount']} | |
# store these customers in customers_by_coupon, keyed on coupon code | |
customers_with_discounts.each do |customer| | |
coupon_id = customer['discount']['coupon']['id'] | |
(customers_by_coupon[coupon_id] ||= []) << customer | |
end | |
end | |
puts "Done loading customers!" | |
if customers_by_coupon.length > 0 | |
# print out a CSV list of "coupon_id, customer_id" pairs | |
puts "\nCOUPON_ID, CUSTOMER_ID" | |
customers_by_coupon.each do |(coupon_id,customers)| | |
customers.each do |c| | |
puts "#{coupon_id}, #{c.id}" | |
end | |
end | |
else | |
puts "\nNo customers have discounts." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment