peeping_tom 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/peeping_tom.rb CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  require File.join(File.dirname(__FILE__), "peeping_tom", "peeper")
3
3
  require File.join(File.dirname(__FILE__), "peeping_tom", "site")
4
+ require File.join(File.dirname(__FILE__), "peeping_tom", "with_proxy")
4
5
  require File.join(File.dirname(__FILE__), "peeping_tom", "dsl")
5
6
 
6
7
 
@@ -4,36 +4,38 @@ require 'uri'
4
4
 
5
5
  module PeepingTom
6
6
  module DSL
7
- def ping(site, method = :head)
8
- method = method.to_s.downcase
9
- raise ArgumentError, "only accepts head or get" unless ['head', 'get'].include?(method)
10
-
11
- Timeout::timeout(site.timeout) do
12
- url = URI.parse(site.url)
13
- res = Net::HTTP.start(url.host, url.port) do |http|
14
- file = "/" + url.to_s.sub(/http:\/\/.*?\//, '')
15
- http.send(method, file)
16
- end
17
- return res.code.to_i >= 200 && res.code.to_i < 400
18
- end
19
- return error!
20
- rescue Timeout::Error, Errno::ECONNREFUSED, SocketError
21
- return error!
22
- end
23
7
 
24
8
  def channels
25
9
  @channels
26
10
  end
27
11
 
12
+ def ping(site)
13
+ @site.ping(*@site.with_args)
14
+ end
15
+
16
+ def verify(site)
17
+ @site.verify(*@site.with_args)
18
+ end
19
+
20
+ def with(*args)
21
+ args
22
+ end
23
+
28
24
  def watch(name)
25
+ alias :old_site :site
26
+ alias :site :proxy_site
27
+
29
28
  PeepingTom.peeper(name).sites.each_value do |site|
30
- @error = false
31
- yield site
32
- if @error
33
- $stdout.puts "#{site.name} is DOWN"
34
- @channels.each {|c| c.notify!(site)}
29
+ @site = site
30
+ yield
31
+ if site.error?
32
+ @errors = true
33
+ $stdout.puts "#{site.name} has an ERROR"
34
+ (@channels || []).each {|c| c.notify!(site)}
35
35
  end
36
36
  end
37
+
38
+ alias :site :old_site
37
39
  end
38
40
 
39
41
  def errors
@@ -46,12 +48,6 @@ module PeepingTom
46
48
  yield
47
49
  end
48
50
 
49
- def error!
50
- @error = true
51
- @errors = true
52
- return false
53
- end
54
-
55
51
  def group(name, &block)
56
52
  PeepingTom.set_peeper(name)
57
53
  yield
@@ -75,5 +71,9 @@ module PeepingTom
75
71
  def site(name, site, opts = {})
76
72
  PeepingTom.peeper.register(name, site, opts)
77
73
  end
74
+
75
+ def proxy_site(*args)
76
+ @site.with(*args)
77
+ end
78
78
  end
79
79
  end
@@ -13,5 +13,58 @@ module PeepingTom
13
13
  @url = "http://#{@url}" unless @url =~ /^http:\/\//
14
14
  @url
15
15
  end
16
+
17
+ def verify(regex = //)
18
+ Timeout::timeout(self.timeout) do
19
+ url = URI.parse(self.url)
20
+ res = Net::HTTP.start(url.host, url.port) do |http|
21
+ file = "/" + url.to_s.sub(/http:\/\/.*?\//, '')
22
+ http.get(file)
23
+ end
24
+ if res.body =~ regex
25
+ return true
26
+ else
27
+ @error = true
28
+ return false
29
+ end
30
+ end
31
+ @error = true
32
+ return false
33
+ rescue Timeout::Error, Errno::ECONNREFUSED, SocketError
34
+ @error = true
35
+ return false
36
+ end
37
+
38
+ def ping(method = :head)
39
+ method = method.to_s.downcase
40
+ method = 'head' if method == ''
41
+ raise ArgumentError, "only accepts head or get: #{method}" unless ['head', 'get'].include?(method)
42
+
43
+ Timeout::timeout(self.timeout) do
44
+ url = URI.parse(self.url)
45
+ res = Net::HTTP.start(url.host, url.port) do |http|
46
+ file = "/" + url.to_s.sub(/http:\/\/.*?\//, '')
47
+ http.send(method, file)
48
+ end
49
+ return res.code.to_i >= 200 && res.code.to_i < 400
50
+ end
51
+ @error = true
52
+ return false
53
+ rescue Timeout::Error, Errno::ECONNREFUSED, SocketError
54
+ @error = true
55
+ return false
56
+ end
57
+
58
+ def error?
59
+ @error
60
+ end
61
+
62
+ def with(*args)
63
+ @with = args
64
+ end
65
+
66
+ def with_args
67
+ @with
68
+ end
16
69
  end
17
- end
70
+ end
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 5
8
- version: "0.5"
7
+ - 6
8
+ version: "0.6"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Terry Heath