Sha256: c8b9ccb5b237b2c162e3b3714c8a7c98823a59d5052f58b8e3bd7edee195e005

Contents?: true

Size: 703 Bytes

Versions: 1

Compression:

Stored size: 703 Bytes

Contents

require "net/http"
require "uri"

module Bard
  class Ping < Struct.new(:server)
    def self.call server
      new(server).call
    end

    def call
      return true if server.ping == false
      response = get_response_with_redirect(server.ping)
      response.is_a?(Net::HTTPSuccess)
    end

    private

    def get_response_with_redirect uri_str, limit=5
      response = Net::HTTP.get_response(URI(uri_str))

      case response
      when Net::HTTPRedirection
        if limit == 0
          puts "too many HTTP redirects"
          response
        else
          get_response_with_redirect(response["location"], limit - 1)
        end
      else
        response
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bard-0.64.1 lib/bard/ping.rb