with_retries 0.0.1 → 0.0.2

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
+ SHA1:
3
+ metadata.gz: a246d623e3529e5e633805268c6c2677f78a3791
4
+ data.tar.gz: 59f954301fa56ee90c1f30b5879fa4a44c377c93
5
+ SHA512:
6
+ metadata.gz: 33d7e074f2f5e025db49f12b58b3a7410b63acfdf3cd60f0d2d367a94577323a93ab65d38c3ff5d7312ccf412457cf6b41bb3cc4f8e6ba7dd8983730030d0923
7
+ data.tar.gz: bba2dba316f56ceee9673150a9e7c6d4f5bc8bcff00fe51f32629d634d1b9231954aae6ba95bcf736fed639ab10f63136e2ba103149f78cd9a14581a0b0b8f67
data/Guardfile ADDED
@@ -0,0 +1,69 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'bundler' do
5
+ watch('Gemfile')
6
+ # Uncomment next line if Gemfile contain `gemspec' command
7
+ # watch(/^.+\.gemspec/)
8
+ end
9
+
10
+ guard 'livereload' do
11
+ watch(%r{app/.+\.(erb|haml)})
12
+ watch(%r{app/helpers/.+\.rb})
13
+ watch(%r{(public/|app/assets).+\.(css|js|html)})
14
+ watch(%r{(app/assets/.+\.css)\.s[ac]ss}) { |m| m[1] }
15
+ watch(%r{(app/assets/.+\.js)\.coffee}) { |m| m[1] }
16
+ watch(%r{config/locales/.+\.yml})
17
+ end
18
+
19
+ guard 'rails' do
20
+ watch('Gemfile.lock')
21
+ watch(%r{^(config|lib)/.*})
22
+ end
23
+
24
+
25
+ guard :rspec do
26
+ watch(%r{^spec/.+_spec\.rb$})
27
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
28
+ watch('spec/spec_helper.rb') { "spec" }
29
+
30
+ # Rails example
31
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
32
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
33
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
34
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
35
+ watch('config/routes.rb') { "spec/routing" }
36
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
37
+
38
+ # Capybara features specs
39
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
40
+
41
+ # Turnip features and steps
42
+ watch(%r{^spec/acceptance/(.+)\.feature$})
43
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
44
+ end
45
+
46
+
47
+ guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
48
+ watch('config/application.rb')
49
+ watch('config/environment.rb')
50
+ watch('config/environments/test.rb')
51
+ watch(%r{^config/initializers/.+\.rb$})
52
+ watch('Gemfile')
53
+ watch('Gemfile.lock')
54
+ watch('spec/spec_helper.rb') { :rspec }
55
+ watch('test/test_helper.rb') { :test_unit }
56
+ watch(%r{features/support/}) { :cucumber }
57
+ end
58
+
59
+ guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
60
+ watch('config/application.rb')
61
+ watch('config/environment.rb')
62
+ watch('config/environments/test.rb')
63
+ watch(%r{^config/initializers/.+\.rb$})
64
+ watch('Gemfile')
65
+ watch('Gemfile.lock')
66
+ watch('spec/spec_helper.rb') { :rspec }
67
+ watch('test/test_helper.rb') { :test_unit }
68
+ watch(%r{features/support/}) { :cucumber }
69
+ end
data/README.md CHANGED
@@ -1,6 +1,4 @@
1
- # WithRetries
2
-
3
- TODO: Write a gem description
1
+ # with_retries
4
2
 
5
3
  ## Installation
6
4
 
@@ -18,7 +16,23 @@ Or install it yourself as:
18
16
 
19
17
  ## Usage
20
18
 
21
- TODO: Write usage instructions here
19
+ ```ruby
20
+ # example.rb
21
+ require 'with_retries'
22
+
23
+ Boom = Class.new(RuntimeError)
24
+
25
+ with_retries(Boom, attempts: 3) do
26
+ puts "Here I am"
27
+ raise Boom.new("BOOM!")
28
+ end
29
+
30
+ #=> $ ruby example.rb
31
+ #=> Here I am
32
+ #=> Here I am
33
+ #=> Here I am
34
+ #=> example.rb:8:in `block in <main>': BOOM! (Boom)
35
+ ```
22
36
 
23
37
  ## Contributing
24
38
 
@@ -1,3 +1,3 @@
1
1
  module WithRetries
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/with_retries.rb CHANGED
@@ -4,11 +4,23 @@ module Kernel
4
4
  def with_retries(errors, params = {}, &block)
5
5
  attempts = params[:attempts] or
6
6
  raise ArgumentError.new("Attempts parameter not provided")
7
+ logger = params[:logger]
7
8
 
8
9
  begin
9
10
  yield
10
- rescue *errors
11
+ rescue *errors => e
11
12
  attempts -= 1
13
+
14
+ if logger
15
+ times = (attempts == 1 ? "time" : "times")
16
+
17
+ logger.warn(
18
+ "Attempt failed. Retrying #{attempts} more #{times}...\n" +
19
+ ["#{e.class}: #{e.message}:", *e.backtrace].join("\n ") +
20
+ "\n"
21
+ )
22
+ end
23
+
12
24
  attempts > 0 ? retry : raise
13
25
  end
14
26
  end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'logger'
5
+ require 'with_retries'
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Kernel do
4
+ describe "#with_retries" do
5
+ Boom = Class.new(RuntimeError)
6
+
7
+ context "when given valid parameters" do
8
+ it "rescues the given error" do
9
+ expect do
10
+ attempt = 0
11
+ with_retries(Boom, attempts: 3) do
12
+ attempts += 1
13
+ raise Boom.new if attempt < 3
14
+ end
15
+ end.to_not raise_error(Boom)
16
+ end
17
+
18
+ it "retries the block the given number of times" do
19
+ Kernel.should_receive(:raise).exactly(3).times
20
+
21
+ attempts = 0
22
+ with_retries(Boom, attempts: 3) do
23
+ attempts += 1
24
+ raise Boom.new if attempts < 3
25
+ end
26
+ attempts.should == 3
27
+ end
28
+
29
+ it "raises the exception when the number of retries has been exceeded" do
30
+ expect do
31
+ with_retries(Boom, attempts: 2) { 3.times { raise Boom.new } }
32
+ end.to raise_error(Boom)
33
+ end
34
+
35
+ it "does not retry unexpected exceptions" do
36
+ unexpected_error = Class.new(RuntimeError)
37
+ expect do
38
+ with_retries(Boom, attempts: 2) { raise unexpected_error.new }
39
+ end.to raise_error(unexpected_error)
40
+ end
41
+
42
+ context "when given a logger parameter" do
43
+ let(:logger) { Logger.new(STDOUT) }
44
+
45
+ it "logs a warning to the given logger" do
46
+ logger.should_receive(:warn).exactly(5).times
47
+
48
+ expect do
49
+ with_retries(Boom, attempts: 5, logger: logger) do
50
+ raise Boom.new
51
+ end
52
+ end.to raise_error
53
+ end
54
+ end
55
+ end
56
+
57
+ context "when not given an attempts parameter" do
58
+ it "raises an ArgumentError" do
59
+ expect do
60
+ with_retries(Boom) { raise Boom.new }
61
+ end.to raise_error(ArgumentError, "Attempts parameter not provided")
62
+ end
63
+ end
64
+ end
65
+ end
data/with_retries.gemspec CHANGED
@@ -16,4 +16,5 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
+ gem.add_development_dependency 'rspec', '~> 2.14.1'
19
20
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: with_retries
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Bryan Woods
@@ -11,8 +10,22 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2013-02-25 00:00:00.000000000 Z
15
- dependencies: []
13
+ date: 2013-10-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.14.1
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: 2.14.1
16
29
  description: Adds a with_retries method to Kernel for retrying things
17
30
  email:
18
31
  - dev@howaboutwe.com
@@ -22,35 +35,39 @@ extra_rdoc_files: []
22
35
  files:
23
36
  - .gitignore
24
37
  - Gemfile
38
+ - Guardfile
25
39
  - LICENSE.txt
26
40
  - README.md
27
41
  - Rakefile
28
42
  - lib/with_retries.rb
29
43
  - lib/with_retries/version.rb
44
+ - spec/spec_helper.rb
45
+ - spec/with_retries_spec.rb
30
46
  - with_retries.gemspec
31
47
  homepage: ''
32
48
  licenses: []
49
+ metadata: {}
33
50
  post_install_message:
34
51
  rdoc_options: []
35
52
  require_paths:
36
53
  - lib
37
54
  required_ruby_version: !ruby/object:Gem::Requirement
38
- none: false
39
55
  requirements:
40
- - - ! '>='
56
+ - - '>='
41
57
  - !ruby/object:Gem::Version
42
58
  version: '0'
43
59
  required_rubygems_version: !ruby/object:Gem::Requirement
44
- none: false
45
60
  requirements:
46
- - - ! '>='
61
+ - - '>='
47
62
  - !ruby/object:Gem::Version
48
63
  version: '0'
49
64
  requirements: []
50
65
  rubyforge_project:
51
- rubygems_version: 1.8.23
66
+ rubygems_version: 2.0.3
52
67
  signing_key:
53
- specification_version: 3
68
+ specification_version: 4
54
69
  summary: Maybe later
55
- test_files: []
70
+ test_files:
71
+ - spec/spec_helper.rb
72
+ - spec/with_retries_spec.rb
56
73
  has_rdoc: