waiter_ 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd0325695fdeaf22e3e8d68315874f9f4dbaeb83
4
+ data.tar.gz: 16016830d0702876aa84c58755dfcb415b2384a7
5
+ SHA512:
6
+ metadata.gz: e44261b1a34d9e14053a2dc6b557f00d4fe75a9ac416073c7faf3494b59c1ac2353c975a9e9043e13e8b0bcc68b9959d927e58b161ae2d6fca37f973f9f74880
7
+ data.tar.gz: 42c81f41bf86dcbb66d4f29f4a181f4fa5c01192b6dd8a6f45f229ff9abf3dcd4e1e8189b888df8280a03141e34b5e0161a9190c3bf6b0d8bc0a484d95106f70
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ *.gem
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.iml
12
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Waiter
2
+
3
+ A simple wait/polling gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'waiter', git: 'git@github.atl.pdrop.net:dmcneil/waiter.git'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ ## Usage
18
+
19
+ Just `include Waiter` and the following APIs will be available.
20
+
21
+ ```ruby
22
+ wait('foo').to eq 'foo' # Will pass.
23
+ wait('foo').to eq 'bar' # Will throw exception.
24
+ wait('foo').to_not eq 'bar' # Will pass.
25
+ wait('foo').to_not eq 'foo' # Will fail.
26
+ ```
27
+
28
+ To adjust the timeout/polling, simple chain methods are supported.
29
+
30
+ The *for* method accepts an Integer and adjusts the timeout. Use the *every* method with an Integer to adjust the polling time.
31
+
32
+ ```ruby
33
+ # Wait for 30 seconds, polling every 2 second.
34
+ wait('foo').every(2).for(30).to eq 'foo'
35
+ ```
36
+
37
+ You do *not* have to use both methods, you can adjust one or the other. They can also be used in any order.
38
+
39
+ ```ruby
40
+ # Wait for 30 seconds, using the default 1 second poll.
41
+ wait('foo').for(30).to eq 'foo'
42
+
43
+ # Wait for the default 15 seconds, polling every 5 seconds.
44
+ wait('foo').every(5).to eq 'foo'
45
+ ```
46
+
47
+ You can also pass a block to be evaluated, using *until*, until it is true.
48
+
49
+ ```ruby
50
+ wait.until { true == true }
51
+
52
+ wait.every(5).for(30).until {
53
+ true == true
54
+ }
55
+ ```
56
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Waiter
2
+ VERSION = '0.0.1'
3
+ end
data/lib/waiter.rb ADDED
@@ -0,0 +1,131 @@
1
+ require 'rspec/expectations'
2
+
3
+ module Waiter
4
+ # Wrapper for RSpec expectations that allows waiting/polling.
5
+ #
6
+ # The default timeout is 15 seconds, polling every 1 second.
7
+ #
8
+ # @example
9
+ #
10
+ # wait('foo').to eq 'foo' # Will pass.
11
+ # wait('foo').to eq 'bar' # Will throw exception.
12
+ # wait('foo').to_not eq 'bar' # Will pass.
13
+ # wait('foo').to_not eq 'foo' # Will fail.
14
+ #
15
+ # To adjust the timeout/polling, simple chain methods are supported.
16
+ #
17
+ # The *for* method accepts an Integer and adjusts the timeout.
18
+ # Use the *every* method with an Integer to adjust the polling time.
19
+ #
20
+ # @example
21
+ #
22
+ # # Wait for 30 seconds, polling every 2 second.
23
+ # wait('foo').every(2).for(30).to eq 'foo'
24
+ #
25
+ # You do *not* have to use both methods, you can adjust one or the other.
26
+ # They can also be used in any order.
27
+ #
28
+ # @example
29
+ #
30
+ # # Wait for 30 seconds, using the default 1 second poll.
31
+ # wait('foo').for(30).to eq 'foo'
32
+ #
33
+ # # Wait for the default 15 seconds, polling every 5 seconds.
34
+ # wait('foo').every(5).to eq 'foo'
35
+ #
36
+ # You can also pass a block to be evaluated, using *until*, until it is true.
37
+ #
38
+ # @example
39
+ #
40
+ # wait.until { true == true }
41
+ #
42
+ # wait.every(5).for(30).until {
43
+ # true == true
44
+ # }
45
+ #
46
+ # @author Derek McNeil <dmcneil@pindrop.com>
47
+ def wait(value=true, &block)
48
+ WaitExpectationTarget.for(value, block)
49
+ end
50
+
51
+ alias_method :wait_for, :wait
52
+
53
+ class WaitExpectationTarget < RSpec::Expectations::ExpectationTarget
54
+ attr_reader :target
55
+ attr_accessor :timeout, :polling, :message
56
+
57
+ DEFAULT_TIMEOUT = 15
58
+ DEFAULT_POLLING = 1
59
+
60
+ def initialize(value)
61
+ @target = value
62
+ @timeout = DEFAULT_TIMEOUT
63
+ @polling = DEFAULT_POLLING
64
+ end
65
+
66
+ def wait(message=nil, &block)
67
+ unless @timeout > @polling
68
+ raise ArgumentError, 'Timeout must be a higher value than polling.'
69
+ end
70
+
71
+ @message = message || default_failure_message
72
+
73
+ while @timeout > 0 && @timeout > @polling
74
+ @result = begin
75
+ block.call
76
+ rescue Exception => error
77
+ @error = error
78
+ false
79
+ end
80
+
81
+ break if @result
82
+ sleep @polling
83
+ @timeout = @timeout - @polling
84
+ end
85
+
86
+ unless @result
87
+ if @error
88
+ @error.message.prepend @message
89
+ else
90
+ @error = RuntimeError.new @message
91
+ end
92
+
93
+ raise @error
94
+ end
95
+ end
96
+
97
+ alias_method :until, :wait
98
+
99
+ def every(seconds)
100
+ @polling = seconds
101
+ self
102
+ end
103
+
104
+ def for(seconds)
105
+ @timeout = seconds
106
+ self
107
+ end
108
+
109
+ def to(matcher=nil, message=nil, &block)
110
+ prevent_operator_matchers(:to) unless matcher
111
+ wait(message) do
112
+ RSpec::Expectations::PositiveExpectationHandler.handle_matcher(target, matcher, message, &block)
113
+ end
114
+ @target
115
+ end
116
+
117
+ def not_to(matcher=nil, message=nil, &block)
118
+ prevent_operator_matchers(:not_to) unless matcher
119
+ wait(message) do
120
+ RSpec::Expectations::NegativeExpectationHandler.handle_matcher(target, matcher, message, &block)
121
+ end
122
+ @target
123
+ end
124
+
125
+ alias_method :to_not, :not_to
126
+
127
+ def default_failure_message
128
+ "Timed out after waiting for #{@timeout} seconds, polling every #{@polling} second.\n"
129
+ end
130
+ end
131
+ end
data/waiter.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require_relative 'lib/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'waiter_'
8
+ spec.version = Waiter::VERSION
9
+ spec.authors = ['Derek McNeil']
10
+ spec.email = ['dmcneil@pindropsecurity.com']
11
+
12
+ spec.summary = 'A simple polling gem.'
13
+ spec.homepage = 'https://github.atl.pdrop.net/dmcneil/waiter'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_development_dependency 'bundler', '~> 1.12'
19
+ spec.add_development_dependency 'rake', '~> 10.0'
20
+ spec.add_development_dependency 'rspec', '~> 3.0'
21
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: waiter_
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Derek McNeil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - dmcneil@pindropsecurity.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - lib/version.rb
68
+ - lib/waiter.rb
69
+ - waiter.gemspec
70
+ homepage: https://github.atl.pdrop.net/dmcneil/waiter
71
+ licenses: []
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.4.5.1
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A simple polling gem.
93
+ test_files: []