File: internet_connection.rb

package info (click to toggle)
ruby-vcr 6.0.0%2Breally5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: ruby: 8,456; sh: 177; makefile: 7
file content (37 lines) | stat: -rw-r--r-- 794 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module VCR
  # Ruby 1.8 provides Ping.pingecho, but it was removed in 1.9.
  # 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(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

  # @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