tretry 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +3 -3
  3. data/lib/tretry.rb +44 -45
  4. metadata +48 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f933d5081600c123053375007affe0de256d65786552eeed9f795df3db5a587
4
- data.tar.gz: 97cb722fc297493e9e62df7f95aec8307f7d21904f56ec39d327426c200d8103
3
+ metadata.gz: 7ec5ecfe155371b1b3c9698118b3d1d01c6136125c94dc136925ae1473e234a6
4
+ data.tar.gz: 497ed8ab7b1bff0ae9f9c5025fab65b086459f6aa55b73fdfaf624a8a62ef875
5
5
  SHA512:
6
- metadata.gz: 6d8a9a03e39478b6174012c927e9356f63c2728d9362747f0e3345ee0d5173047e30c317d0ae67e8a3c928203d3a0f4eceedad64433faf74a738dadf881263a2
7
- data.tar.gz: a9af5b8ae687177475cf3aff52a6157dc8647676bc7519ccc01fadc98799166e12b6a5c030cf2f629ce2fee3bc08e7db58aedc9782783e16e95e9fea680c98c3
6
+ metadata.gz: f28e16cff32efb56d911ad726f2f23d8d8aebe1ad602ca364b9321caf67fc793528afe73a828ae97c5c503163a348aa38ef3dab8c9ac18a28644be68097db052
7
+ data.tar.gz: c40a00b1dbb9afa773e438dfdef671d4a614f5da4b852266ac80d6d1e6595238d14c688c6c4586cbffd7a383568aa233f52126c9b2aa99dec712278f57d99624
data/Rakefile CHANGED
@@ -3,8 +3,8 @@ require "bundler"
3
3
  begin
4
4
  Bundler.setup(:default, :development)
5
5
  rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
6
+ $stderr.warn e.message
7
+ $stderr.warn "Run `bundle install` to install missing gems"
8
8
  exit e.status_code
9
9
  end
10
10
  require "rake"
@@ -20,4 +20,4 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
20
20
  spec.rcov = true
21
21
  end
22
22
 
23
- task :default => :spec
23
+ task default: :spec
data/lib/tretry.rb CHANGED
@@ -1,9 +1,8 @@
1
- #A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.
1
+ # A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.
2
2
  class Tretry
3
3
  autoload :Result, "#{__dir__}/tretry/result"
4
4
 
5
- attr_reader :fails
6
- attr_accessor :timeout, :tries, :wait
5
+ attr_accessor :error, :errors, :exit, :fails, :interrupt, :return_error, :timeout, :tries, :wait
7
6
 
8
7
  #===Runs a block of code a given amount of times until it succeeds.
9
8
  #===Examples
@@ -18,68 +17,70 @@ class Tretry
18
17
  end
19
18
 
20
19
  def initialize(errors: nil, exit: true, interrupt: true, return_error: nil, timeout: nil, tries: 3, wait: nil)
21
- @errors = errors
22
- @exit = exit
23
- @fails = []
24
- @interrupt = interrupt
20
+ self.errors = errors
21
+ self.exit = exit
22
+ self.fails = []
23
+ self.interrupt = interrupt
25
24
  @before_retry = []
26
- @return_error = return_error
27
- @timeout = timeout
28
- @tries = tries
29
- @wait = wait
25
+ self.return_error = return_error
26
+ self.timeout = timeout
27
+ self.tries = tries
28
+ self.wait = wait
30
29
  end
31
30
 
32
31
  def before_retry(&block)
33
32
  @before_retry << block
34
33
  end
35
34
 
36
- def try(&block)
35
+ def try(&block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
37
36
  raise "No block given." unless block
37
+
38
38
  @block = block
39
39
 
40
- @tries.times do |count|
40
+ tries.times do |count|
41
41
  @count = count
42
42
 
43
43
  unless first_try?
44
44
  # Sleep for a given amount of time if the 'wait'-argument is given.
45
- sleep(@wait) if @wait
45
+ sleep(wait) if wait
46
46
 
47
- call_before_retry(error: @error)
48
- @error = nil
47
+ call_before_retry(error: error)
48
+ self.error = nil
49
49
  end
50
50
 
51
51
  begin
52
52
  # If a timeout-argument has been given, then run the code through the timeout.
53
- if @timeout
53
+ if timeout
54
54
  try_with_timeout
55
55
  else
56
56
  # Else call block normally.
57
57
  @res = @block.call
58
58
  @dobreak = true
59
59
  end
60
- rescue Exception => e
60
+ rescue Exception => e # rubocop:disable Lint/RescueException
61
61
  handle_error(e)
62
62
  end
63
63
 
64
64
  if @doraise
65
- if @return_error
66
- @fails << {error: @error}
65
+ if return_error
66
+ fails << {error: error}
67
+
67
68
  return Tretry::Result.new(
68
- fails: @fails,
69
+ fails: fails,
69
70
  error: true
70
71
  )
71
- else
72
- raise @error
73
72
  end
74
- elsif @error
75
- @fails << {error: @error}
73
+
74
+ raise error
75
+ elsif error
76
+ fails << {error: error}
76
77
  end
77
78
 
78
79
  break if @dobreak
79
80
  end
80
81
 
81
82
  Tretry::Result.new(
82
- fails: @fails,
83
+ fails: fails,
83
84
  result: @res,
84
85
  error: false
85
86
  )
@@ -88,29 +89,27 @@ class Tretry
88
89
  private
89
90
 
90
91
  def try_with_timeout
91
- begin
92
- require "timeout"
93
- Timeout.timeout(@timeout) do
94
- @res = @block.call
95
- @dobreak = true
96
- end
97
- rescue Timeout::Error => e
98
- handle_error(e)
92
+ require "timeout"
93
+ Timeout.timeout(timeout) do
94
+ @res = @block.call
95
+ @dobreak = true
99
96
  end
97
+ rescue Timeout::Error => e
98
+ handle_error(e)
100
99
  end
101
100
 
102
- def handle_error(e)
103
- if e.class == Interrupt
104
- raise e if @interrupt
105
- elsif e.class == SystemExit
106
- raise e if @exit
107
- elsif last_try? || @errors&.include?(e.class)
108
- @doraise = e
109
- elsif @errors&.include?(e.class)
101
+ def handle_error(error) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
102
+ if error.instance_of?(Interrupt)
103
+ raise error if interrupt
104
+ elsif error.instance_of?(SystemExit)
105
+ raise error if self.exit
106
+ elsif last_try? || (errors && !errors.include?(error.class))
107
+ @doraise = error
108
+ elsif errors&.include?(error.class) # rubocop:disable Lint/EmptyConditionalBody
110
109
  # Given error was in the :errors-array - do nothing. Maybe later it should be logged and returned in a stats-hash or something? - knj
111
110
  end
112
111
 
113
- @error = e
112
+ self.error = error
114
113
  end
115
114
 
116
115
  def call_before_retry(args)
@@ -120,10 +119,10 @@ private
120
119
  end
121
120
 
122
121
  def last_try?
123
- (@count + 1) >= @tries
122
+ (@count + 1) >= tries
124
123
  end
125
124
 
126
125
  def first_try?
127
- @count == 0
126
+ @count.zero?
128
127
  end
129
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tretry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Stöckel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,48 @@ dependencies:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.11.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-performance
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  description: A library for doing retries in Ruby with timeouts, analysis of errors,
42
84
  waits between tries and more.
43
85
  email: k@spernj.org
@@ -51,7 +93,8 @@ files:
51
93
  homepage: http://github.com/kaspernj/tretry
52
94
  licenses:
53
95
  - MIT
54
- metadata: {}
96
+ metadata:
97
+ rubygems_mfa_required: 'true'
55
98
  post_install_message:
56
99
  rdoc_options: []
57
100
  require_paths:
@@ -60,14 +103,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
103
  requirements:
61
104
  - - ">="
62
105
  - !ruby/object:Gem::Version
63
- version: '0'
106
+ version: '2.6'
64
107
  required_rubygems_version: !ruby/object:Gem::Requirement
65
108
  requirements:
66
109
  - - ">="
67
110
  - !ruby/object:Gem::Version
68
111
  version: '0'
69
112
  requirements: []
70
- rubygems_version: 3.1.6
113
+ rubygems_version: 3.0.3.1
71
114
  signing_key:
72
115
  specification_version: 4
73
116
  summary: A library for doing retries in Ruby with timeouts, analysis of errors, waits