easy_retry 1.0.2 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d203deefb007c887481fc2b29f63da4582422c7fc20604acd9c9b88c0092f20c
4
- data.tar.gz: 64667abb6f3c5b3ef6a1cefbfe6bb2dbb0de5e0d09ed2b76f31a2a74e591d456
3
+ metadata.gz: b1628eebabfc2dd25f53d106aa0c665b20c2cc804ee7ff66a7372e784b02594d
4
+ data.tar.gz: 0a1c561a8b510b25c580f4a6df1e3d2af2b913459c30c8c44d30d7985203bb96
5
5
  SHA512:
6
- metadata.gz: a6ccf3c01e0b0fc33db703542107377e4c07088d38cd41657bcbd5ee0281098c94f4b93d07b2792e3c349b2030adb1045ea88a4cb56bd51ad4a4aeeee2eb2c2b
7
- data.tar.gz: 0bafb03fbf2c70833483f494fd12cfd0b206d38599119568be5a3e70c1f6a7882548315124d7071ef0261d834069e9d0595462468673465e54fadec6182d8ad8
6
+ metadata.gz: 8ace3d6f0a2e94bcc8e29cf04c9bf1b93a8e5e6d17a45c6b21d50d191a3279d50815f7f805aa21665aa114724ea6d064a9ade21e2939582464798ba6d01bf5d9
7
+ data.tar.gz: 839d5afade1c5e160fb2c7c181f7b952f30bd147246c3956b268ad0039e3343bab1ba8b4a90b197e3cc0565cc193cdce77780c94ec6b8c7147d4ed3eaeda3652
data/.rubocop.yml CHANGED
@@ -1,13 +1,12 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.6
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.6.0
3
4
 
4
5
  Style/StringLiterals:
5
6
  Enabled: true
6
- EnforcedStyle: double_quotes
7
7
 
8
8
  Style/StringLiteralsInInterpolation:
9
9
  Enabled: true
10
- EnforcedStyle: double_quotes
11
10
 
12
11
  Layout/LineLength:
13
12
  Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2022-11-17
3
+ ## [1.0.6] - TBD
4
+
5
+ - Allow configuration of logger
6
+
7
+ ## [Released]
8
+
9
+ ## [1.0.5] - 2022-11-22
10
+
11
+ - Raise before sleeping to not sleep for try^2 seconds before raising on the last try.
12
+ - Allow passing of single error
13
+ - Expand ReadMe
14
+
15
+ ## [1.0.4] - 2022-11-17
16
+
17
+ - Bugfix for EasyRetry to work in Rails _and_ non-Rails projects
18
+
19
+ ## [1.0.2] - 2022-11-17
20
+
21
+ - Improve the setup of the project
22
+
23
+ ## [1.0.0] - 2022-11-16
4
24
 
5
25
  - Initial release
data/Gemfile CHANGED
@@ -1,12 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source "https://rubygems.org"
3
+ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in easy_retry.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
8
+ gem 'rake', '~> 13.0'
9
9
 
10
- gem "rspec", "~> 3.0"
10
+ gem 'rspec', '~> 3.0'
11
11
 
12
- gem "rubocop", "~> 1.21"
12
+ gem 'rubocop', '~> 1.21'
13
+
14
+ gem 'pry'
15
+
16
+ gem 'simplecov', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ easy_retry (1.0.5)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.5.0)
12
+ docile (1.4.0)
13
+ json (2.6.2)
14
+ method_source (1.0.0)
15
+ parallel (1.22.1)
16
+ parser (3.1.2.1)
17
+ ast (~> 2.4.1)
18
+ pry (0.14.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.6.1)
24
+ rexml (3.2.5)
25
+ rspec (3.12.0)
26
+ rspec-core (~> 3.12.0)
27
+ rspec-expectations (~> 3.12.0)
28
+ rspec-mocks (~> 3.12.0)
29
+ rspec-core (3.12.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-expectations (3.12.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-mocks (3.12.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-support (3.12.0)
38
+ rubocop (1.39.0)
39
+ json (~> 2.3)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.1.2.1)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.23.0, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 1.4.0, < 3.0)
48
+ rubocop-ast (1.23.0)
49
+ parser (>= 3.1.1.0)
50
+ ruby-progressbar (1.11.0)
51
+ simplecov (0.21.2)
52
+ docile (~> 1.1)
53
+ simplecov-html (~> 0.11)
54
+ simplecov_json_formatter (~> 0.1)
55
+ simplecov-html (0.12.3)
56
+ simplecov_json_formatter (0.1.4)
57
+ unicode-display_width (2.3.0)
58
+
59
+ PLATFORMS
60
+ x86_64-darwin-21
61
+ x86_64-linux
62
+
63
+ DEPENDENCIES
64
+ easy_retry!
65
+ pry
66
+ rake (~> 13.0)
67
+ rspec (~> 3.0)
68
+ rubocop (~> 1.21)
69
+ simplecov
70
+
71
+ BUNDLED WITH
72
+ 2.3.7
data/README.md CHANGED
@@ -1,3 +1,7 @@
1
+ ![Specs](https://github.com/goudekettingrm/easy_retry/actions/workflows/main.yml/badge.svg)
2
+
3
+ <a href="https://www.buymeacoffee.com/goudekettingrm" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-blue.png" alt="Buy Me A Coffee" height="41" width="174"></a>
4
+
1
5
  # EasyRetry
2
6
 
3
7
  <i>Easily retry a block of code a predetermined number of times.</i>
@@ -65,6 +69,38 @@ The code above will not rescue from the `ActiveRecord::RecordInvalid` error and
65
69
  from (pry):16:in `block in __pry__'
66
70
  ```
67
71
 
72
+ Passing an array is not necessary if you need to only rescue from a single error
73
+
74
+ ```rb
75
+ 4.tries(rescue_from: ZeroDivisionError) do |try|
76
+ raise ZeroDivisionError if try < 2
77
+ raise ActiveRecord::RecordInvalid if try < 4
78
+ puts "Success!"
79
+ end
80
+ ```
81
+
82
+ This will generate the same output.
83
+
84
+ ## Block results
85
+
86
+ EasyRetry gives you back the result of the first time the block you passed successfully runs. This can be useful when you need to use the result of the block for other tasks that you do not necessarily want to place in the block.
87
+
88
+ ```rb
89
+ result = 2.tries do |try|
90
+ raise 'Woops' if try < 2
91
+ "This is try number #{try}"
92
+ end
93
+
94
+ puts result
95
+ ```
96
+
97
+ The code above will produce the following output.
98
+
99
+ ```
100
+ Error: Woops (1/2)
101
+ => "This is try number 2"
102
+ ```
103
+
68
104
  ## Retry delay
69
105
 
70
106
  The delay for each retry is based on the iteration count. The delay after each failed attempt is _n^2_, where _n_ is the current iteration that failed. E.g. after the first try, EasyRetry waits 1 second, after the second try it waits 4 seconds, then 9, then 16, then 25, then 36, etc.
@@ -77,7 +113,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
77
113
 
78
114
  ## Contributing
79
115
 
80
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easy_retry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/easy_retry/blob/main/CODE_OF_CONDUCT.md).
116
+ Bug reports and pull requests are welcome on GitHub at https://github.com/GoudekettingRM/easy_retry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/GoudekettingRM/easy_retry/blob/main/CODE_OF_CONDUCT.md).
81
117
 
82
118
  ## License
83
119
 
@@ -85,4 +121,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
85
121
 
86
122
  ## Code of Conduct
87
123
 
88
- Everyone interacting in the EasyRetry project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/easy_retry/blob/main/CODE_OF_CONDUCT.md).
124
+ Everyone interacting in the EasyRetry project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/GoudekettingRM/easy_retry/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
8
- require "rubocop/rake_task"
8
+ require 'rubocop/rake_task'
9
9
 
10
10
  RuboCop::RakeTask.new
11
11
 
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/easy_retry/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'easy_retry'
7
+ spec.version = EasyRetry::VERSION
8
+ spec.authors = ['Robin Goudeketting, Peter Duijnstee']
9
+ spec.email = ['robin@goudeketting.nl']
10
+
11
+ spec.summary = 'Easily retry a block of code a predetermined number of times'
12
+ spec.description = 'Easily retry a block of code a predetermined number of times'
13
+ spec.homepage = 'https://github.com/GoudekettingRM/easy_retry'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.6.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/GoudekettingRM/easy_retry'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/GoudekettingRM/easy_retry/blob/main/CHANGELOG.md'
20
+ spec.metadata['rubygems_mfa_required'] = 'true'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ # spec.add_dependency "example-gem", "~> 1.0"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyRetry
4
- VERSION = "1.0.2"
4
+ VERSION = '1.0.5'
5
5
  end
data/lib/easy_retry.rb CHANGED
@@ -1,28 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "easy_retry/version"
3
+ require_relative 'easy_retry/version'
4
4
 
5
+ # Extend the Numeric class with a #tries method
5
6
  class Numeric
7
+ # rubocop:disable Metrics/MethodLength
6
8
  def tries(rescue_from: [StandardError])
7
9
  raise ArgumentError, 'No block given' unless block_given?
8
10
 
11
+ rescue_from = Array(rescue_from)
9
12
  max_retry = self
10
13
  current_try = 1
14
+ result = nil
11
15
 
12
16
  loop do
13
- yield(current_try)
17
+ result = yield(current_try)
14
18
 
15
19
  break
16
- rescue *rescue_from => error
17
- Rails.logger.error "Error: #{error.message} (#{current_try}/#{max_retry})"
20
+ rescue *rescue_from => e
21
+ if defined?(Rails)
22
+ Rails.logger.error "Error: #{e.message} (#{current_try}/#{max_retry})"
23
+ else
24
+ puts "Error: #{e.message} (#{current_try}/#{max_retry})"
25
+ end
18
26
 
19
- sleep (current_try * current_try).seconds
27
+ raise if current_try >= max_retry
20
28
 
21
- current_try += 1
29
+ sleep current_try * current_try
22
30
 
23
- raise error if current_try > max_retry
31
+ current_try += 1
24
32
  end
33
+
34
+ result
25
35
  end
36
+ # rubocop:enable Metrics/MethodLength
26
37
 
27
38
  alias try tries
28
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_retry
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Goudeketting, Peter Duijnstee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-17 00:00:00.000000000 Z
11
+ date: 2022-11-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Easily retry a block of code a predetermined number of times
14
14
  email:
@@ -19,12 +19,15 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - ".rspec"
21
21
  - ".rubocop.yml"
22
+ - ".ruby-version"
22
23
  - CHANGELOG.md
23
24
  - CODE_OF_CONDUCT.md
24
25
  - Gemfile
26
+ - Gemfile.lock
25
27
  - LICENSE.txt
26
28
  - README.md
27
29
  - Rakefile
30
+ - easy_retry.gemspec
28
31
  - lib/easy_retry.rb
29
32
  - lib/easy_retry/version.rb
30
33
  - sig/easy_retry.rbs
@@ -35,6 +38,7 @@ metadata:
35
38
  homepage_uri: https://github.com/GoudekettingRM/easy_retry
36
39
  source_code_uri: https://github.com/GoudekettingRM/easy_retry
37
40
  changelog_uri: https://github.com/GoudekettingRM/easy_retry/blob/main/CHANGELOG.md
41
+ rubygems_mfa_required: 'true'
38
42
  post_install_message:
39
43
  rdoc_options: []
40
44
  require_paths: