I think your http connection or remote connection is unstable. Whenever I have this situation, I use a timeout and try again several times. The following code will execute your code for 10 seconds, and if it is not completed, it will time out and try again. If the retry is not performed many times, it will ultimately fail.
def timeout_and_retry
retries = 0
begin
Timeout::timeout(10) { yield }
rescue Timeout::Error
raise if (self.retries += 1) > 3
retry
end
end
timeout_and_retry do
end