attempt_to 0.3
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.
- data/Gemfile +3 -0
- data/README.md +11 -0
- data/Rakefile +6 -0
- data/attempt_to.gemfile +20 -0
- data/lib/attempt_to.rb +2 -0
- data/lib/attempt_to/attempt_to.rb +21 -0
- data/lib/attempt_to/version.rb +4 -0
- data/spec/attempt_to_spec.rb +53 -0
- metadata +84 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
attempt_to
|
2
|
+
==========
|
3
|
+
|
4
|
+
Small gem for attempting to do something and returning errors if it fails.
|
5
|
+
|
6
|
+
Usage:
|
7
|
+
|
8
|
+
# Try to connect 3 times to the network, otherwise exit.
|
9
|
+
attempt_to('connect to the network', 3) {
|
10
|
+
TCPSocket.new("some.very.weird.domain.name", 9342)
|
11
|
+
}
|
data/Rakefile
ADDED
data/attempt_to.gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path('../lib/attempt_to/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'attempt_to'
|
6
|
+
gem.version = AttemptTo::VERSION
|
7
|
+
gem.date = Date.today.to_s
|
8
|
+
|
9
|
+
gem.summary = "AttemptTo is a small utility for attempting a code block multiple times"
|
10
|
+
gem.description = "AttemptTo calls a code block and re-tries it if it throws an exception. Otherwise exits"
|
11
|
+
|
12
|
+
gem.authors = ['Bas Vodde']
|
13
|
+
gem.email = 'basv@odd-e.com'
|
14
|
+
gem.homepage = 'https://github.com/basvodde/attempt_to'
|
15
|
+
|
16
|
+
gem.add_dependency('rake')
|
17
|
+
gem.add_development_dependency('rspec', [">= 2.0.0"])
|
18
|
+
|
19
|
+
gem.files = `git ls-files -- {.,test,spec,lib}/*`.split("\n")
|
20
|
+
end
|
data/lib/attempt_to.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module AttemptTo
|
4
|
+
def self.attempt_to(something, amount)
|
5
|
+
amount.times { |times|
|
6
|
+
begin
|
7
|
+
return yield
|
8
|
+
rescue Exception => ex
|
9
|
+
Kernel.puts "Failed attempt ##{times+1} to #{something}. Error message: #{ex.message}"
|
10
|
+
if times+1 == amount
|
11
|
+
Kernel.puts "Attempted #{amount} times to #{something}. Giving up..."
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
end
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def attempt_to(something, amount, &code_block)
|
20
|
+
AttemptTo.attempt_to(something, amount, &code_block)
|
21
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
|
2
|
+
require 'attempt_to'
|
3
|
+
require 'socket'
|
4
|
+
|
5
|
+
describe "Attempt to do something a couple of times, if it fails, bail out." do
|
6
|
+
|
7
|
+
before(:each) {
|
8
|
+
@attempts = 0
|
9
|
+
}
|
10
|
+
|
11
|
+
it "calls the block only once when it succeeds" do
|
12
|
+
attempt_to("do this", 3) {
|
13
|
+
@attempts += 1
|
14
|
+
}
|
15
|
+
@attempts.should == 1
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns whatever the code block returns" do
|
19
|
+
attempt_to("do this", 2) {
|
20
|
+
"returns"
|
21
|
+
}.should == "returns"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "calls the block multiple times when it fails " do
|
25
|
+
Kernel.should_receive(:puts).with("Failed attempt #1 to do something. Error message: wrong 1")
|
26
|
+
attempt_to("do something", 3) {
|
27
|
+
@attempts += 1
|
28
|
+
raise(Exception, "wrong #{@attempts}") if @attempts == 1
|
29
|
+
}
|
30
|
+
@attempts.should == 2
|
31
|
+
end
|
32
|
+
|
33
|
+
it "bails out aften the maximum amount of attempts" do
|
34
|
+
Kernel.should_receive(:puts).exactly(3).times
|
35
|
+
Kernel.should_receive(:puts).with("Attempted 3 times to do this. Giving up...")
|
36
|
+
lambda {
|
37
|
+
attempt_to("do this", 3) {
|
38
|
+
@attempts += 1
|
39
|
+
raise(Exception, "wrong!")
|
40
|
+
}
|
41
|
+
}.should raise_error(SystemExit)
|
42
|
+
|
43
|
+
@attempts.should == 3
|
44
|
+
end
|
45
|
+
|
46
|
+
# it "Can run the example code" do
|
47
|
+
# attempt_to('connect to the network', 3) {
|
48
|
+
# TCPSocket.new("some.very.weird.domain.name", 9342)
|
49
|
+
# }
|
50
|
+
#
|
51
|
+
# end
|
52
|
+
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attempt_to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "0.3"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bas Vodde
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-12-03 00:00:00 +08:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rake
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 2.0.0
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
description: AttemptTo calls a code block and re-tries it if it throws an exception. Otherwise exits
|
39
|
+
email: basv@odd-e.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- Gemfile
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- attempt_to.gemfile
|
51
|
+
- lib/attempt_to.rb
|
52
|
+
- lib/attempt_to/attempt_to.rb
|
53
|
+
- lib/attempt_to/version.rb
|
54
|
+
- spec/attempt_to_spec.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: https://github.com/basvodde/attempt_to
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.6.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: AttemptTo is a small utility for attempting a code block multiple times
|
83
|
+
test_files: []
|
84
|
+
|