class Learn::InternetConnection

Constants

NO_INTERNET_MESSAGE
STATUS_URI
SUCCESS_STATUS

Attributes

connection[RW]
silent[R]

Public Class Methods

internet_connection?() click to toggle source
# File lib/learn/internet_connection.rb, line 18
def self.internet_connection?
  new(silent: true).connection?
end
new(silent: false) click to toggle source
# File lib/learn/internet_connection.rb, line 26
def initialize(silent: false)
  @connection = false
  @silent     = silent

  test_connection
end
no_internet_connection?() click to toggle source
# File lib/learn/internet_connection.rb, line 14
def self.no_internet_connection?
  new.no_connection?
end
test_connection() click to toggle source
# File lib/learn/internet_connection.rb, line 22
def self.test_connection
  new
end

Public Instance Methods

connection?() click to toggle source
# File lib/learn/internet_connection.rb, line 73
def connection?
  connection
end
no_connection?() click to toggle source
# File lib/learn/internet_connection.rb, line 69
def no_connection?
  !connection
end
test_connection(retries: 3) click to toggle source
# File lib/learn/internet_connection.rb, line 33
def test_connection(retries: 3)
  begin
    Timeout::timeout(5) do
      resp = Net::HTTP.get(STATUS_URI)

      if resp.match(/#{SUCCESS_STATUS}/)
        self.connection = true
      else
        self.connection = false
        puts NO_INTERNET_MESSAGE if !silent
      end
    end
  rescue Timeout::Error
    if retries > 0
      test_connection(retries: retries - 1)
    else
      self.connection = false
      puts NO_INTERNET_MESSAGE if !silent
    end
  rescue SocketError => e
    if e.message.match(/getaddrinfo: nodename nor servname provided/)
      if retries > 0
        test_connection(retries: retries - 1)
      else
        self.connection = false
        puts NO_INTERNET_MESSAGE if !silent
      end
    end
  rescue OpenSSL::SSL::SSLError
    self.connection = false
    puts "It looks like your SSL certificates aren't quite right."
    puts "Please run `rvm osx-ssl-certs update all` and then try again."
    exit
  end
end