online 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +20 -20
- data/README.md +9 -0
- data/Rakefile +1 -0
- data/lib/online.rb +16 -5
- data/online.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e39ddd7a39de7638e54b088e1f350444bf0922860082e55d0705524b1df08dec
|
|
4
|
+
data.tar.gz: 88cdb650e021f7f195c7394c23b84166a54345d322c75ee603ab51e4033d88fd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7103a153d77be803ba028d2c5321cbb7a4ea76df9d77fe0a5d0922ef2bd8ab9f7ca27599a1466ed787f8f6e2e528589321c625aa8879a3c6b38cb8af418030fd
|
|
7
|
+
data.tar.gz: 0d52eed0528be475ae2346833af45d7df78fc54d2160467e80b614c45e5313ac1509042f47ae7eb9961006be26dc8d4a2e8bf04b1730a0ec2351fa88d34bd56a
|
data/Gemfile.lock
CHANGED
|
@@ -11,34 +11,34 @@ GEM
|
|
|
11
11
|
public_suffix (>= 2.0.2, < 7.0)
|
|
12
12
|
ansi (1.5.0)
|
|
13
13
|
ast (2.4.3)
|
|
14
|
-
bigdecimal (3.
|
|
14
|
+
bigdecimal (3.3.1)
|
|
15
15
|
builder (3.3.0)
|
|
16
|
-
crack (1.0.
|
|
16
|
+
crack (1.0.1)
|
|
17
17
|
bigdecimal
|
|
18
18
|
rexml
|
|
19
19
|
docile (1.4.1)
|
|
20
|
-
hashdiff (1.2.
|
|
21
|
-
json (2.
|
|
20
|
+
hashdiff (1.2.1)
|
|
21
|
+
json (2.15.2)
|
|
22
22
|
language_server-protocol (3.17.0.5)
|
|
23
23
|
lint_roller (1.1.0)
|
|
24
|
-
minitest (5.
|
|
24
|
+
minitest (5.26.2)
|
|
25
25
|
minitest-reporters (1.7.1)
|
|
26
26
|
ansi
|
|
27
27
|
builder
|
|
28
28
|
minitest (>= 5.0)
|
|
29
29
|
ruby-progressbar
|
|
30
30
|
parallel (1.27.0)
|
|
31
|
-
parser (3.3.
|
|
31
|
+
parser (3.3.10.0)
|
|
32
32
|
ast (~> 2.4.1)
|
|
33
33
|
racc
|
|
34
|
-
prism (1.
|
|
34
|
+
prism (1.5.2)
|
|
35
35
|
public_suffix (6.0.2)
|
|
36
36
|
racc (1.8.1)
|
|
37
37
|
rainbow (3.1.1)
|
|
38
|
-
rake (13.3.
|
|
39
|
-
regexp_parser (2.11.
|
|
40
|
-
rexml (3.4.
|
|
41
|
-
rubocop (1.
|
|
38
|
+
rake (13.3.1)
|
|
39
|
+
regexp_parser (2.11.3)
|
|
40
|
+
rexml (3.4.4)
|
|
41
|
+
rubocop (1.81.7)
|
|
42
42
|
json (~> 2.3)
|
|
43
43
|
language_server-protocol (~> 3.17.0.2)
|
|
44
44
|
lint_roller (~> 1.1.0)
|
|
@@ -46,20 +46,20 @@ GEM
|
|
|
46
46
|
parser (>= 3.3.0.2)
|
|
47
47
|
rainbow (>= 2.2.2, < 4.0)
|
|
48
48
|
regexp_parser (>= 2.9.3, < 3.0)
|
|
49
|
-
rubocop-ast (>= 1.
|
|
49
|
+
rubocop-ast (>= 1.47.1, < 2.0)
|
|
50
50
|
ruby-progressbar (~> 1.7)
|
|
51
51
|
unicode-display_width (>= 2.4.0, < 4.0)
|
|
52
|
-
rubocop-ast (1.
|
|
52
|
+
rubocop-ast (1.47.1)
|
|
53
53
|
parser (>= 3.3.7.2)
|
|
54
54
|
prism (~> 1.4)
|
|
55
55
|
rubocop-minitest (0.38.2)
|
|
56
56
|
lint_roller (~> 1.1)
|
|
57
57
|
rubocop (>= 1.75.0, < 2.0)
|
|
58
58
|
rubocop-ast (>= 1.38.0, < 2.0)
|
|
59
|
-
rubocop-performance (1.26.
|
|
59
|
+
rubocop-performance (1.26.1)
|
|
60
60
|
lint_roller (~> 1.1)
|
|
61
61
|
rubocop (>= 1.75.0, < 2.0)
|
|
62
|
-
rubocop-ast (>= 1.
|
|
62
|
+
rubocop-ast (>= 1.47.1, < 2.0)
|
|
63
63
|
rubocop-rake (0.7.1)
|
|
64
64
|
lint_roller (~> 1.1)
|
|
65
65
|
rubocop (>= 1.72.1)
|
|
@@ -74,14 +74,14 @@ GEM
|
|
|
74
74
|
simplecov-html (0.13.2)
|
|
75
75
|
simplecov_json_formatter (0.1.4)
|
|
76
76
|
timeout (0.4.3)
|
|
77
|
-
unicode-display_width (3.
|
|
78
|
-
unicode-emoji (~> 4.
|
|
79
|
-
unicode-emoji (4.0
|
|
80
|
-
webmock (3.
|
|
77
|
+
unicode-display_width (3.2.0)
|
|
78
|
+
unicode-emoji (~> 4.1)
|
|
79
|
+
unicode-emoji (4.1.0)
|
|
80
|
+
webmock (3.26.1)
|
|
81
81
|
addressable (>= 2.8.0)
|
|
82
82
|
crack (>= 0.3.2)
|
|
83
83
|
hashdiff (>= 0.4.0, < 2.0.0)
|
|
84
|
-
yard (0.9.
|
|
84
|
+
yard (0.9.38)
|
|
85
85
|
|
|
86
86
|
PLATFORMS
|
|
87
87
|
aarch64-linux
|
data/README.md
CHANGED
|
@@ -44,6 +44,15 @@ if online?(ttl: 3)
|
|
|
44
44
|
end
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
+
You can specify HTTP timeout (in seconds) to prevent hanging on slow connections,
|
|
48
|
+
for example:
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
if online?(timeout: 2)
|
|
52
|
+
# We are online, if the timeout has not expired
|
|
53
|
+
end
|
|
54
|
+
```
|
|
55
|
+
|
|
47
56
|
That's it.
|
|
48
57
|
|
|
49
58
|
## How to contribute
|
data/Rakefile
CHANGED
data/lib/online.rb
CHANGED
|
@@ -30,8 +30,9 @@ module Kernel
|
|
|
30
30
|
# for checking internet availability before performing network-dependent operations
|
|
31
31
|
# or for implementing offline-mode functionality in applications.
|
|
32
32
|
#
|
|
33
|
-
# @param uri [String] the URI to check connectivity against (default: '
|
|
33
|
+
# @param uri [String] the URI to check connectivity against (default: 'https://www.google.com/generate_204')
|
|
34
34
|
# @param ttl [Integer] time-to-live for cached results in seconds (default: 300 seconds = 5 minutes)
|
|
35
|
+
# @param timeout [Integer] open, read and write HTTP timeout (default: 10 seconds)
|
|
35
36
|
# @return [Boolean] true if the URI is reachable and returns HTTP success, false otherwise
|
|
36
37
|
#
|
|
37
38
|
# @example Basic usage - check internet connectivity
|
|
@@ -85,18 +86,28 @@ module Kernel
|
|
|
85
86
|
# @note Returns false for any network-related errors including timeouts, DNS failures,
|
|
86
87
|
# and unreachable hosts
|
|
87
88
|
# @note Results are cached with a default TTL of 5 minutes to reduce network requests
|
|
88
|
-
def online?(uri: '
|
|
89
|
+
def online?(uri: 'https://www.google.com/generate_204', ttl: 300, timeout: 10)
|
|
89
90
|
raise 'The URI is nil' if uri.nil?
|
|
90
91
|
raise 'The TTL is nil' if ttl.nil?
|
|
91
92
|
key = uri.to_s
|
|
92
93
|
OnlineOrOffline.mutex.synchronize do
|
|
93
94
|
entry = OnlineOrOffline.cache[key]
|
|
94
95
|
return entry[:status] if entry && (Time.now - entry[:time]) < ttl
|
|
96
|
+
uri = URI(uri) unless uri.is_a?(URI)
|
|
95
97
|
status =
|
|
96
|
-
|
|
97
|
-
Net::HTTP.
|
|
98
|
+
begin
|
|
99
|
+
Net::HTTP.start(
|
|
100
|
+
uri.hostname,
|
|
101
|
+
uri.port,
|
|
102
|
+
use_ssl: uri.scheme == 'https',
|
|
103
|
+
open_timeout: timeout,
|
|
104
|
+
read_timeout: timeout,
|
|
105
|
+
write_timeout: timeout
|
|
106
|
+
) do |http|
|
|
107
|
+
http.request_get(uri, {}).is_a?(Net::HTTPSuccess)
|
|
108
|
+
end
|
|
98
109
|
rescue \
|
|
99
|
-
|
|
110
|
+
Socket::ResolutionError, Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout,
|
|
100
111
|
Errno::EHOSTUNREACH, Errno::EINVAL, Errno::EADDRNOTAVAIL, Errno::EBADF
|
|
101
112
|
false
|
|
102
113
|
end
|
data/online.gemspec
CHANGED
|
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
|
9
9
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
|
10
10
|
s.required_ruby_version = '>=3.2'
|
|
11
11
|
s.name = 'online'
|
|
12
|
-
s.version = '0.
|
|
12
|
+
s.version = '0.2.0'
|
|
13
13
|
s.license = 'MIT'
|
|
14
14
|
s.summary = 'Checks whether we are currently online or offline'
|
|
15
15
|
s.description =
|