tryout 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ coverage
2
+ doc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.3
6
+ - 2.0.0
7
+
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+
5
+ group :development, :test do
6
+ gem 'simplecov'
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tryout (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activesupport (3.2.9)
10
+ i18n (~> 0.6)
11
+ multi_json (~> 1.0)
12
+ diff-lcs (1.1.3)
13
+ i18n (0.6.1)
14
+ multi_json (1.5.0)
15
+ redcarpet (2.2.2)
16
+ rspec (2.12.0)
17
+ rspec-core (~> 2.12.0)
18
+ rspec-expectations (~> 2.12.0)
19
+ rspec-mocks (~> 2.12.0)
20
+ rspec-core (2.12.1)
21
+ rspec-expectations (2.12.0)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.12.0)
24
+ simplecov (0.7.1)
25
+ multi_json (~> 1.0)
26
+ simplecov-html (~> 0.7.1)
27
+ simplecov-html (0.7.1)
28
+ yard (0.8.3)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ activesupport (>= 3.0.0)
35
+ redcarpet (~> 2.2.2)
36
+ rspec (>= 2.0.0)
37
+ simplecov
38
+ tryout!
39
+ yard (~> 0.8.3)
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2012 Endel Dreyer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ Tryout [![Build Status](https://secure.travis-ci.org/endel/tryout.png)](http://travis-ci.org/endel/tryout)
2
+ ===
3
+
4
+ Allows you to do dirty stuff without messing up your code base.
5
+
6
+ Background
7
+ ---
8
+
9
+ I've found myself using begin/rescue/retry through HTTP requests frequently, due
10
+ timeout, weird stuff caused by a messed up third-party service, or even due
11
+ internet connectivity lost.
12
+
13
+
14
+ How to use
15
+ ---
16
+
17
+ Currently, Tryout supports 3 alternatives of conditional logic.
18
+
19
+ **if/unless**: call any method on the result of the `try` block.
20
+
21
+ value = Tryout.try { RestClient.get('http://www.google.com') }.retry(3, :if => :empty?)
22
+ value = Tryout.try { RestClient.get('http://www.google.com') }.retry(3, :unless => :present?)
23
+
24
+ **block**: the result of the `try` block will be passed as argument for the second one.
25
+ Is up to you when the result is considered invalid.
26
+
27
+ value = Tryout.try { RestClient.get('http://www.google.com') }.retry(3) do |invalid|
28
+ # Invalidate when response have length lesser than 100
29
+ invalid.length < 100
30
+ end
31
+
32
+ License
33
+ ---
34
+
35
+ Tryout is released under the MIT license. Please read the LICENSE file.
@@ -0,0 +1,5 @@
1
+ class Tryout
2
+ # Gem Version
3
+ VERSION = '0.1.0'
4
+ end
5
+
data/lib/tryout.rb ADDED
@@ -0,0 +1,73 @@
1
+ #
2
+ # Allows you to do dirty stuff without messing up your code base.
3
+ #
4
+ class Tryout
5
+ # Initialize the Tryout instance.
6
+ #
7
+ # @example
8
+ # value = Tryout.try { RestClient.get('http://www.google.com') }.retry(3, :if => :empty?)
9
+ #
10
+ # @param [block] block content retrieval logic
11
+ # @return [Tryout] tryout object
12
+ def self.try &block
13
+ self.new(&block)
14
+ end
15
+
16
+ autoload :VERSION, 'tryout/version'
17
+
18
+ # Initializes Tryout with the content retrieval logic.
19
+ # @param [block] block content retrieval logic
20
+ def initialize(&block)
21
+ @attempts = 0
22
+ @block = block
23
+ end
24
+
25
+ # Retry the block call
26
+ #
27
+ # @param [Integer] times (default: 2)
28
+ # @param [Hash] conditions
29
+ # @option [Symbol] if method to call from result
30
+ # @option [Symbol] unless method to call from result
31
+ #
32
+ # @return [Object] result from block call
33
+ def retry(times = 2, conditions = nil, &evaluator)
34
+ result = @block.call
35
+
36
+ # Call block to evaluate valid result
37
+ if block_given?
38
+ conditions = { :match => evaluator.call(result) }
39
+ end
40
+
41
+ # Throw a exception to be rescued, if retry count reached the limit.
42
+ if apply_conditions?(result, conditions)
43
+ throw Exception.new("#{result.inspect} doesn't match conditions. Already retried #{times.inspect} times.")
44
+ end
45
+
46
+ result
47
+ rescue Exception => e
48
+ @attempts += 1
49
+ if @attempts < times
50
+ retry
51
+ else
52
+ raise e
53
+ end
54
+ end
55
+
56
+ protected
57
+
58
+ # Apply conditions to check a valid result
59
+ # @param [Object] result
60
+ # @param [Hash] conditions
61
+ # @return [Boolean] need a retry?
62
+ def apply_conditions?(result, conditions)
63
+ negation = conditions.has_key?(:unless)
64
+ bool = if conditions.has_key?(:match)
65
+ conditions[:match]
66
+ else
67
+ result.send(conditions[:if] || conditions[:unless])
68
+ end
69
+
70
+ (negation) ? !bool : bool
71
+ end
72
+ end
73
+
@@ -0,0 +1,9 @@
1
+ $: << 'lib'
2
+
3
+ require 'bundler/setup'
4
+ require 'simplecov'
5
+ SimpleCov.start
6
+
7
+ require 'rspec'
8
+ require 'tryout'
9
+ require 'active_support/core_ext'
@@ -0,0 +1,45 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe Tryout do
4
+ it "should allow to access version" do
5
+ Tryout::VERSION.should be_a(String)
6
+ end
7
+
8
+ it "should retry 3 times" do
9
+ sample = [1,2,3].each
10
+ valid = Tryout.try do
11
+ sample.next
12
+ end.retry(3) do |invalid|
13
+ invalid < 3
14
+ end
15
+ valid.should == 3
16
+ end
17
+
18
+ it "should raise error when retried more than allowed" do
19
+ sample = [1,2,3,4,5,6].each
20
+ expect do
21
+ valid = Tryout.try do
22
+ sample.next
23
+ end.retry(5) do |invalid|
24
+ invalid < 6
25
+ end
26
+ end.to raise_exception
27
+ end
28
+
29
+ it "allow :if option" do
30
+ emptyness = ["", "", "", "hey!"].each
31
+ valid = Tryout.try do
32
+ emptyness.next
33
+ end.retry(5, :if => :empty?)
34
+ valid.should == "hey!"
35
+ end
36
+
37
+ it "allow :unless option" do
38
+ presence = ["", "", "", "hey!"].each
39
+ valid = Tryout.try do
40
+ presence.next
41
+ end.retry(5, :unless => :present?)
42
+ valid.should == "hey!"
43
+ end
44
+ end
45
+
data/tryout.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'tryout/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "tryout"
6
+ s.version = Tryout::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Endel Dreyer"]
9
+ s.email = ["endel.dreyer@gmail.com"]
10
+ s.homepage = "http://github.com/endel/tryout"
11
+
12
+ s.summary = "Clean begin/rescue/retry utility."
13
+ s.description = "Allows you to do dirty stuff without messing up your code base."
14
+ s.licenses = ['MIT']
15
+
16
+ s.add_development_dependency "activesupport", ">= 3.0.0"
17
+ s.add_development_dependency "rspec", ">= 2.0.0"
18
+ s.add_development_dependency "yard", "~> 0.8.3"
19
+ s.add_development_dependency "redcarpet", "~> 2.2.2"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
23
+ s.require_paths = ["lib"]
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tryout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Endel Dreyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.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: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.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: 2.0.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: yard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.3
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.8.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: redcarpet
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.2.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: 2.2.2
78
+ description: Allows you to do dirty stuff without messing up your code base.
79
+ email:
80
+ - endel.dreyer@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - .travis.yml
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE
91
+ - README.md
92
+ - lib/tryout.rb
93
+ - lib/tryout/version.rb
94
+ - spec/spec_helper.rb
95
+ - spec/tryout_spec.rb
96
+ - tryout.gemspec
97
+ homepage: http://github.com/endel/tryout
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Clean begin/rescue/retry utility.
122
+ test_files: []
123
+ has_rdoc: