Sha256: f6e9699c3e8c23880b2f41150e472a5a576de5b9d80038b556b7840b60c2c640

Contents?: true

Size: 898 Bytes

Versions: 1

Compression:

Stored size: 898 Bytes

Contents

require 'net/http'

module ShuntCache
  module Checker

    class << self
      def wait_for_http(url, options = {})
        retries = options.fetch(:retry, 10)
        wait_time = options.fetch(:wait_time, 6)

        uri = URI(url)

        retries.times do
          begin
            response = request(uri, options)
            code = response.code.to_i
            return true if 200 <= code && 399 >= code
          rescue Errno::ECONNREFUSED, Timeout::Error => e
          end
          sleep(wait_time)
        end

        false
      end

      def request(uri, options = {})
        timeout = options.fetch(:timeout, 10)

        response = nil
        Net::HTTP.start(uri.host, uri.port) do |http|
          http.read_timeout = timeout
          request = Net::HTTP::Get.new(uri.path)
          response = http.request(request)
        end
        response
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shunt_cache-0.0.2 lib/shunt_cache/checker.rb