From: Nokan Emiro Date: 2013-01-25T02:33:08+09:00 Subject: Re: How to DRY this up? --e89a8fb1fd7e2cbea404d40c34f8 Content-Type: text/plain; charset=UTF-8 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. > > --e89a8fb1fd7e2cbea404d40c34f8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

You can create a class that represents the api connecti= on and use method_missing
to collect all the API calls to one pla= ce in your code, like this:

class ApiWrapperClass
=C2=A0 def method_missing(meth, *args)
=C2=A0 =C2=A0 retries =3D = 0
=C2=A0 =C2=A0 begin
=C2=A0 =C2=A0 =C2=A0 SomeAPI.send= (meth, *args)
=C2=A0 =C2=A0 rescue API::Exception
=C2= =A0 =C2=A0 =C2=A0 ...
=C2=A0 =C2=A0 end
end
<= br>
x =3D 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 wi= th the following sort of
begin-rescue-end wrapper:

(pseudo-code)

retries =3D 0
begin
=C2=A0 response =3D SomeAPI.api_method1(:request =3D> { 'ThisId'= =3D> this_id })
rescue API::Exception =3D> e
=C2=A0 retries.succ
=C2=A0 sleep( retries * 5 )
=C2=A0 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.


--e89a8fb1fd7e2cbea404d40c34f8--