On 7/8/05, Ezra Zygmuntowicz
<ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org>
wrote:> Hey there list-
> i am trying to get my head around some exception handling. Could
> some kind soul please point me in the right direction?
>
> Heres the code in my model:
> def self.fetch (page)
> Net::HTTP.start("192.168.0.2") do |http|
> data = http.get("/#{page}")
> data.body.gsub!(/\/temporaryimages/, "192.168.0.2
> temporaryimages")
> data.body.gsub!(/\/wrappers\/(\d+)\.news/i,
''/page/display/\1'')
> end
> end
>
> What would be the proper way to wrap this in a
> begin..rescue...retry....end block so if the connection is reset or
> some othere http error occurs, the code will retry the fetch until it
> succeeds?
Ezra-
There is a pretty good example of this in the Net::HTTP documentation,
which can be viewed at:
ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html
Basically you probably want to just do a recursive call to some method
on the model. Something like this (borrowed straight from the docs)
should help you out.
def fetch( uri_str, limit = 10 )
# You should choose better exception.
raise ArgumentError, ''HTTP redirect too deep'' if limit
== 0
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then
fetch(response[''location''], limit - 1)
else
response.error!
end
end
Cheers,
Ben