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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b676e3f37ff58fe5ad57c2bebe0afafeb9c522e18113ff6098f456f18b4d4df0
4
- data.tar.gz: c388c79309f619b4f9c5ffc58a680859281396d029ebf6fb3dbb2feddcf3571c
3
+ metadata.gz: e39ddd7a39de7638e54b088e1f350444bf0922860082e55d0705524b1df08dec
4
+ data.tar.gz: 88cdb650e021f7f195c7394c23b84166a54345d322c75ee603ab51e4033d88fd
5
5
  SHA512:
6
- metadata.gz: 5576f41fd242027a3401a428c491b9b507550460dab23505ae24b17ea833f5d352336a8485b6099b01de031d9b9d5b3ecdcc52ba4d32fb929aadddfcef38fd49
7
- data.tar.gz: '0068189a76b8a4da93194471c7d0798fb102c608f09c8e5cbccb9ada926e44b6dbed4f95939da4276d9c52ba364571fbb40b00a6913b270b0e793ac7bcb00e2b'
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.2.2)
14
+ bigdecimal (3.3.1)
15
15
  builder (3.3.0)
16
- crack (1.0.0)
16
+ crack (1.0.1)
17
17
  bigdecimal
18
18
  rexml
19
19
  docile (1.4.1)
20
- hashdiff (1.2.0)
21
- json (2.13.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.25.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.9.0)
31
+ parser (3.3.10.0)
32
32
  ast (~> 2.4.1)
33
33
  racc
34
- prism (1.4.0)
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.0)
39
- regexp_parser (2.11.2)
40
- rexml (3.4.2)
41
- rubocop (1.80.2)
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.46.0, < 2.0)
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.46.0)
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.0)
59
+ rubocop-performance (1.26.1)
60
60
  lint_roller (~> 1.1)
61
61
  rubocop (>= 1.75.0, < 2.0)
62
- rubocop-ast (>= 1.44.0, < 2.0)
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.1.5)
78
- unicode-emoji (~> 4.0, >= 4.0.4)
79
- unicode-emoji (4.0.4)
80
- webmock (3.25.1)
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.37)
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
@@ -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
@@ -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: 'http://www.google.com')
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: 'http://www.google.com', ttl: 300)
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
- Timeout.timeout(10) do
97
- Net::HTTP.get_response(URI(uri)).is_a?(Net::HTTPSuccess)
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
- Timeout::Error, Timeout::ExitException, Socket::ResolutionError,
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.1.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 =
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.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko