Sha256: 6115134581e7ec34b3f71dfd49a8c457d47dbacf1b61fccd0def0957b87f14b8

Contents?: true

Size: 964 Bytes

Versions: 26

Compression:

Stored size: 964 Bytes

Contents

module VCR
  # Ruby 1.8 provides Ping.pingecho, but it was removed in 1.9.
  # So we try requiring it, and if that fails, define it ourselves.
  begin
    require 'ping'
    Ping = ::Ping
  rescue LoadError
    # This is copied, verbatim, from Ruby 1.8.7's ping.rb.
    require 'timeout'
    require "socket"

    # @private
    module Ping
      def pingecho(host, timeout=5, service="echo")
        begin
          timeout(timeout) do
            s = TCPSocket.new(host, service)
            s.close
          end
        rescue Errno::ECONNREFUSED
          return true
        rescue Timeout::Error, StandardError
          return false
        end
        return true
      end
      module_function :pingecho
    end
  end

  # @private
  module InternetConnection
    extend self

    EXAMPLE_HOST = "example.com"

    def available?
      @available = VCR::Ping.pingecho(EXAMPLE_HOST, 1, 80) unless defined?(@available)
      @available
    end
  end
end

Version data entries

26 entries across 26 versions & 4 rubygems

Version Path
vcr-2.2.0 lib/vcr/util/internet_connection.rb
vcr-2.1.1 lib/vcr/util/internet_connection.rb
vcr-2.1.0 lib/vcr/util/internet_connection.rb
vcr-2.0.1 lib/vcr/util/internet_connection.rb
vcr-2.0.0 lib/vcr/util/internet_connection.rb
vcr-2.0.0.rc2 lib/vcr/util/internet_connection.rb