polling 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +21 -7
- data/lib/polling/confirm.rb +37 -1
- data/lib/polling/target.rb +2 -6
- data/lib/polling/version.rb +1 -1
- data/lib/polling.rb +3 -3
- data/polling.gemspec +3 -3
- data/test/helper.rb +1 -1
- data/test/test_confirm.rb +16 -2
- data/test/test_run.rb +7 -6
- data/test/test_target.rb +10 -10
- metadata +5 -5
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Polling
|
2
2
|
|
3
|
-
|
3
|
+
ruby polling library
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -8,22 +8,36 @@ Add this line to your application's Gemfile:
|
|
8
8
|
|
9
9
|
gem 'polling'
|
10
10
|
|
11
|
+
Or install it yourself as:
|
12
|
+
|
13
|
+
$ gem install polling
|
14
|
+
|
11
15
|
## Usage
|
12
16
|
|
13
|
-
|
17
|
+
It starts at per minute 0 second.
|
14
18
|
|
15
19
|
time = [0,10,20,30,40,50]
|
16
20
|
Polling::run(time,debug=false) do
|
17
|
-
|
21
|
+
puts "test"
|
22
|
+
sleep 2
|
18
23
|
end
|
19
24
|
|
20
|
-
|
25
|
+
The multiple of 60 is set up.
|
21
26
|
|
22
27
|
time = [300]
|
23
|
-
Polling::run(time
|
24
|
-
|
28
|
+
Polling::run(time) do
|
29
|
+
puts "hoge"
|
30
|
+
sleep 2
|
31
|
+
end
|
32
|
+
|
33
|
+
Please set a vlue that is divisible by 60.
|
34
|
+
|
35
|
+
time = ["5s"]
|
36
|
+
Polling::run(time) do
|
37
|
+
puts "test"
|
38
|
+
sleep 2
|
25
39
|
end
|
26
40
|
|
27
41
|
## Copyright
|
28
42
|
|
29
|
-
Copyright
|
43
|
+
Copyright (c) 2012, hiro-su All rights reserved.
|
data/lib/polling/confirm.rb
CHANGED
@@ -3,8 +3,44 @@ module Polling
|
|
3
3
|
module_function
|
4
4
|
def check_value(value)
|
5
5
|
value = value.to_i
|
6
|
-
raise TypeError, "Please set
|
6
|
+
raise TypeError, "Please set a value that is multiple of 60." if (value % 60 != 0) && value > 60
|
7
7
|
return value
|
8
8
|
end
|
9
|
+
|
10
|
+
def check_arr(arr)
|
11
|
+
if arr.count == 1
|
12
|
+
time = convert(arr[0])
|
13
|
+
arr = time < 60 ? create_arr(time) : [time]
|
14
|
+
raise TypeError, "Please set a value that is divisible by 60." if (60 % time != 0) && time < 60
|
15
|
+
end
|
16
|
+
return arr
|
17
|
+
end
|
18
|
+
|
19
|
+
def convert(value)
|
20
|
+
case value
|
21
|
+
when /(\d+)s/
|
22
|
+
value = $1.to_i
|
23
|
+
when /(\d+)m/
|
24
|
+
value = $1.to_i*60
|
25
|
+
when /(\d+)h/
|
26
|
+
value = $1.to_i*60*60
|
27
|
+
when /(\d+)d/
|
28
|
+
value = $1.to_i*60*60*24
|
29
|
+
when String
|
30
|
+
value = value.to_i
|
31
|
+
end
|
32
|
+
return value
|
33
|
+
end
|
34
|
+
|
35
|
+
def create_arr(time)
|
36
|
+
arr = Array.new
|
37
|
+
arr << 0 if time != 0
|
38
|
+
result = time
|
39
|
+
while time < 60
|
40
|
+
arr << time
|
41
|
+
time += result
|
42
|
+
end
|
43
|
+
return arr
|
44
|
+
end
|
9
45
|
end
|
10
46
|
end
|
data/lib/polling/target.rb
CHANGED
@@ -4,12 +4,7 @@ module Polling
|
|
4
4
|
module Target
|
5
5
|
module_function
|
6
6
|
|
7
|
-
|
8
|
-
#TODO imple delay
|
9
|
-
attr_accessor :delay
|
10
|
-
end
|
11
|
-
|
12
|
-
def create_time(interval,debug=false)
|
7
|
+
def interval(interval,debug=false)
|
13
8
|
init = 60
|
14
9
|
now_to_f = Time.now.to_f
|
15
10
|
|
@@ -23,5 +18,6 @@ module Polling
|
|
23
18
|
|
24
19
|
return stime
|
25
20
|
end
|
21
|
+
|
26
22
|
end
|
27
23
|
end
|
data/lib/polling/version.rb
CHANGED
data/lib/polling.rb
CHANGED
@@ -7,12 +7,12 @@ module Polling
|
|
7
7
|
module_function
|
8
8
|
def run(arr, debug=false)
|
9
9
|
start = true
|
10
|
-
|
10
|
+
arr = Confirm::check_arr(arr)
|
11
11
|
loop do
|
12
12
|
arr.each do |time|
|
13
13
|
time = Confirm::check_value(time)
|
14
|
-
Sleep::exec Target::
|
15
|
-
Sleep::exec Target::
|
14
|
+
Sleep::exec Target::interval(0, debug) if start
|
15
|
+
Sleep::exec Target::interval(time, debug) unless start
|
16
16
|
start = false
|
17
17
|
yield
|
18
18
|
end
|
data/polling.gemspec
CHANGED
@@ -6,11 +6,11 @@ require 'polling/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "polling"
|
8
8
|
gem.version = Polling::VERSION
|
9
|
-
gem.authors = ["
|
10
|
-
gem.email = ["h
|
9
|
+
gem.authors = ["hiro-su"]
|
10
|
+
gem.email = ["h.sugipon@gmail.com"]
|
11
11
|
gem.description = %q{polling}
|
12
12
|
gem.summary = %q{polling}
|
13
|
-
gem.homepage = "https://github.com/
|
13
|
+
gem.homepage = "https://github.com/hiro-su/polling"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/test/helper.rb
CHANGED
data/test/test_confirm.rb
CHANGED
@@ -5,7 +5,21 @@ class PollingTest < Test::Unit::TestCase
|
|
5
5
|
assert_equal 0, Polling::Confirm::check_value(0)
|
6
6
|
assert_equal 0, Polling::Confirm::check_value("0")
|
7
7
|
assert_equal 60, Polling::Confirm::check_value(60)
|
8
|
-
assert_equal
|
9
|
-
|
8
|
+
assert_equal 5, Polling::Confirm::check_value("5")
|
9
|
+
|
10
|
+
assert_equal 5, Polling::Confirm::convert(5)
|
11
|
+
assert_equal 5, Polling::Confirm::convert("5")
|
12
|
+
assert_equal 5, Polling::Confirm::convert("5s")
|
13
|
+
assert_equal 300, Polling::Confirm::convert("5m")
|
14
|
+
assert_equal 18000, Polling::Confirm::convert("5h")
|
15
|
+
|
16
|
+
assert_equal [0,10,20,30,40,50], Polling::Confirm::check_arr(["10s"])
|
17
|
+
assert_equal [0,10,20,30,40,50], Polling::Confirm::check_arr(["10"])
|
18
|
+
assert_equal [0,10,20,30,40,50], Polling::Confirm::check_arr([10])
|
19
|
+
assert_equal [0,30], Polling::Confirm::check_arr([30])
|
20
|
+
assert_equal [60], Polling::Confirm::check_arr([60])
|
21
|
+
assert_equal [60], Polling::Confirm::check_arr(["60"])
|
22
|
+
assert_equal [60], Polling::Confirm::check_arr(["60s"])
|
23
|
+
assert_equal [120], Polling::Confirm::check_arr(["120s"])
|
10
24
|
end
|
11
25
|
end
|
data/test/test_run.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
2
|
|
3
3
|
class PollingTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
def test_run
|
5
|
+
arr = [0,10,20,30,40,50]
|
6
|
+
Polling::run(arr,true) do
|
7
|
+
puts "test"
|
8
|
+
sleep 2
|
9
|
+
end
|
10
|
+
end
|
10
11
|
end
|
data/test/test_target.rb
CHANGED
@@ -5,22 +5,22 @@ class PollingTest < Test::Unit::TestCase
|
|
5
5
|
|
6
6
|
def test_target_time
|
7
7
|
Time.stubs(:now).returns(Time.parse "2012/12/31 23:59:59")
|
8
|
-
assert_equal 1, Polling::Target::
|
9
|
-
assert_equal 6, Polling::Target::
|
10
|
-
assert_equal 0, Polling::Target::
|
11
|
-
assert_equal 1, Polling::Target::
|
12
|
-
assert_equal 2, Polling::Target::
|
8
|
+
assert_equal 1, Polling::Target::interval(0)
|
9
|
+
assert_equal 6, Polling::Target::interval(5)
|
10
|
+
assert_equal 0, Polling::Target::interval(59)
|
11
|
+
assert_equal 1, Polling::Target::interval(60)
|
12
|
+
assert_equal 2, Polling::Target::interval(61)
|
13
13
|
|
14
14
|
arr = [0,1,5,10,20,30,40,50,58]
|
15
15
|
arr.each do |time|
|
16
|
-
assert_equal time+1, Polling::Target::
|
16
|
+
assert_equal time+1, Polling::Target::interval(time)
|
17
17
|
end
|
18
18
|
|
19
19
|
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:00")
|
20
|
-
assert_equal 180, Polling::Target::
|
21
|
-
assert_equal 300, Polling::Target::
|
22
|
-
assert_equal 300, Polling::Target::
|
23
|
-
assert_equal 3600, Polling::Target::
|
20
|
+
assert_equal 180, Polling::Target::interval(180,true)
|
21
|
+
assert_equal 300, Polling::Target::interval(300,true)
|
22
|
+
assert_equal 300, Polling::Target::interval(300,true)
|
23
|
+
assert_equal 3600, Polling::Target::interval(3600,true)
|
24
24
|
|
25
25
|
end
|
26
26
|
end
|
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polling
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- hiro-su
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-16 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: polling
|
15
15
|
email:
|
16
|
-
- h
|
16
|
+
- h.sugipon@gmail.com
|
17
17
|
executables: []
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
@@ -34,7 +34,7 @@ files:
|
|
34
34
|
- test/test_run.rb
|
35
35
|
- test/test_sleep.rb
|
36
36
|
- test/test_target.rb
|
37
|
-
homepage: https://github.com/
|
37
|
+
homepage: https://github.com/hiro-su/polling
|
38
38
|
licenses: []
|
39
39
|
post_install_message:
|
40
40
|
rdoc_options: []
|