Created
November 7, 2013 05:52
-
-
Save scotchi/7349679 to your computer and use it in GitHub Desktop.
ShopifyAPI::Base.find_each
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
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