Class: Learn::InternetConnection
- Inherits:
-
Object
- Object
- Learn::InternetConnection
- 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
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#silent ⇒ Object
readonly
Returns the value of attribute silent.
Class Method Summary collapse
Instance Method Summary collapse
- #connection? ⇒ Boolean
-
#initialize(silent: false) ⇒ InternetConnection
constructor
A new instance of InternetConnection.
- #no_connection? ⇒ Boolean
- #test_connection(retries: 3) ⇒ Object
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
#connection ⇒ Object
Returns the value of attribute connection.
7 8 9 |
# File 'lib/learn/internet_connection.rb', line 7 def connection @connection end |
#silent ⇒ Object (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
18 19 20 |
# File 'lib/learn/internet_connection.rb', line 18 def self.internet_connection? new(silent: true).connection? end |
.no_internet_connection? ⇒ Boolean
14 15 16 |
# File 'lib/learn/internet_connection.rb', line 14 def self.no_internet_connection? new.no_connection? end |
.test_connection ⇒ Object
22 23 24 |
# File 'lib/learn/internet_connection.rb', line 22 def self.test_connection new end |
Instance Method Details
#connection? ⇒ Boolean
73 74 75 |
# File 'lib/learn/internet_connection.rb', line 73 def connection? connection end |
#no_connection? ⇒ 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..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 |