Question

I have such class (some code was removed):

class Terms

  def get_connection(path, proxy_addr, proxy_port)
    @http = Net::HTTP.new('*', 443, proxy_addr, proxy_port)
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @http.read_timeout = 500
    header = {
      'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0',
      'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
      'Accept-Language' => 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',  
      'Connection' => 'keep-alive'
    }
    @resp_get, @data_get = @http.get(path, header)
    @get_body_text = @resp_get.body
    return @resp_get
  end
  def first(path, type_id)
    ***
    @resp_post2 = @http.post(path, data, headers)
    return @resp_post2
  end
end

and then i call it in main:

v = Terms.get_connection
v.first

but main trouble is that my website could be down, and i could get error, that @resp_post2 = @http.post(path, data, headers) couldn't be reached. And my question is how to loop this post request, and check, if something is bad, than try again to send this post request. Maybe to use try catch, maybe while, but how to write this code? how to check on page present, and on connection present?

Was it helpful?

Solution

As I think you can user begin rescue in you first method

def first(path, type_id)
  begin
   @resp_post2 = @http.post(path, data, headers)
   return @resp_post2
  rescue
    retry #it will retry it once more
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top