Retryable is another handy gem that lets you repeat a code block after an exception has happened. It has a really easy syntax, and comes with many customizations, that make it a great tool to use, especially when you have to work with web services.
You can install it by simply adding the following in your Gemfile:
gem
‘retryable’
You can use many parameters to configure it based on your needs, such the number of iterations:
require <span style="font-weight: 400;">"open-uri"</span>
<span style="font-weight: 400;">Retryable</span><span style="font-weight: 400;">.retryable(</span><span style="font-weight: 400;">:tries</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">2</span><span style="font-weight: 400;">) </span><span style="font-weight: 400;">do</span>
<span style="font-weight: 400;"> xml </span><span style="font-weight: 400;">=</span><span style="font-weight: 400;"> open(</span><span style="font-weight: 400;">"URL"</span><span style="font-weight: 400;">).read</span>
<span style="font-weight: 400;">end</span>
type of exceptions:
<span style="font-weight: 400;">require</span> <span style="font-weight: 400;">"open-uri"</span>
<span style="font-weight: 400;">Retryable</span><span style="font-weight: 400;">.retryable(</span><span style="font-weight: 400;">:on</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">OpenURI</span><span style="font-weight: 400;">::</span><span style="font-weight: 400;">HTTPError</span><span style="font-weight: 400;">) </span><span style="font-weight: 400;">do</span>
<span style="font-weight: 400;"> xml </span><span style="font-weight: 400;">=</span><span style="font-weight: 400;"> open(</span><span style="font-weight: 400;">"URL"</span><span style="font-weight: 400;">).read</span>
<span style="font-weight: 400;">end</span>
the pattern that should be matched:
<span style="font-weight: 400;">Retryable</span><span style="font-weight: 400;">.retryable(</span><span style="font-weight: 400;">:matching</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">/Invalid parameter/</span><span style="font-weight: 400;">) </span><span style="font-weight: 400;">do </span><span style="font-weight: 400;">|retries, exception|</span>
<span style="font-weight: 400;"> </span><span style="font-weight: 400;">raise</span> <span style="font-weight: 400;">"Invalid parameter submitted"</span> <span style="font-weight: 400;">if</span><span style="font-weight: 400;"> retries </span><span style="font-weight: 400;">==</span> <span style="font-weight: 400;">0</span>
<span style="font-weight: 400;">end</span>
You can also make the code block take a break using :sleep and the number of seconds that you want the break to last before the new execution of code block begins.
It can even be used to specify the type of call back that you want to execute when the exception is thrown:
span style="font-weight: 400;">exception_cb <span style="font-weight: 400;">=</span> <span style="font-weight: 400;">Proc</span><span style="font-weight: 400;">.</span><span style="font-weight: 400;">new</span> <span style="font-weight: 400;">do </span><span style="font-weight: 400;">|exception|</span>
<span style="font-weight: 400;"> </span><span style="font-weight: 400;"># http://smartinez87.github.io/exception_notification</span>
<span style="font-weight: 400;"> </span><span style="font-weight: 400;">ExceptionNotifier</span><span style="font-weight: 400;">.notify_exception(exception, </span><span style="font-weight: 400;">:data</span><span style="font-weight: 400;"> => {</span><span style="font-weight: 400;">:message</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">"An exception has been thrown"</span><span style="font-weight: 400;">})</span>
<span style="font-weight: 400;">end</span>
<span style="font-weight: 400;">Retryable</span><span style="font-weight: 400;">.retryable(</span><span style="font-weight: 400;">:exception_cb</span><span style="font-weight: 400;"> => exception_cb) </span><span style="font-weight: 400;">do</span>
<span style="font-weight: 400;">end</span>
You can also disable it in certain occasions when you need to do so:
span style="font-weight: 400;">Retryable<span style="font-weight: 400;">.enabled?</span>
<span style="font-weight: 400;">=> </span><span style="font-weight: 400;">true</span>
<span style="font-weight: 400;">Retryable</span><span style="font-weight: 400;">.disable</span>
<span style="font-weight: 400;">Retryable</span><span style="font-weight: 400;">.enabled?</span>
<span style="font-weight: 400;">=> </span><span style="font-weight: 400;">false</span>
There are other methods to repeat a code block in Ruby when a particular exception is thrown, for example begin, rescue, retry cycle. Retryable is just another cool one that joins the party, and makes it even more beautiful to code in Ruby. As it comes with so many parameters that can easily be specified, it makes it really worth giving it a try.
The power of choice is part of the autonomy, which ultimately contributes to motivation. Having the opportunity to choose between so many alternatives for the same thing is really inspiring.
You can learn more about this gem and also view its source code by going to this Github repository.