Skip to content

Instantly share code, notes, and snippets.

@raecoo
Forked from ianneub/example.rb
Last active August 29, 2015 14:15
Show Gist options
  • Save raecoo/eda3b559fce6010c2769 to your computer and use it in GitHub Desktop.
Save raecoo/eda3b559fce6010c2769 to your computer and use it in GitHub Desktop.
# Big thank you to Scott Wheeler and BBG in the Shopify API Forums
# http://ecommerce.shopify.com/c/shopify-apis-and-technology/t/paginate-api-results-113066
module ShopifyAPI
class Base
RETRY_AFTER = 60
def self.find_all(params = {}, &block)
params[:limit] ||= 50
params[:page] = 1
retried = false
begin
until find(:all, :params => params).each { |value| block.call(value) }.length < params[:limit]
params[:page] += 1
retried = false
end
rescue ActiveResource::ConnectionError, ActiveResource::ServerError,
ActiveResource::ClientError => ex
unless retried
sleep(((ex.respond_to?(:response) && ex.response['Retry-After']) || RETRY_AFTER).to_i)
retried = true
retry
else
raise ex
end
end
end
end
end
ShopifyAPI::Customer.find_all do |customer|
# do something with the customer
end
ShopifyAPI::Order.find_all(:status => :any) do |order|
# do something with the order
end
ShopifyAPI::Product.find_all(:limit => 250) do |order|
# do something with the product
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment