Skip to content

Instantly share code, notes, and snippets.

@hayesdavis
Created March 10, 2010 22:18
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 hayesdavis/328513 to your computer and use it in GitHub Desktop.
Save hayesdavis/328513 to your computer and use it in GitHub Desktop.
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