try-until 0.3.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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTgyNWMzYmMwZjI0MWQ1OGJkZjRlYjJmNjVhMmM4NjViZjNmZTI3Mw==
5
+ data.tar.gz: !binary |-
6
+ YzliYTUyOGIwOTExNmUwNjZiZGVlMTMwNmRmZjY1NDc0Y2Y5ZGMxMQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZjRkOWU5NWI5M2I3OWI4OTJlMzNlNDc3ZTM3Y2I4ZmExNDNkNjU4ZGYyN2U0
10
+ MjUxNzY0NDhiOWY1YmQxZGEyMTQ4MDFkODFkODI5ZGUwZmRmZDk5YzE2NTlh
11
+ ZjcyNjZlZGQ0NTRiYzJjNzQ5YTlmZmEzODQ0YmNiYjZhMGNmNmU=
12
+ data.tar.gz: !binary |-
13
+ ODY3MDVhMzAxNjlkZWRhMDE2NGFlNWE0ZDJhNDk4YzlmZjYxMWNkNTdmNDNi
14
+ MjgxZGI2NzU2ZjAwY2VlZTJmZTQ2Yzk4NDMxNTk0Y2YxYjRlMjdlMmYzODQ4
15
+ YWQ3MTRjNzUyZjIyMmRiYTgxOWY1ZWFjZGYyMGIwN2Y2ZWRiNGM=
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/try_until'
@@ -0,0 +1,4 @@
1
+ require 'try_until/probe'
2
+ require 'try_until/repeatedly'
3
+
4
+
@@ -0,0 +1,23 @@
1
+ module TryUntil
2
+ # This PORO holds information about a target object against which
3
+ # to call a specified method with specified arguments
4
+ class Probe
5
+
6
+ # Example: Probe.new(SomeClass.new, :some_method, [arg1, arg2, ...])
7
+ def initialize(target, method, args = [])
8
+ @target = target
9
+ @method = method
10
+ @args = args
11
+ end
12
+
13
+ def sample
14
+ @target.send(@method, *@args)
15
+ end
16
+
17
+ def to_s
18
+ "Probe: #{@target.class}##{@method}(#{@args})"
19
+ end
20
+ end
21
+ end
22
+
23
+
@@ -0,0 +1,73 @@
1
+ module TryUntil
2
+ # External interface
3
+ # Example:
4
+ # include TryUntil
5
+ # result = Repeatedly.new(Probe.new(Object.new, :to_s))
6
+ # .attempts(5)
7
+ # .interval(10)
8
+ # .rescues([ ArgumentError, IOError ])
9
+ # .stop_when(lambda { |response| JSON.parse(response.body)['id'] == 'some_id' })
10
+ # .execute
11
+ #
12
+ # Not all of the above settings are required. These are the default values:
13
+ # attempt = 3
14
+ # interval = 0
15
+ # rescues = []
16
+ # stop_when = lambda { |response| false }
17
+ #
18
+ class Repeatedly
19
+
20
+ def initialize(probe)
21
+ @probe = probe
22
+ end
23
+
24
+ def attempts(int_num)
25
+ @attempts = int_num
26
+ self
27
+ end
28
+
29
+ def interval(seconds)
30
+ @interval = seconds
31
+ self
32
+ end
33
+
34
+ def rescues(errors)
35
+ @rescues = errors
36
+ self
37
+ end
38
+
39
+ def stop_when(callable)
40
+ @stop_when = callable
41
+ self
42
+ end
43
+
44
+ # The heart of this gem: This method will repeatedly call '#sample' on the
45
+ # subject and evaluate if the expectated result is returned.
46
+ # In case of errors it will rescue those and continue, provided the type
47
+ # of error is among the ones defined in @rescues.
48
+ def execute
49
+ @attempts = 3 unless @attempts
50
+ @interval = 0 unless @interval
51
+ @stop_when = lambda { |response| false } unless @stop_when
52
+ @rescues = [] unless @rescues
53
+ count = 0
54
+ while count < @attempts
55
+ begin
56
+ result = @probe.sample
57
+ return result if @stop_when.call(result)
58
+ rescue *@rescues => exception
59
+ raise exception, "During final attempt (#{@attempts} configured) target returned #{exception}" if count + 1 == @attempts
60
+ ensure
61
+ count += 1
62
+ Kernel.sleep @interval if @interval > 0
63
+ end
64
+ end
65
+ raise "After #{@attempts} attempts, the expected result was not returned!"
66
+ end
67
+
68
+ def configuration
69
+ { :probe => @probe.to_s, :attempts => @attempts, :interval => @interval,
70
+ :rescues => @rescues, :stop_when => @stop_when }
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module TryUntil
2
+ VERSION = "0.3.0"
3
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: try-until
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Markus Krogemann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.14.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.14.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: metric_fu
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 4.4.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 4.4.1
55
+ description: The try-until library supports repeatedly calling code that may fail
56
+ or respond with values other than expected. Matchers, realized as lambdas, can be
57
+ used to specify expected result.
58
+ email:
59
+ - markus@krogemann.de
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - lib/try-until.rb
65
+ - lib/try_until/probe.rb
66
+ - lib/try_until/repeatedly.rb
67
+ - lib/try_until/version.rb
68
+ - lib/try_until.rb
69
+ homepage: https://github.com/mkrogemann/try-until
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.3
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Retry and matching logic
93
+ test_files: []