Skip to content

Instantly share code, notes, and snippets.

@darul75
Last active April 14, 2021 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darul75/f19f42176448659d20e96fad189fc972 to your computer and use it in GitHub Desktop.
Save darul75/f19f42176448659d20e96fad189fc972 to your computer and use it in GitHub Desktop.
str#1
#!/usr/bin/ruby
require 'stripe'
Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
# Maximum allowed
PAGE_SIZE = 100
# Displaying charge info
def display_charges_info(data)
data.each do |child|
puts "#{Time.at(child['created'])} - Amount: #{child['amount']} - Id: #{child['id']}"
end
end
# Displaying pagination info
def display_page_info(pageIndex)
puts "------------------------------------
Page #{pageIndex} to #{pageIndex+PAGE_SIZE-1}
------------------------------------"
end
# Easiest way
def auto_paginate_over_all_charges()
# can be customised with more advanced parameters
# https://stripe.com/docs/api/charges/list?lang=ruby#list_charges-created
charges = Stripe::Charge.list({limit: PAGE_SIZE})
# automatic pagination
charges.auto_paging_each do |charge|
puts "#{Time.at(charge['created'])} - Amount: #{charge['amount']} - Id: #{charge['id']}"
end
end
# Advanced
def paginate_over_all_charges()
pageStart = 1
display_page_info(pageStart)
# initial range
charges = Stripe::Charge.list({limit: PAGE_SIZE})
# keep reference of last charge made withing this page
lastId = charges['data'][-1]['id']
display_charges_info(charges['data'])
# start looping over pages until has_more is false
while charges.has_more do
pageStart += PAGE_SIZE
display_page_info(pageStart)
# starting_after: cursor/offset for paginating
charges = Stripe::Charge.list({limit: PAGE_SIZE, starting_after: lastId})
# set new cursor value based on last page item
lastId = charges['data'][-1]['id']
display_charges_info(charges['data'])
end
end
# you can use one or the other option at your convenience, if I was you I would go
# with the easiest one ;)
# auto_paginate_over_all_charges()
paginate_over_all_charges()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment