net/http and begin/end block

E

Ezra Zygmuntowicz

Hey list-
I have the following code that seems to work fine:

require 'net/http'

def fetch(page, section='display', limit=5)
data = ''
begin
data = Net::HTTP.get_response("192.168.0.2", "/#{page}")
rescue Exception => e
limit -= 1
limit > 0 ? retry : raise
end
data.body.gsub!(/\/temporaryimages/, "http://192.168.0.2/
temporaryimages")
data.body.gsub!(/\/wrappers\/(\d+)\.news/i, "/page/#{section}/\1")
end

I am using it to fetch some templated web pages from an in house
server that almost never errors out. So my code works fine. But I am
wondering if this code will do what I think it should do upon
failure. Before I added the begin/rescue/end block, the server would
occasionally error out with a connection reset by peer error. So my
question is will this syntax work to retry fetching the page 5 times
before it gives up. Will the rescue Exception => e part catch pretty
much any type of error?
Thanks in advance for anyone explaining this to me or offering a
more robust solution.


-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top