retrying 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rspec'
7
+ gem 'pry'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tomasz Szymczyszyn
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,67 @@
1
+ # Retrying
2
+
3
+ Runs a code block, and retries it when an exception occurs.
4
+
5
+ It's configured using optional parameters `:tries` and `:on`.
6
+
7
+ Should the number of retries be reached without success, the last exception
8
+ will be raised.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ``` bash
15
+ gem 'retrying'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ ``` bash
21
+ $ bundle
22
+ ```
23
+
24
+ Or install it yourself as:
25
+
26
+ ``` bash
27
+ $ gem install retrying
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ Open an URL, try up to three times retrying when an `OpenURI::HTTPError` occurs.
33
+
34
+ ``` ruby
35
+ require "retrying"
36
+ require "open-uri"
37
+
38
+ include Retrying
39
+
40
+ retrying(:tries => 3, :on => OpenURI::HTTPError) do
41
+ xml = open("http://example.com/test.xml").read
42
+ end
43
+ ```
44
+
45
+ Do _something_, try up to five times for either `ArgumentError` or
46
+ `TimeoutError` exceptions.
47
+
48
+ ``` ruby
49
+ retrying(:tries => 5, :on => [ArgumentError, TimeoutError]) do
50
+ # some crazy code
51
+ end
52
+ ```
53
+
54
+ ## Alternatives
55
+
56
+ * [nfedyashev/retryable](https://github.com/nfedyashev/retryable)
57
+ * [robertsosinski/retryable](https://github.com/robertsosinski/retryable)
58
+
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
67
+
data/lib/retrying.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'retrying/version'
2
+
3
+ module Retrying
4
+ def retrying(options = {}, &block)
5
+ options = { :on => StandardError, :tries => 2 }.merge(options)
6
+ exceptions = Array(options[:on])
7
+
8
+ attempts = 0
9
+ begin
10
+ yield
11
+ rescue *exceptions => ex
12
+ attempts += 1
13
+ raise if attempts >= options[:tries]
14
+ retry
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Retrying
2
+ VERSION = "0.0.1"
3
+ end
data/retrying.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'retrying/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "retrying"
8
+ spec.version = Retrying::VERSION
9
+ spec.authors = ["Tomasz Szymczyszyn"]
10
+ spec.email = ["tomasz.szymczyszyn@gmail.com"]
11
+ spec.description = %q{Easily retry block of code in case of an exception}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/kammerer/retrying"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,58 @@
1
+ require 'retrying'
2
+
3
+ include Retrying
4
+
5
+ describe Retrying do
6
+ describe '#retrying' do
7
+ it 'retries a block once with no args' do
8
+ tries = 0
9
+ raised = false
10
+
11
+ begin
12
+ retrying do
13
+ tries += 1
14
+ raise FloatDomainError
15
+ end
16
+ rescue FloatDomainError
17
+ raised = true
18
+ end
19
+
20
+ expect(tries).to eq(2)
21
+ expect(raised).to be_true
22
+ end
23
+
24
+ it 'retries a block number of times' do
25
+ tries = 0
26
+ raised = false
27
+
28
+ begin
29
+ retrying(:tries => 3) do
30
+ tries += 1
31
+ raise StandardError
32
+ end
33
+ rescue StandardError
34
+ raised = true
35
+ end
36
+
37
+ expect(tries).to eq(3)
38
+ expect(raised).to be_true
39
+ end
40
+
41
+ it 'retries specific exceptions' do
42
+ tries = 0
43
+ raised = false
44
+
45
+ begin
46
+ retrying(:on => IOError) do
47
+ tries += 1
48
+ raise FloatDomainError
49
+ end
50
+ rescue FloatDomainError
51
+ raised = true
52
+ end
53
+
54
+ expect(tries).to eq(1)
55
+ expect(raised).to be_true
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: retrying
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tomasz Szymczyszyn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
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: '1.3'
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: '0'
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: '0'
46
+ description: Easily retry block of code in case of an exception
47
+ email:
48
+ - tomasz.szymczyszyn@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - lib/retrying.rb
58
+ - lib/retrying/version.rb
59
+ - retrying.gemspec
60
+ - spec/retrying_spec.rb
61
+ homepage: https://github.com/kammerer/retrying
62
+ licenses:
63
+ - MIT
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.25
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Easily retry block of code in case of an exception
86
+ test_files:
87
+ - spec/retrying_spec.rb
88
+ has_rdoc: