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.



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

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

  test_connection
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#silentObject (readonly)

Returns the value of attribute silent.



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

def silent
  @silent
end

Class Method Details

.internet_connection?Boolean

Returns:

  • (Boolean)


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

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

.no_internet_connection?Boolean

Returns:

  • (Boolean)


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

def self.no_internet_connection?
  new.no_connection?
end

.test_connectionObject



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

def self.test_connection
  new
end

Instance Method Details

#connection?Boolean

Returns:

  • (Boolean)


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

def connection?
  connection
end

#no_connection?Boolean

Returns:

  • (Boolean)


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

def no_connection?
  !connection
end

#test_connection(retries: 3) ⇒ Object



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