Sha256: 6460f6a611eb7f5a65c8c3c3d09a9aaa56b3fd748ed321781bbc7ef547c68421

Contents?: true

Size: 1.69 KB

Versions: 14

Compression:

Stored size: 1.69 KB

Contents

require 'net/http'
require 'timeout'

module Learn
  class InternetConnection
    attr_accessor :connection
    attr_reader   :silent

    STATUS_URI          = URI('https://learn.co/p/gem_status')
    SUCCESS_STATUS      = 'this is a boring message to prove you can connect to the internet'
    NO_INTERNET_MESSAGE = "It seems like you aren't connected to the internet. All features of the Learn gem may not work properly. Trying anyway..."

    def self.no_internet_connection?
      new.no_connection?
    end

    def self.internet_connection?
      new(silent: true).connection?
    end

    def self.test_connection
      new
    end

    def initialize(silent: false)
      @connection = false
      @silent     = silent

      test_connection
    end

    def test_connection
      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
        self.connection = false
        puts NO_INTERNET_MESSAGE if !silent
      rescue SocketError => e
        if e.message.match(/getaddrinfo: nodename nor servname provided/)
          self.connection = false
          puts NO_INTERNET_MESSAGE if !silent
        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

    def no_connection?
      !connection
    end

    def connection?
      connection
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
learn-co-3.2.2 lib/learn/internet_connection.rb
learn-co-3.2.1 lib/learn/internet_connection.rb
learn-co-3.2.0 lib/learn/internet_connection.rb
learn-co-3.1.2 lib/learn/internet_connection.rb
learn-co-3.1.1 lib/learn/internet_connection.rb
learn-co-3.1.0 lib/learn/internet_connection.rb
learn-co-3.0.0 lib/learn/internet_connection.rb
learn-co-2.5.3 lib/learn/internet_connection.rb
learn-co-2.5.2 lib/learn/internet_connection.rb
learn-co-2.5.1 lib/learn/internet_connection.rb
learn-co-2.5.0 lib/learn/internet_connection.rb
learn-co-2.4.2 lib/learn/internet_connection.rb
learn-co-2.4.1 lib/learn/internet_connection.rb
learn-co-2.4.0 lib/learn/internet_connection.rb