online 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b676e3f37ff58fe5ad57c2bebe0afafeb9c522e18113ff6098f456f18b4d4df0
4
- data.tar.gz: c388c79309f619b4f9c5ffc58a680859281396d029ebf6fb3dbb2feddcf3571c
3
+ metadata.gz: 9ef9ba22638e58d94758ea94060301897d60251ff964c63b7a8cc45f238d4a3e
4
+ data.tar.gz: 412b3685da1492928b2de676d5b19cf706cc5a6b4ca5d9cb7c5d8627f27b805e
5
5
  SHA512:
6
- metadata.gz: 5576f41fd242027a3401a428c491b9b507550460dab23505ae24b17ea833f5d352336a8485b6099b01de031d9b9d5b3ecdcc52ba4d32fb929aadddfcef38fd49
7
- data.tar.gz: '0068189a76b8a4da93194471c7d0798fb102c608f09c8e5cbccb9ada926e44b6dbed4f95939da4276d9c52ba364571fbb40b00a6913b270b0e793ac7bcb00e2b'
6
+ metadata.gz: e75c5f2eda683c5fb86b1ec4e43cacc9df7bb028ac1c006f3d0ce206f9cf99791b8e14dd22eba8ce3e9f4ee474337b1cc5a578754aa730fdbcc00ebe00687413
7
+ data.tar.gz: f2dbd844247fd86d688d7b289525577b0c9993e40201a5cef318344855d4052f8a763f5f34e5c04f8942ab60c62fae122ddd5e7d93e8f491d38fbbe9136d8a57
data/Gemfile CHANGED
@@ -6,7 +6,7 @@
6
6
  source 'https://rubygems.org'
7
7
  gemspec
8
8
 
9
- gem 'minitest', '~>5.25', require: false
9
+ gem 'minitest', '~>6.0', require: false
10
10
  gem 'minitest-reporters', '~>1.7', require: false
11
11
  gem 'rake', '~>13.2', require: false
12
12
  gem 'rubocop', '~>1.73', require: false
data/Gemfile.lock CHANGED
@@ -2,43 +2,46 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  online (0.0.0)
5
+ openssl (>= 3)
5
6
  timeout (~> 0.4)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
10
- addressable (2.8.7)
11
- public_suffix (>= 2.0.2, < 7.0)
11
+ addressable (2.8.8)
12
+ public_suffix (>= 2.0.2, < 8.0)
12
13
  ansi (1.5.0)
13
14
  ast (2.4.3)
14
- bigdecimal (3.2.2)
15
+ bigdecimal (4.0.1)
15
16
  builder (3.3.0)
16
- crack (1.0.0)
17
+ crack (1.0.1)
17
18
  bigdecimal
18
19
  rexml
19
20
  docile (1.4.1)
20
- hashdiff (1.2.0)
21
- json (2.13.2)
21
+ hashdiff (1.2.1)
22
+ json (2.18.0)
22
23
  language_server-protocol (3.17.0.5)
23
24
  lint_roller (1.1.0)
24
- minitest (5.25.5)
25
+ minitest (6.0.1)
26
+ prism (~> 1.5)
25
27
  minitest-reporters (1.7.1)
26
28
  ansi
27
29
  builder
28
30
  minitest (>= 5.0)
29
31
  ruby-progressbar
32
+ openssl (4.0.0)
30
33
  parallel (1.27.0)
31
- parser (3.3.9.0)
34
+ parser (3.3.10.0)
32
35
  ast (~> 2.4.1)
33
36
  racc
34
- prism (1.4.0)
35
- public_suffix (6.0.2)
37
+ prism (1.7.0)
38
+ public_suffix (7.0.0)
36
39
  racc (1.8.1)
37
40
  rainbow (3.1.1)
38
- rake (13.3.0)
39
- regexp_parser (2.11.2)
40
- rexml (3.4.2)
41
- rubocop (1.80.2)
41
+ rake (13.3.1)
42
+ regexp_parser (2.11.3)
43
+ rexml (3.4.4)
44
+ rubocop (1.82.1)
42
45
  json (~> 2.3)
43
46
  language_server-protocol (~> 3.17.0.2)
44
47
  lint_roller (~> 1.1.0)
@@ -46,20 +49,20 @@ GEM
46
49
  parser (>= 3.3.0.2)
47
50
  rainbow (>= 2.2.2, < 4.0)
48
51
  regexp_parser (>= 2.9.3, < 3.0)
49
- rubocop-ast (>= 1.46.0, < 2.0)
52
+ rubocop-ast (>= 1.48.0, < 2.0)
50
53
  ruby-progressbar (~> 1.7)
51
54
  unicode-display_width (>= 2.4.0, < 4.0)
52
- rubocop-ast (1.46.0)
55
+ rubocop-ast (1.49.0)
53
56
  parser (>= 3.3.7.2)
54
- prism (~> 1.4)
57
+ prism (~> 1.7)
55
58
  rubocop-minitest (0.38.2)
56
59
  lint_roller (~> 1.1)
57
60
  rubocop (>= 1.75.0, < 2.0)
58
61
  rubocop-ast (>= 1.38.0, < 2.0)
59
- rubocop-performance (1.26.0)
62
+ rubocop-performance (1.26.1)
60
63
  lint_roller (~> 1.1)
61
64
  rubocop (>= 1.75.0, < 2.0)
62
- rubocop-ast (>= 1.44.0, < 2.0)
65
+ rubocop-ast (>= 1.47.1, < 2.0)
63
66
  rubocop-rake (0.7.1)
64
67
  lint_roller (~> 1.1)
65
68
  rubocop (>= 1.72.1)
@@ -73,15 +76,15 @@ GEM
73
76
  simplecov (~> 0.19)
74
77
  simplecov-html (0.13.2)
75
78
  simplecov_json_formatter (0.1.4)
76
- timeout (0.4.3)
77
- unicode-display_width (3.1.5)
78
- unicode-emoji (~> 4.0, >= 4.0.4)
79
- unicode-emoji (4.0.4)
80
- webmock (3.25.1)
79
+ timeout (0.6.0)
80
+ unicode-display_width (3.2.0)
81
+ unicode-emoji (~> 4.1)
82
+ unicode-emoji (4.2.0)
83
+ webmock (3.26.1)
81
84
  addressable (>= 2.8.0)
82
85
  crack (>= 0.3.2)
83
86
  hashdiff (>= 0.4.0, < 2.0.0)
84
- yard (0.9.37)
87
+ yard (0.9.38)
85
88
 
86
89
  PLATFORMS
87
90
  aarch64-linux
@@ -94,7 +97,7 @@ PLATFORMS
94
97
  x86_64-linux
95
98
 
96
99
  DEPENDENCIES
97
- minitest (~> 5.25)
100
+ minitest (~> 6.0)
98
101
  minitest-reporters (~> 1.7)
99
102
  online!
100
103
  rake (~> 13.2)
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
@@ -33,6 +33,7 @@ require 'yard'
33
33
  desc 'Build Yard documentation'
34
34
  YARD::Rake::YardocTask.new do |t|
35
35
  t.files = ['lib/**/*.rb']
36
+ t.options = ['--fail-on-warning']
36
37
  end
37
38
 
38
39
  require 'rubocop/rake_task'
data/lib/online.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  # SPDX-FileCopyrightText: Copyright (c) 2024-2025 Yegor Bugayenko
4
4
  # SPDX-License-Identifier: MIT
5
5
 
6
+ require 'openssl'
6
7
  require 'net/http'
7
8
  require 'socket'
8
9
  require 'timeout'
@@ -30,8 +31,9 @@ module Kernel
30
31
  # for checking internet availability before performing network-dependent operations
31
32
  # or for implementing offline-mode functionality in applications.
32
33
  #
33
- # @param uri [String] the URI to check connectivity against (default: 'http://www.google.com')
34
+ # @param uri [String] the URI to check connectivity against (default: 'https://www.google.com/generate_204')
34
35
  # @param ttl [Integer] time-to-live for cached results in seconds (default: 300 seconds = 5 minutes)
36
+ # @param timeout [Integer] open, read and write HTTP timeout (default: 10 seconds)
35
37
  # @return [Boolean] true if the URI is reachable and returns HTTP success, false otherwise
36
38
  #
37
39
  # @example Basic usage - check internet connectivity
@@ -85,19 +87,29 @@ module Kernel
85
87
  # @note Returns false for any network-related errors including timeouts, DNS failures,
86
88
  # and unreachable hosts
87
89
  # @note Results are cached with a default TTL of 5 minutes to reduce network requests
88
- def online?(uri: 'http://www.google.com', ttl: 300)
90
+ def online?(uri: 'https://www.google.com/generate_204', ttl: 300, timeout: 10)
89
91
  raise 'The URI is nil' if uri.nil?
90
92
  raise 'The TTL is nil' if ttl.nil?
91
93
  key = uri.to_s
92
94
  OnlineOrOffline.mutex.synchronize do
93
95
  entry = OnlineOrOffline.cache[key]
94
96
  return entry[:status] if entry && (Time.now - entry[:time]) < ttl
97
+ uri = URI(uri) unless uri.is_a?(URI)
95
98
  status =
96
- Timeout.timeout(10) do
97
- Net::HTTP.get_response(URI(uri)).is_a?(Net::HTTPSuccess)
99
+ begin
100
+ Net::HTTP.start(
101
+ uri.hostname,
102
+ uri.port,
103
+ use_ssl: uri.scheme == 'https',
104
+ open_timeout: timeout,
105
+ read_timeout: timeout,
106
+ write_timeout: timeout
107
+ ) do |http|
108
+ http.request_get(uri, {}).is_a?(Net::HTTPSuccess)
109
+ end
98
110
  rescue \
99
- Timeout::Error, Timeout::ExitException, Socket::ResolutionError,
100
- Errno::EHOSTUNREACH, Errno::EINVAL, Errno::EADDRNOTAVAIL, Errno::EBADF
111
+ Socket::ResolutionError, Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout,
112
+ Errno::EHOSTUNREACH, Errno::EINVAL, Errno::EADDRNOTAVAIL, Errno::EBADF, OpenSSL::SSL::SSLError
101
113
  false
102
114
  end
103
115
  OnlineOrOffline.cache[key] = { status: status, time: Time.now }
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.1.0'
12
+ s.version = '0.3.0'
13
13
  s.license = 'MIT'
14
14
  s.summary = 'Checks whether we are currently online or offline'
15
15
  s.description =
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
23
  s.rdoc_options = ['--charset=UTF-8']
24
24
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
25
+ s.add_dependency 'openssl', '>=3'
25
26
  s.add_dependency 'timeout', '~>0.4'
26
27
  s.metadata['rubygems_mfa_required'] = 'true'
27
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: online
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -9,6 +9,20 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: openssl
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '3'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: timeout
14
28
  requirement: !ruby/object:Gem::Requirement