bard 1.7.2 → 1.7.3

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: 674739cf14af03776e5f892cb47463decdc1a3a6a493ee4e62dc3528a4fe2c36
4
- data.tar.gz: 5685ced74cc962121ad634d9290aeba0663373c9db052ca3480d37f29ec8ec1b
3
+ metadata.gz: 68411db6b0d930d06cc7708835eaa23d10cbedc42eb905bec4065c47834f488a
4
+ data.tar.gz: e2488bd0f625e3aa7a87aa6f3334178dbdff49a19a7c020d2424b073dea3f999
5
5
  SHA512:
6
- metadata.gz: 734fe59b6c52c355168637b37685bda6a54dc05faf66db0d7758bcb00305a00069056915f433fc5359b36236896a300ac5162632872cc00e71c5bc7d730239de
7
- data.tar.gz: 924afe38e6cdf1dfa179917856d172c35c1d083571d1dfbfdce53d3c45e8b15950c94c3d6131c123d529005a80ba518ae7ffb420f9eb16fa700999a7cff8e432
6
+ metadata.gz: 1cc1f667c31694e70f54621b13d3871e74c011f4fd25ab80b66b6caa325fc89d878f4f4199d4adec314b599c4c9e8162ee12dd3e5830b255e9cfcde1c9504e06
7
+ data.tar.gz: 9f9be575ee8baa54d8f175c6a4c9227daa22b360343a1143736168d68b5f91697840819f9ed1f165b07e2cea2660c92bce2e466b72e87bf236f459cff304fed5
data/lib/bard/ping.rb CHANGED
@@ -8,16 +8,26 @@ module Bard
8
8
  end
9
9
 
10
10
  def call
11
- server.ping.reject do |url|
12
- response = get_response_with_redirect(url) rescue nil
13
- response.is_a?(Net::HTTPSuccess)
14
- end
11
+ server.ping.reject { |url| reachable?(url) }
15
12
  end
16
13
 
17
14
  private
18
15
 
16
+ def reachable?(url)
17
+ attempts = 0
18
+ begin
19
+ attempts += 1
20
+ response = get_response_with_redirect(url)
21
+ response.is_a?(Net::HTTPSuccess)
22
+ rescue StandardError
23
+ retry if attempts < 2
24
+ false
25
+ end
26
+ end
27
+
19
28
  def get_response_with_redirect uri_str, limit=5
20
- response = Net::HTTP.get_response(URI(uri_str))
29
+ uri = URI(uri_str)
30
+ response = http_get(uri)
21
31
 
22
32
  case response
23
33
  when Net::HTTPRedirection
@@ -25,11 +35,32 @@ module Bard
25
35
  puts "too many HTTP redirects"
26
36
  response
27
37
  else
28
- get_response_with_redirect(response["location"], limit - 1)
38
+ location = response["location"]
39
+ return response unless location
40
+
41
+ next_uri = begin
42
+ uri + location
43
+ rescue URI::InvalidURIError
44
+ URI(location)
45
+ end
46
+
47
+ get_response_with_redirect(next_uri, limit - 1)
29
48
  end
30
49
  else
31
50
  response
32
51
  end
33
52
  end
53
+
54
+ def http_get(uri)
55
+ Net::HTTP.start(
56
+ uri.host,
57
+ uri.port,
58
+ use_ssl: uri.scheme == "https",
59
+ open_timeout: 5,
60
+ read_timeout: 5,
61
+ ) do |http|
62
+ http.get(uri.request_uri)
63
+ end
64
+ end
34
65
  end
35
66
  end
data/lib/bard/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Bard
2
- VERSION = "1.7.2"
2
+ VERSION = "1.7.3"
3
3
  end
4
4
 
@@ -3,29 +3,58 @@ require "bard/ping"
3
3
 
4
4
  describe Bard::Ping do
5
5
  let(:server) { double("server", ping: ["http://example.com"]) }
6
+ let(:ping) { described_class.new(server) }
7
+
8
+ def success_response
9
+ Net::HTTPSuccess.new(1.0, "200", "OK")
10
+ end
11
+
12
+ def not_found_response
13
+ Net::HTTPNotFound.new(1.0, "404", "Not Found")
14
+ end
6
15
 
7
16
  context "when the server is reachable" do
8
- it "should return an empty array" do
9
- allow(Net::HTTP).to receive(:get_response).and_return(Net::HTTPSuccess.new(1.0, "200", "OK"))
10
- expect(Bard::Ping.call(server)).to be_empty
17
+ it "returns an empty array" do
18
+ allow(ping).to receive(:http_get).and_return(success_response)
19
+ expect(ping.call).to be_empty
11
20
  end
12
21
  end
13
22
 
14
23
  context "when the server is not reachable" do
15
- it "should return the url" do
16
- allow(Net::HTTP).to receive(:get_response).and_return(Net::HTTPNotFound.new(1.0, "404", "Not Found"))
17
- expect(Bard::Ping.call(server)).to eq(["http://example.com"])
24
+ it "returns the url" do
25
+ allow(ping).to receive(:http_get).and_return(not_found_response)
26
+ expect(ping.call).to eq(["http://example.com"])
18
27
  end
19
28
  end
20
29
 
21
30
  context "when there is a redirect" do
22
- it "should follow the redirect and return an empty array" do
31
+ it "follows the redirect and returns an empty array" do
23
32
  redirect_response = Net::HTTPRedirection.new(1.0, "301", "Moved Permanently")
24
- redirect_response["location"] = "http://example.com/new"
25
- success_response = Net::HTTPSuccess.new(1.0, "200", "OK")
26
- allow(Net::HTTP).to receive(:get_response).with(URI("http://example.com")).and_return(redirect_response)
27
- allow(Net::HTTP).to receive(:get_response).with(URI("http://example.com/new")).and_return(success_response)
28
- expect(Bard::Ping.call(server)).to be_empty
33
+ redirect_response["location"] = "/new"
34
+ allow(ping).to receive(:http_get).with(URI("http://example.com")).and_return(redirect_response)
35
+ allow(ping).to receive(:http_get).with(URI("http://example.com/new")).and_return(success_response)
36
+ expect(ping.call).to be_empty
37
+ end
38
+ end
39
+
40
+ context "when a transient error occurs" do
41
+ it "retries once before marking down" do
42
+ calls = 0
43
+ allow(ping).to receive(:http_get) do
44
+ calls += 1
45
+ raise Errno::ECONNRESET if calls == 1
46
+
47
+ success_response
48
+ end
49
+
50
+ expect(ping.call).to be_empty
51
+ end
52
+ end
53
+
54
+ context "when errors persist across retries" do
55
+ it "returns the url" do
56
+ allow(ping).to receive(:http_get).and_raise(Errno::ECONNREFUSED)
57
+ expect(ping.call).to eq(["http://example.com"])
29
58
  end
30
59
  end
31
60
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.2
4
+ version: 1.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-12-11 00:00:00.000000000 Z
10
+ date: 2025-12-15 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: thor