Class: Learn::InternetConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/learn/internet_connection.rb

Constant Summary collapse

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..."

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(silent: false) ⇒ InternetConnection

Returns a new instance of InternetConnection.



25
26
27
28
29
30
# File 'lib/learn/internet_connection.rb', line 25

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

  test_connection
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/learn/internet_connection.rb', line 6

def connection
  @connection
end

#silentObject (readonly)

Returns the value of attribute silent.



7
8
9
# File 'lib/learn/internet_connection.rb', line 7

def silent
  @silent
end

Class Method Details

.internet_connection?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/learn/internet_connection.rb', line 17

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

.no_internet_connection?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/learn/internet_connection.rb', line 13

def self.no_internet_connection?
  new.no_connection?
end

.test_connectionObject



21
22
23
# File 'lib/learn/internet_connection.rb', line 21

def self.test_connection
  new
end

Instance Method Details

#connection?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/learn/internet_connection.rb', line 72

def connection?
  connection
end

#no_connection?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/learn/internet_connection.rb', line 68

def no_connection?
  !connection
end

#test_connection(retries: 3) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/learn/internet_connection.rb', line 32

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