Last active
August 29, 2015 14:04
-
-
Save krames/7dd4c390178adbc9840a 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
def request(verb, endpoint, opts={}) | |
retry_on_auth_failure do | |
cached_token.request(verb, endpoint, opts) | |
end | |
end | |
def retry_on_auth_failure(retries=1, timeout=0, &block) | |
attempts_remaining = retries | |
begin | |
yield | |
rescue OAuth2::Error => error | |
if error.response.status == 401 && attempts_remaining > 0 | |
attempts_remaining -= 1 | |
cache_new_token | |
sleep(timeout) | |
retry | |
else | |
raise error | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about just this:
Reference: http://blog.mirthlab.com/2012/05/25/cleanly-retrying-blocks-of-code-after-an-exception-in-ruby/