tries 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.travis.yml +10 -0
- data/Gemfile +3 -0
- data/Guardfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +108 -0
- data/Rakefile +5 -0
- data/lib/tries/version.rb +3 -0
- data/lib/tries.rb +16 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/tries/tries_spec.rb +81 -0
- data/tries.gemspec +28 -0
- metadata +139 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
guard 'rspec', cli: '--drb --profile', all_after_pass: false do
|
2
|
+
# Specs
|
3
|
+
watch(%r(^spec/.+_spec\.rb$))
|
4
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
5
|
+
watch(%r(^spec/support/(.+)\.rb$)) { 'spec' }
|
6
|
+
|
7
|
+
# Files
|
8
|
+
watch(%r(^lib/(.+)\.rb$)) { |m| "spec/#{m[1]}_spec.rb" }
|
9
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 kraut computing UG (haftungsbeschränkt)
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# Tries
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/krautcomputing/tries.png)](http://travis-ci.org/krautcomputing/tries)
|
4
|
+
[![Dependency Status](https://gemnasium.com/krautcomputing/tries.png)](https://gemnasium.com/krautcomputing/tries)
|
5
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/krautcomputing/tries)
|
6
|
+
|
7
|
+
Solidify your code and retry on petty exceptions
|
8
|
+
|
9
|
+
## Requirements
|
10
|
+
|
11
|
+
Requires Ruby 1.9.2 or higher
|
12
|
+
|
13
|
+
## Is it production ready?
|
14
|
+
|
15
|
+
Yes! I have been using this code in numerous applications for several years.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
gem 'tries'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install tries
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
3.tries on: Timeout::Error do
|
35
|
+
Mechanize.new.get 'https://www.google.com/'
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
## Detailed usage
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# Helper code to explain how it works
|
43
|
+
|
44
|
+
FooError = Class.new(StandardError)
|
45
|
+
BarError = Class.new(StandardError)
|
46
|
+
|
47
|
+
@counter = 0
|
48
|
+
|
49
|
+
def method_that_raises_exception
|
50
|
+
@counter += 1
|
51
|
+
puts "Counter is #{@counter}"
|
52
|
+
|
53
|
+
case @counter
|
54
|
+
when 1 then raise FooError
|
55
|
+
when 2 then raise FooError
|
56
|
+
when 3 then raise BarError
|
57
|
+
when 4 then raise StandardError
|
58
|
+
end
|
59
|
+
|
60
|
+
puts 'You made it through!'
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# Rescue all errors
|
66
|
+
|
67
|
+
4.tries do
|
68
|
+
method_that_raises_exception
|
69
|
+
end
|
70
|
+
|
71
|
+
> Counter is 1
|
72
|
+
> Counter is 2
|
73
|
+
> Counter is 3
|
74
|
+
> Counter is 4
|
75
|
+
> Counter is 5
|
76
|
+
> You made it through!
|
77
|
+
```
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
# Rescue only certain errors
|
81
|
+
|
82
|
+
3.tries on: FooError do
|
83
|
+
method_that_raises_exception
|
84
|
+
end
|
85
|
+
|
86
|
+
> Counter is 1
|
87
|
+
> Counter is 2
|
88
|
+
> Counter is 3
|
89
|
+
> BarError: BarError
|
90
|
+
|
91
|
+
3.tries on: [FooError, BarError] do
|
92
|
+
method_that_raises_exception
|
93
|
+
end
|
94
|
+
|
95
|
+
> Counter is 1
|
96
|
+
> Counter is 2
|
97
|
+
> Counter is 3
|
98
|
+
> Counter is 4
|
99
|
+
> StandardError: StandardError
|
100
|
+
```
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
1. Fork it
|
105
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
106
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
107
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
108
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/tries.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'tries/version'
|
2
|
+
|
3
|
+
class Integer
|
4
|
+
def tries(options = {}, &block)
|
5
|
+
attempts = self
|
6
|
+
exception_classes = Array(options[:on] || StandardError)
|
7
|
+
|
8
|
+
begin
|
9
|
+
return yield
|
10
|
+
rescue *exception_classes
|
11
|
+
retry if (attempts -= 1) > 0
|
12
|
+
end
|
13
|
+
|
14
|
+
yield
|
15
|
+
end
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Tries do
|
4
|
+
before do
|
5
|
+
@counter = 0
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'when retrying on all errors' do
|
9
|
+
it 'retries at least the defined number of times' do
|
10
|
+
expect do
|
11
|
+
5.tries do
|
12
|
+
raise_foo_foo_bar_bar_standard
|
13
|
+
end
|
14
|
+
end.to_not raise_error
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'retries max the defined number of times' do
|
18
|
+
expect do
|
19
|
+
3.tries do
|
20
|
+
raise_foo_foo_bar_bar_standard
|
21
|
+
end
|
22
|
+
end.to raise_error(BarError)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when retrying only on one error' do
|
27
|
+
it 'retries at least the defined number of times' do
|
28
|
+
expect do
|
29
|
+
2.tries on: FooError do
|
30
|
+
raise_foo_foo_bar_bar_standard
|
31
|
+
end
|
32
|
+
end.to raise_error(BarError)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'retries max the defined number of times' do
|
36
|
+
expect do
|
37
|
+
1.tries on: FooError do
|
38
|
+
raise_foo_foo_bar_bar_standard
|
39
|
+
end
|
40
|
+
end.to raise_error(FooError)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when retrying on multiple errors' do
|
45
|
+
it 'retries at least the defined number of times' do
|
46
|
+
expect do
|
47
|
+
5.tries on: [FooError, BarError] do
|
48
|
+
raise_foo_foo_bar_bar_standard
|
49
|
+
end
|
50
|
+
end.to raise_error(StandardError)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'retries max the defined number of times' do
|
54
|
+
expect do
|
55
|
+
3.tries on: [FooError, BarError] do
|
56
|
+
raise_foo_foo_bar_bar_standard
|
57
|
+
end
|
58
|
+
end.to raise_error(BarError)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
FooError = Class.new(StandardError)
|
64
|
+
BarError = Class.new(StandardError)
|
65
|
+
|
66
|
+
def raise_foo_foo_bar_bar_standard
|
67
|
+
@counter += 1
|
68
|
+
|
69
|
+
case @counter
|
70
|
+
when 1
|
71
|
+
raise FooError
|
72
|
+
when 2
|
73
|
+
raise FooError
|
74
|
+
when 3
|
75
|
+
raise BarError
|
76
|
+
when 4
|
77
|
+
raise BarError
|
78
|
+
when 5
|
79
|
+
raise StandardError
|
80
|
+
end
|
81
|
+
end
|
data/tries.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'tries/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |gem|
|
9
|
+
gem.name = 'tries'
|
10
|
+
gem.version = Tries::VERSION
|
11
|
+
gem.platform = Gem::Platform::RUBY
|
12
|
+
gem.authors = ['Manuel Meurer']
|
13
|
+
gem.email = 'manuel.meurer@gmail.com'
|
14
|
+
gem.summary = 'Solidify your code and retry on petty exceptions'
|
15
|
+
gem.description = 'Retry your code multiple times on exceptions that might go away: 3.times on: Timeout::Error { call_api }'
|
16
|
+
gem.homepage = 'https://github.com/krautcomputing/tries'
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r(^bin/)).map { |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
|
21
|
+
gem.require_paths = ['lib']
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rspec', '~> 2.12.0'
|
24
|
+
gem.add_development_dependency 'rake', '~> 10.0.1'
|
25
|
+
gem.add_development_dependency 'rb-fsevent', '~> 0.9.2'
|
26
|
+
gem.add_development_dependency 'guard', '~> 1.5.2'
|
27
|
+
gem.add_development_dependency 'guard-rspec', '~> 2.1.2'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tries
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Manuel Meurer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.12.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.12.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 10.0.1
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 10.0.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rb-fsevent
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.9.2
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.2
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.5.2
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.5.2
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: guard-rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 2.1.2
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 2.1.2
|
94
|
+
description: ! 'Retry your code multiple times on exceptions that might go away: 3.times
|
95
|
+
on: Timeout::Error { call_api }'
|
96
|
+
email: manuel.meurer@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .travis.yml
|
103
|
+
- Gemfile
|
104
|
+
- Guardfile
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- lib/tries.rb
|
109
|
+
- lib/tries/version.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/tries/tries_spec.rb
|
112
|
+
- tries.gemspec
|
113
|
+
homepage: https://github.com/krautcomputing/tries
|
114
|
+
licenses: []
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 1.8.23
|
134
|
+
signing_key:
|
135
|
+
specification_version: 3
|
136
|
+
summary: Solidify your code and retry on petty exceptions
|
137
|
+
test_files:
|
138
|
+
- spec/spec_helper.rb
|
139
|
+
- spec/tries/tries_spec.rb
|