retrier 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a7a979e1dba5b5a2fc42472e7b3bda3237249dff
4
+ data.tar.gz: d2e25de2e347612d5309baff7c756e407a1054c3
5
+ SHA512:
6
+ metadata.gz: 1749e26444a75b2a35a41a5f5659a75f7cef65b12d1c659d5fb80c09ee12bd2ed11efd9b3e0f97e2884091d4f418a72897af834bb76a230c8b504f049f1b3af9
7
+ data.tar.gz: 0162115d3dd12f5f6722fecfa8236ff4b1455568c83a5397340c2cc9e8a8254a742cadb156609540d0077752ee57bb0636232c23862bd7e7660d5a7e69663ac3
@@ -0,0 +1,26 @@
1
+ #Retrier
2
+
3
+ Retry a code block the given number of times.
4
+
5
+ ##Usage
6
+
7
+ ###Basic usage
8
+ ```ruby
9
+ Retrier.new(max_tries: 5) do
10
+ do_something_which_may_fail
11
+ end
12
+ ```
13
+
14
+ ###Exception handlers
15
+ If supplied with a list of handler functions, `Retrier` will call the handler method. If there isn't a registered handler for the raised exception it will retry the block.
16
+ ```ruby
17
+ handlers = {
18
+ StandardError: (exception) -> {
19
+ do_something_if_standard_error_has_beenraised
20
+ }
21
+ }
22
+
23
+ Retrier.new(handlers: handlers) do
24
+ risky_operation
25
+ end
26
+ ```
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ class Retrier
2
+ def initialize(options={}, &block)
3
+ max_tries = options.fetch(:max_tries, 10)
4
+ rescuable = Array(options.fetch(:rescue, StandardError))
5
+ handlers = options[:handlers]
6
+
7
+ attempts = 0
8
+
9
+ begin
10
+ attempts += 1
11
+ return block.call(attempts)
12
+ rescue *rescuable => exception
13
+ if handlers
14
+ handlers.each do |ex,handler|
15
+ handler.call(exception) if Kernel.const_get(ex) == exception.class
16
+ end
17
+ elsif attempts >= max_tries
18
+ raise exception
19
+ else
20
+ retry
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "retrier"
5
+ gem.version = "1.0.0"
6
+ gem.authors = ["Boris Filipov"]
7
+ gem.email = ["bfilipov+rubygems@gmail.com"]
8
+ gem.description = %q{ Retries a code block the given number of times }
9
+ gem.homepage = "https://github.com/borfd/retrier"
10
+ gem.license = "MIT"
11
+ gem.summary = %q{Execute any code block an arbitrary number of times. Optionally supply exception handlers.}
12
+ gem.files = `git ls-files`.split($/)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.require_paths = ["lib"]
16
+
17
+ gem.add_development_dependency "rake"
18
+ end
@@ -0,0 +1,66 @@
1
+ require 'retrier'
2
+
3
+ class MyCustomTestException < RuntimeError; end
4
+ class MyCustomTestExceptionB < RuntimeError; end
5
+
6
+ describe Retrier do
7
+
8
+ let(:max_tries) { 4 }
9
+
10
+ it "runs only once if there is no exception" do
11
+ dbl = double
12
+ dbl.should_receive(:test_this).exactly(1).times
13
+
14
+ Retrier.new do
15
+ dbl.test_this
16
+ end
17
+ end
18
+
19
+ it "retries the given number of times" do
20
+ tries = 0
21
+ dbl = double
22
+ dbl.should_receive(:test_this).exactly(max_tries).times
23
+
24
+ Retrier.new(max_tries: max_tries) do |attempts|
25
+ tries += 1
26
+ dbl.test_this
27
+ fail StandardError unless tries == max_tries
28
+ end
29
+
30
+ end
31
+
32
+ it "doesn't catch an exception which isn't passed" do
33
+ expect {
34
+ Retrier.new(rescue: MyCustomTestException) do
35
+ fail MyCustomTestExceptionB
36
+ end
37
+ }.to raise_error(MyCustomTestExceptionB)
38
+ end
39
+
40
+ context "with registered exception handlers" do
41
+ let(:dbl) { double }
42
+ let(:handlers) do
43
+ {
44
+ MyCustomTestException: -> (ex) {
45
+ dbl.my_custom_exception_raised
46
+ },
47
+ MyCustomTestExceptionB: -> (ex) {
48
+ dbl.this_should_not_happen
49
+ },
50
+ StandardError: -> (ex) {
51
+ dbl.this_should_not_happen
52
+ }
53
+ }
54
+ end
55
+
56
+ it "rescues exception and calls appropriate handler" do
57
+ dbl.should_receive(:my_custom_exception_raised)
58
+ dbl.should_not_receive(:this_should_not_happen)
59
+
60
+ Retrier.new(handlers: handlers) do
61
+ raise MyCustomTestException
62
+ end
63
+ end
64
+
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: retrier
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Boris Filipov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: ' Retries a code block the given number of times '
28
+ email:
29
+ - bfilipov+rubygems@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - lib/retrier.rb
37
+ - retrier.gemspec
38
+ - spec/retrier_spec.rb
39
+ homepage: https://github.com/borfd/retrier
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.14
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Execute any code block an arbitrary number of times. Optionally supply exception
63
+ handlers.
64
+ test_files:
65
+ - spec/retrier_spec.rb