polling 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'mocha'
data/LICENSE.txt ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012, Internet Initiative Japan Inc.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Polling
2
+
3
+ fluent-plugin-snmp用pollingライブラリ
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'polling'
10
+
11
+ ## Usage
12
+
13
+ 必ず0秒からスタートします
14
+
15
+ time = [0,10,20,30,40,50]
16
+ Polling::run(time,debug=false) do
17
+ p "hoge"
18
+ end
19
+
20
+ もし60以上の値を設定する場合には60で割り切れる値を設定
21
+
22
+ time = [300]
23
+ Polling::run(time,debug=false) do
24
+ p "hoge"
25
+ end
26
+
27
+ ## Copyright
28
+
29
+ Copyright:: Copyright (c) 2012, Internet Initiative Japan Inc. All rights reserved.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new
@@ -0,0 +1,10 @@
1
+ module Polling
2
+ module Confirm
3
+ module_function
4
+ def check_value(value)
5
+ value = value.to_i
6
+ raise TypeError, "Please set as a multiple of 60" if (value % 60 != 0) && value > 60
7
+ return value
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module Polling
2
+ module Sleep
3
+ module_function
4
+ def exec(stime)
5
+ stime > 0 ? (sleep stime) : false
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ require 'time'
2
+
3
+ module Polling
4
+ module Target
5
+ module_function
6
+
7
+ class << self
8
+ #TODO imple delay
9
+ attr_accessor :delay
10
+ end
11
+
12
+ def create_time(interval,debug=false)
13
+ init = 60
14
+ now_to_f = Time.now.to_f
15
+
16
+ stime = interval - (now_to_f % init)
17
+ if stime < 0
18
+ stime = init - stime.abs
19
+ end
20
+
21
+ ### debug
22
+ print "sleep #{stime}seconds (until #{Time.at(now_to_f + stime)})\n" if debug
23
+
24
+ return stime
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Polling
2
+ VERSION = "0.0.2"
3
+ end
data/lib/polling.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'polling/version'
2
+ require 'polling/confirm'
3
+ require 'polling/sleep'
4
+ require 'polling/target'
5
+
6
+ module Polling
7
+ module_function
8
+ def run(arr, debug=false)
9
+ start = true
10
+
11
+ loop do
12
+ arr.each do |time|
13
+ time = Confirm::check_value(time)
14
+ Sleep::exec Target::create_time(0, debug) if start
15
+ Sleep::exec Target::create_time(time, debug) unless start
16
+ start = false
17
+ yield
18
+ end
19
+ end
20
+ end
21
+ end
data/polling.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'polling/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "polling"
8
+ gem.version = Polling::VERSION
9
+ gem.authors = ["hiroshi sugimoto"]
10
+ gem.email = ["h-sugimoto@iij.ad.jp"]
11
+ gem.description = %q{polling}
12
+ gem.summary = %q{polling}
13
+ gem.homepage = "https://github.com/iij/polling"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'polling'
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
2
+
3
+ class PollingTest < Test::Unit::TestCase
4
+ def test_check_value
5
+ assert_equal 0, Polling::Confirm::check_value(0)
6
+ assert_equal 0, Polling::Confirm::check_value("0")
7
+ assert_equal 60, Polling::Confirm::check_value(60)
8
+ assert_equal 60, Polling::Confirm::check_value("60")
9
+ assert_equal RuntimeError, Polling::Confirm::check_value(61)
10
+ end
11
+ end
data/test/test_run.rb ADDED
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
2
+
3
+ class PollingTest < Test::Unit::TestCase
4
+ #def test_run
5
+ # arr = [0,5,10,15,20,25,30,35,40,45,50,55]
6
+ # Polling::run(arr,true) do
7
+ # print "end: Ctrl+c\n"
8
+ # end
9
+ #end
10
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
2
+
3
+ class PollingTest < Test::Unit::TestCase
4
+ def test_sleep
5
+ assert_equal false, Polling::Sleep::exec(-1)
6
+ assert_equal false, Polling::Sleep::exec(0)
7
+ #assert_equal 5, Polling::Sleep::exec(5)
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
2
+ require 'time'
3
+
4
+ class PollingTest < Test::Unit::TestCase
5
+
6
+ def test_target_time
7
+ Time.stubs(:now).returns(Time.parse "2012/12/31 23:59:59")
8
+ assert_equal 1, Polling::Target::create_time(0)
9
+ assert_equal 6, Polling::Target::create_time(5)
10
+ assert_equal 0, Polling::Target::create_time(59)
11
+ assert_equal 1, Polling::Target::create_time(60)
12
+ assert_equal 2, Polling::Target::create_time(61)
13
+
14
+ arr = [0,1,5,10,20,30,40,50,58]
15
+ arr.each do |time|
16
+ assert_equal time+1, Polling::Target::create_time(time)
17
+ end
18
+
19
+ Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:00")
20
+ assert_equal 180, Polling::Target::create_time(180,true)
21
+ assert_equal 300, Polling::Target::create_time(300,true)
22
+ assert_equal 300, Polling::Target::create_time(300,true)
23
+ assert_equal 3600, Polling::Target::create_time(3600,true)
24
+
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polling
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - hiroshi sugimoto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: polling
15
+ email:
16
+ - h-sugimoto@iij.ad.jp
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/polling.rb
27
+ - lib/polling/confirm.rb
28
+ - lib/polling/sleep.rb
29
+ - lib/polling/target.rb
30
+ - lib/polling/version.rb
31
+ - polling.gemspec
32
+ - test/helper.rb
33
+ - test/test_confirm.rb
34
+ - test/test_run.rb
35
+ - test/test_sleep.rb
36
+ - test/test_target.rb
37
+ homepage: https://github.com/iij/polling
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.24
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: polling
61
+ test_files:
62
+ - test/helper.rb
63
+ - test/test_confirm.rb
64
+ - test/test_run.rb
65
+ - test/test_sleep.rb
66
+ - test/test_target.rb