poller_bear 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fcbb4848b7d8352155a1760834c40a2388829a13b4e36c8f70afce9460376d5d
4
+ data.tar.gz: 0acf773eb5e5fc7d68a600e01470a8346c7df67ddd34ad0f39f1d1e043ad06d0
5
+ SHA512:
6
+ metadata.gz: c3436db17177ff398e1a58bdaa1cf795231f77f9f61197c80fd17189189ad4880f460aef1ef1e196501dac677d4f4b279f045829133c8ae44e9228afa0188267
7
+ data.tar.gz: b5945ff217bb36f41646387da3f0d99d8dc0f9d5c8ccffe5542649753645f7e221fe7c91dfca83a4e7d89d963dda7e70d35d211d8c2ab729c7af637be92bcefd
@@ -0,0 +1,7 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ open-pull-requests-limit: 10
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in poller_bear.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ poller_bear (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.25.4)
10
+ rake (10.5.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.17)
17
+ minitest (~> 5.0)
18
+ poller_bear!
19
+ rake (~> 10.0)
20
+
21
+ BUNDLED WITH
22
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Bassem Mawhoob
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,161 @@
1
+ # PollerBear
2
+
3
+ A zero-dependency Ruby gem built for effortless polling.
4
+ Perfect for external APIs and any task that requires, repeatable retries until your conditions are met.
5
+ Elegant and beautifully expressive.
6
+
7
+ ```ruby
8
+ PollerBear.poll(every: 1, for: 15, stop_when: -> (response, attempt) { response.success? }) do
9
+ make_api_call
10
+ end
11
+
12
+ # => { "status" => 200, "body" => "Yay, the API finally worked!" }
13
+ ```
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'poller_bear'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install poller_bear
30
+
31
+ ## Usage
32
+
33
+ PollerBear exposes a single method poll
34
+
35
+ ```ruby
36
+ # @param [Hash] options
37
+ # @option options [Float, Symbol] :every The interval in seconds between each poll
38
+ # or +:exponential+ for exponential backoff (default: 1.0)
39
+ # @option options [Float] :for The total duration in seconds to poll for (default: nil, meaning no
40
+ # time limit)
41
+ # @option options [Integer] :max_retries The maximum number of retries on failure (default: nil,
42
+ # meaning unlimited)
43
+ # @option options [Proc] :stop_when A lambda that takes the result and attempt number,
44
+ # and returns true to stop polling (default: true, meaning stop after the first attempt if no errors)
45
+ # @option options [Boolean, Array<StandardError>] :retry_on_exceptions Whether to retry on exceptions
46
+ # raised in the block.
47
+ poll(every: 1.0, for: nil, max_retries: nil, stop_when: ->(result, attempt) { true }, retry_on_exceptions: false, &block)
48
+ ```
49
+
50
+ ### Examples
51
+
52
+ - Polling until timeout
53
+
54
+ ```ruby
55
+ PollerBear.poll(every: 1, for: 10, stop_when: ->(result, attempt) { result == :done }) do |attempt|
56
+ puts "Polling..."
57
+ end
58
+
59
+ # => "Polling..."
60
+ # => "Polling..."
61
+ # .. (repeated every second for 10 seconds)
62
+ # PollerBear::TimeoutError raised
63
+ ```
64
+
65
+ - Polling with maximum retries
66
+
67
+ ```ruby
68
+ PollerBear.poll(every: 2, max_retries: 3, stop_when: ->(result, attempt) { result == :done }) do |attempt|
69
+ puts "Polling..."
70
+ end
71
+
72
+ # => "Polling..."
73
+ # => "Polling..."
74
+ # => "Polling..."
75
+ # PollerBear::MaxRetriesExceededError raised
76
+ ```
77
+
78
+ - Built-in exponential backoff (base: 0.5 seconds, factor: 2)
79
+
80
+ ```ruby
81
+ start = Time.now
82
+ PollerBear.poll(every: :exponential, max_retries: 4, stop_when: ->(result, attempt) { result == :done }) do |attempt|
83
+ puts Time.now - start
84
+ end
85
+ # => 0 seconds
86
+ # => 0.5 seconds
87
+ # => 1.5 seconds
88
+ # => 3.5 seconds
89
+ # PollerBear::MaxRetriesExceededError raised
90
+ ```
91
+
92
+ - Custom wait time
93
+
94
+ ```ruby
95
+ start = Time.now
96
+ PollerBear.poll(every: -> (attempt) { attempt + 2 }, for: 20, stop_when: ->(result, attempt) { result == :done }) do
97
+ puts Time.now - start
98
+ end
99
+
100
+ # => 0 seconds
101
+ # => 3 seconds
102
+ # => 7 seconds
103
+ # => 12 seconds
104
+ # => 18 seconds
105
+ # PollerBear::TimeoutError raised
106
+ ```
107
+
108
+ - Retry on exceptions
109
+
110
+ ```ruby
111
+ PollerBear.poll(every: 1, max_retries: 5, retry_on_exceptions: true, stop_when: ->(result, attempt) { result == :done }) do |attempt|
112
+ puts "Attempt #{attempt}"
113
+ raise "Temporary failure" if attempt < 4
114
+ :done
115
+ end
116
+
117
+ # => "Attempt 1"
118
+ # => "Attempt 2"
119
+ # => "Attempt 3"
120
+ # => "Attempt 4"
121
+ # => :done
122
+ ```
123
+
124
+ - Retry on specific exceptions
125
+
126
+ ```ruby
127
+ PollerBear.poll(every: 1, max_retries: 5, retry_on_exceptions: [RuntimeError], stop_when: ->(result, attempt) { result == :done }) do |attempt|
128
+ puts "Attempt #{attempt}"
129
+ raise RuntimeError, "Temporary failure" if attempt < 4
130
+ :done
131
+ end
132
+
133
+ # => "Attempt 1"
134
+ # => "Attempt 2"
135
+ # => "Attempt 3"
136
+ # => "Attempt 4"
137
+ # => :done
138
+ ```
139
+
140
+ - Passing `every` and `for` as durations (when ActiveSupport is available)
141
+
142
+ ```ruby
143
+ PollerBear.poll(every: 5.seconds, for: 2.minutes, stop_when: ->(result, attempt) { result == :done }) do
144
+ puts "Polling..."
145
+ end
146
+ ```
147
+
148
+
149
+ ## Development
150
+
151
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
152
+
153
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
154
+
155
+ ## Contributing
156
+
157
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bassemawhoob/poller_bear.
158
+
159
+ ## License
160
+
161
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "poller_bear"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,87 @@
1
+ module PollerBear
2
+ class TimeoutError < StandardError; end
3
+
4
+ class MaxRetriesExceededError < StandardError; end
5
+
6
+ class Base
7
+ attr_reader :interval, :end_time, :max_retries, :stop_when, :retry_on_exceptions
8
+
9
+ # @param [Hash] options
10
+ # @option options [Float, Symbol] :every The interval in seconds between each poll
11
+ # or +:exponential+ for exponential backoff (default: 1.0)
12
+ # @option options [Float] :for The total duration in seconds to poll for (default: nil, meaning no
13
+ # time limit)
14
+ # @option options [Integer] :max_retries The maximum number of retries on failure (default: nil,
15
+ # meaning unlimited)
16
+ # @option options [Proc] :stop_when A lambda that takes the result and attempt number,
17
+ # and returns true to stop polling (default: true, meaning stop after the first attempt if no errors)
18
+ # @option options [Boolean, Array<StandardError>] :retry_on_exceptions Whether to retry on exceptions
19
+ # raised in the block.
20
+ def initialize(**options)
21
+ @interval = options.fetch(:every, 1.0).then { |val| val.respond_to?(:to_f) ? val.to_f : val }
22
+ @end_time = options.fetch(:for, nil)&.then { |duration| Time.now + duration.to_f }
23
+ @max_retries = options.fetch(:max_retries, nil)
24
+ @stop_when = options.fetch(:stop_when, -> (_result, _attempt) { true })
25
+ @retry_on_exceptions = options.fetch(:retry_on_exceptions, false)
26
+
27
+ warn_on_infinite_polling if @end_time.nil? && options[:stop_when].nil?
28
+ end
29
+
30
+ def poll(&)
31
+ raise ArgumentError, "A block must be provided to poll" unless block_given?
32
+ attempts = max_retries ? max_retries : Float::INFINITY
33
+ error = nil
34
+
35
+ 1.upto(attempts) do |attempt|
36
+ if end_time && Time.now >= end_time
37
+ raise TimeoutError.new("Polling timed out"), cause: error
38
+ end
39
+
40
+ result = yield(attempt)
41
+ return result if stop_when && stop_when.call(result, attempt)
42
+
43
+ sleep_interval(attempt)
44
+ rescue StandardError => error
45
+ if should_retry_on_exception?(error)
46
+ sleep_interval(attempt)
47
+ next
48
+ else
49
+ raise error
50
+ end
51
+ end
52
+
53
+ raise MaxRetriesExceededError.new("Polled maximum number of retries"), cause: error
54
+ end
55
+
56
+ private
57
+
58
+ def warn_on_infinite_polling
59
+ warn <<~STRING
60
+ [PollerBear] Warning: Polling with no time limit and no stop condition will lead to infinite loops.
61
+ STRING
62
+ end
63
+
64
+ def sleep_interval(attempt)
65
+ sleep_duration = if interval.is_a?(Proc)
66
+ interval.call(attempt)
67
+ elsif interval == :exponential
68
+ 0.5 * (2 ** (attempt - 1))
69
+ else
70
+ interval
71
+ end
72
+ sleep(sleep_duration)
73
+ end
74
+
75
+ def should_retry_on_exception?(exception)
76
+ return false unless retry_on_exceptions
77
+
78
+ if retry_on_exceptions == true
79
+ true
80
+ elsif retry_on_exceptions.is_a?(Array)
81
+ retry_on_exceptions.any? { |ex_class| exception.is_a?(ex_class) }
82
+ else
83
+ false
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module PollerBear
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "poller_bear/version"
2
+ require "poller_bear/base"
3
+
4
+ module PollerBear
5
+ def self.poll(**options, &)
6
+ PollerBear::Base.new(**options).poll(&)
7
+ end
8
+ end
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "poller_bear/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "poller_bear"
8
+ spec.version = PollerBear::VERSION
9
+ spec.authors = ["Bassem Mawhoob"]
10
+ spec.email = ["bassem.mawhoob@live.com"]
11
+
12
+ spec.summary = %q{A lightweight Ruby gem for polling anything}
13
+ spec.description = %q{A zero-dependency Ruby gem built for effortless polling. Perfect for external APIs
14
+ and any task that requires, repeatable retries until your conditions are met. Elegant and beautifully
15
+ expressive.}
16
+ spec.homepage = "https://github.com/bassemawhoob/poller_bear"
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
23
+
24
+ spec.metadata["homepage_uri"] = spec.homepage
25
+ spec.metadata["source_code_uri"] = spec.homepage
26
+ else
27
+ raise "RubyGems 2.0 or newer is required to protect against " \
28
+ "public gem pushes."
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = "exe"
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = ["lib"]
39
+
40
+ spec.add_development_dependency "bundler", "~> 1.17"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "minitest", "~> 5.0"
43
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poller_bear
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bassem Mawhoob
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: |-
56
+ A zero-dependency Ruby gem built for effortless polling. Perfect for external APIs
57
+ and any task that requires, repeatable retries until your conditions are met. Elegant and beautifully
58
+ expressive.
59
+ email:
60
+ - bassem.mawhoob@live.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".github/dependabot.yml"
66
+ - ".gitignore"
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/poller_bear.rb
75
+ - lib/poller_bear/base.rb
76
+ - lib/poller_bear/version.rb
77
+ - poller_bear.gemspec
78
+ homepage: https://github.com/bassemawhoob/poller_bear
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ allowed_push_host: https://rubygems.org
83
+ homepage_uri: https://github.com/bassemawhoob/poller_bear
84
+ source_code_uri: https://github.com/bassemawhoob/poller_bear
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.3.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: A lightweight Ruby gem for polling anything
104
+ test_files: []