Skip to content

Instantly share code, notes, and snippets.

@scotchi
Created November 7, 2013 05:52
Show Gist options
  • Save scotchi/7349679 to your computer and use it in GitHub Desktop.
Save scotchi/7349679 to your computer and use it in GitHub Desktop.
ShopifyAPI::Base.find_each
module ShopifyAPI
class Base
RETRY_AFTER = 60
def self.find_each(params = {}, &block)
params[:limit] ||= 50
params[:page] = 1
retried = false
begin
until find(:all, :params => params).each { |value| block.call(value) }.empty?
params[:page] += 1
retried = false
end
rescue ActiveResource::ConnectionError, ActiveResource::ServerError => 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment