You can create a class that represents the api connection and use
method_missing
to collect all the API calls to one place in your code, like this:

class ApiWrapperClass
  def method_missing(meth, *args)
    retries = 0
    begin
      SomeAPI.send(meth, *args)
    rescue API::Exception
      ...
    end
end

x = ApiWrapperClass.new
x.api_call( .... )



On Thu, Jan 24, 2013 at 6:18 PM, tamouse mailing lists <
tamouse.lists / gmail.com> wrote:

> I have some API calls I'm making, all with the following sort of
> begin-rescue-end wrapper:
>
> (pseudo-code)
>
> retries = 0
> begin
>   response = SomeAPI.api_method1(:request => { 'ThisId' => this_id })
> rescue API::Exception => e
>   retries.succ
>   sleep( retries * 5 )
>   retry unless retries > 2
> end
>
> where there are multiple api_methods and request forms. Seems like I
> should be able to DRY this instead of doing this everywhere I have to
> make this sort of API call.
>
>