Created
March 10, 2010 22:18
-
-
Save hayesdavis/328513 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
client = Grackle::Client.new(:api=>:v1) | |
term = 'foo' | |
page = with_retry{client.search?(:q=>term,:rpp=>100)} | |
has_results = !page.results.empty? | |
while(has_results) | |
# page.results has tweets in it | |
unless page.next_page.nil? | |
page = with_retry{client.search?(:q=>term, :rpp=>100, :page=>page.page+1, :max_id=>page.max_id)} | |
has_results = !page.results.empty? | |
else | |
has_results = false | |
end | |
end | |
#A helpful method for handling Twitter errors | |
def with_retry(default=nil,attempts=3) | |
yield if attempts > 0 | |
rescue => e | |
logger.info "Received error #{e}" | |
if should_retry?(e) | |
logger.info "Will attempt #{attempts-1} more time(s)." | |
attempts -= 1 | |
retry | |
else | |
raise e | |
end | |
end | |
def should_retry?(err) | |
if err.kind_of?(Grackle::TwitterError) | |
#Satus of nil means it was an unexpected error generally | |
#500 errors are probably transient | |
err.status.nil? || err.status.to_i >= 500 | |
else | |
false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment