try_again 1.0.0 → 2.0.0

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 +7 -0
  2. data/README +3 -0
  3. data/lib/try_again.rb +13 -15
  4. metadata +27 -37
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f543d0fe4fdeaefbdd2a0a9ddc2a36b9ec20c062
4
+ data.tar.gz: a3c2572280de7f2eb06eb02a16a7fdf74aee9b02
5
+ SHA512:
6
+ metadata.gz: 4e873a8df0938b8f635f9970c79c31b2a9d33eb4a92030fb3f1be301930ef501de25db37ac252a1cf5b03397606d3ef007764230adf0a9a2868ba51967afe54f
7
+ data.tar.gz: ddfae4232b83ca05acf5c3a5c1ab932eee37b4c577b064be51bb6b1c82330340710e392e1cb43dfecf95883f7061a53959d90feb3b3c10dd5f8c0998fdd977a4
data/README CHANGED
@@ -5,6 +5,9 @@ This is one of the first few pieces of code I ever wrote in Ruby, it was intente
5
5
 
6
6
  It's really easy to use
7
7
  Just do
8
+
9
+ require 'try_again'
10
+
8
11
  TryAgain.retry do
9
12
  some code
10
13
  end
data/lib/try_again.rb CHANGED
@@ -1,34 +1,32 @@
1
- #@version 1
1
+ #@version 2
2
2
  #@author Arthur
3
3
  module TryAgain
4
4
  #@param options [Hash] (:sleep => number, :attempts => number, :error => Error, :kill => boolean)
5
5
  #@param block [Block] block of code to be attempted
6
6
  #@return [Boolean]
7
- def self.retry( options={}, &block )
8
- defaults = { :sleep => 3, :attempts => 3, :error => StandardError, :kill => false, :output => nil }
9
- options = defaults.merge options
10
- out = options[:output]
7
+ def self.retry( sleep: 3, attempts: 3, error: StandardError, kill: false, output: nil, &block )
8
+ out = output
11
9
  if out and !out.is_a? IO
12
10
  raise InvalidOutput
13
11
  end
14
- attempts = 0
15
- failed = false
12
+ attempted = 0
13
+ failed = false
16
14
 
17
15
  begin
18
16
  yield
19
- rescue options[:error]
20
- attempts += 1
21
- out.puts "#{ options[:error].to_s } for the #{attempts}# attempt" if out
22
- if attempts < options[:attempts]
23
- sleep options[:sleep]
17
+ rescue error => e
18
+ attempted += 1
19
+ out.puts "#{ error.to_s } for the #{attempts}# attempt" if out
20
+ if attempted < attempts
21
+ sleep sleep
24
22
  retry
25
23
  end
26
- out.puts "#Giving up on #{ options[:error].to_s }, too many attempts" if out
27
- raise options[:error] if options[:kill]
24
+ out.puts "#Giving up on #{ error.to_s }, too many attempts" if out
25
+ raise e if kill
28
26
  failed = true
29
27
  end
30
28
  if !failed and out
31
- out.puts "#{ options[:error].to_s } took #{attempts + 1} attempts to pass"
29
+ out.puts "#{ error.to_s } took #{attempts + 1} attempts to pass"
32
30
  end
33
31
  return !failed
34
32
  end
metadata CHANGED
@@ -1,56 +1,46 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: try_again
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
6
5
  platform: ruby
7
- authors:
8
- - Arthur Silva
6
+ authors:
7
+ - Arthur Silva
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2012-09-03 00:00:00 Z
11
+ date: 2014-12-16 00:00:00.000000000 Z
14
12
  dependencies: []
15
-
16
- description: " This method is usefull for when you want to attempt to get a resource on a network and it may fail \n\n Or maybe you know that there's a possibility that the databases haven't sync yet and you want to try a few times till they do\n"
13
+ description: " This method is usefull for when you want to attempt to get a resource
14
+ on a network and it may fail \n\n Or maybe you know that there's a possibility
15
+ that the databases haven't sync yet and you want to try a few times till they do\n"
17
16
  email: awls99@gmail.com
18
17
  executables: []
19
-
20
18
  extensions: []
21
-
22
19
  extra_rdoc_files: []
23
-
24
- files:
25
- - lib/try_again.rb
26
- - README
20
+ files:
21
+ - lib/try_again.rb
22
+ - README
27
23
  homepage: https://github.com/awls99/Try-Again
28
24
  licenses: []
29
-
25
+ metadata: {}
30
26
  post_install_message:
31
27
  rdoc_options: []
32
-
33
- require_paths:
34
- - lib
35
- required_ruby_version: !ruby/object:Gem::Requirement
36
- none: false
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: "0"
41
- required_rubygems_version: !ruby/object:Gem::Requirement
42
- none: false
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: "0"
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
47
40
  requirements: []
48
-
49
41
  rubyforge_project:
50
- rubygems_version: 1.8.15
42
+ rubygems_version: 2.1.11
51
43
  signing_key:
52
- specification_version: 3
44
+ specification_version: 4
53
45
  summary: A helping method to to retry a block a few times with a sleep in between
54
46
  test_files: []
55
-
56
- has_rdoc: