poller 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ # This class expects a Net::HTTPResponse object and a second argument in its constructor.
2
+ # The second argument can be a String or a Regexp
3
+
4
+ module Matchers
5
+ module HTTP
6
+
7
+ class ResponseBodyContains
8
+
9
+ def initialize(search_term)
10
+ @search_term = search_term
11
+ end
12
+
13
+ def matches?(http_response)
14
+ return @search_term.match(http_response.body) if @search_term.class == Regexp
15
+ http_response.body.include?(@search_term)
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,2 @@
1
+ require 'poller/http/http_poller'
2
+ require 'matchers/http/response_body_contains'
@@ -0,0 +1,31 @@
1
+ # The HttpPoller will constuct an HttpProbe and sample it periodically based on
2
+ # the value given for period_s until a timeout occurs after timeout_s seconds.
3
+ #
4
+ # The proxy information is expected to be either nil or given as an OpenStruct
5
+ # with attributes 'hostname', 'port', 'user' and 'password'.
6
+ # It is also possible to pass in the information as a Hash that will get converted
7
+ # internally into an OpenStruct. In this case, make sure you use these keys:
8
+ # { :hostname => 'proxy.internal.com', :port => 8080, :user => 'user', :password => 'pwd' }
9
+
10
+ require 'poller/poller'
11
+ require 'poller/http/http_probe'
12
+ require 'ostruct'
13
+
14
+ module Poller
15
+ module HTTP
16
+
17
+ class HttpPoller
18
+ include Poller
19
+
20
+ def initialize(url, matcher, timeout_s, period_s, proxy = nil)
21
+ proxy = OpenStruct.new(proxy) if proxy.is_a?(Hash)
22
+ probe = proxy.nil? \
23
+ ? HttpProbe.new(url, matcher) \
24
+ : HttpProbe.new(url, matcher, proxy.hostname, proxy.port, proxy.user, proxy.password)
25
+ super(probe, timeout_s, period_s, url)
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ # The HttpProbe class is built on top of the Ruby built-in net/http library.
2
+ # Proxy support is provided. Please note that the Net::HTTP::Proxy method will
3
+ # return a Net::HTTP object if no proxy information is given (ie proxy_hostname is nil)
4
+ #
5
+ # The 'sample' method will wrap any Exception it may catch into a RuntimeError.
6
+ #
7
+ # Equally, any HTTP Response other than Net::HTTPSuccess will also get wrapped
8
+ # into a RuntimeError as this class expects a GET request to return 200|OK or
9
+ # equivalent HTTP Success objects in its current implementation.
10
+ #
11
+ # HttpProbe also expects a matcher to be passed in. The matcher must return
12
+ # either 'true or 'false' when given the http_response for evaluation via
13
+ # its 'matches?' method.
14
+
15
+ require 'uri'
16
+ require 'net/http'
17
+
18
+ module Poller
19
+ module HTTP
20
+
21
+ class HttpProbe
22
+
23
+ def initialize(url_s, matcher, proxy_hostname = nil, proxy_port = nil, proxy_user = nil, proxy_pwd = nil)
24
+ @uri = URI(url_s)
25
+ @matcher = matcher
26
+ @proxy = Net::HTTP::Proxy(proxy_hostname, proxy_port, proxy_user, proxy_pwd)
27
+ end
28
+
29
+ def sample
30
+ begin
31
+ @http_response = @proxy.get_response(@uri)
32
+ return if @http_response.class == Net::HTTPOK
33
+ rescue Exception => e
34
+ raise RuntimeError, "#sample caught an Exception of class #{e.class} with message: #{e.message}"
35
+ end
36
+ raise RuntimeError, "HTTP request failed, the error class is: #{@http_response.class}"
37
+ end
38
+
39
+ def satisfied?
40
+ return false if @http_response.nil?
41
+ @matcher.matches?(@http_response)
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,37 @@
1
+ # This module handles the generic bits of the poller gem and
2
+ # is intended to be mixed into any concrete Poller implementations
3
+ # The generic parts include the periodic checking of a Probe and
4
+ # raising a RuntimeError in case the timeout period has been exceeded.
5
+ #
6
+ # Poller expects that the probe object has methods called 'satisfied?'
7
+ # that will return a boolean and 'sample' which takes the next sample
8
+ # and has no explicit return value.
9
+
10
+ require 'poller/timeout'
11
+
12
+ module Poller
13
+ module Poller
14
+
15
+ def initialize(probe, timeout_s, period_s, name = nil)
16
+ @probe = probe
17
+ @timeout_s = timeout_s
18
+ @period_s = period_s
19
+ @name = name.nil? ? "no name given" : name
20
+ end
21
+
22
+ def check
23
+ # This following line allows us to inject a timeout object from within our tests
24
+ @timeout ||= Timeout.new(@timeout_s)
25
+
26
+ while !@probe.satisfied?
27
+ raise RuntimeError, "Timeout period has been exceeded for Poller (#{@name})" if @timeout.occured?
28
+ Kernel.sleep @period_s
29
+
30
+ @probe.sample
31
+ end
32
+
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ module Poller
2
+ class Timeout
3
+
4
+ # Specify the timeout period in Integer or Floating Point representation
5
+ # +period_s+:: The timeout period in seconds
6
+ def initialize(period_s)
7
+ @period = period_s
8
+ @start_time = Time.now
9
+ end
10
+
11
+ # Returns true if timeout period has elapsed, false if not
12
+ def occured?
13
+ Time.now - @start_time >= @period
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Poller
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Markus Krogemann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.12.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: 2.12.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: simplecov
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.7.1
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: 0.7.1
46
+ description: Implementations of Poller and Probe as inspired by Steve Freeman and
47
+ Nat Pryce in their GOOS book
48
+ email:
49
+ - markus@krogemann.de
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/matchers/http/response_body_contains.rb
55
+ - lib/poller/http/http_poller.rb
56
+ - lib/poller/http/http_probe.rb
57
+ - lib/poller/poller.rb
58
+ - lib/poller/timeout.rb
59
+ - lib/poller/version.rb
60
+ - lib/poller.rb
61
+ homepage: https://github.com/mkrogemann/poller
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.24
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Pollers and Probes
85
+ test_files: []