Class: WatchmonkeyCli::Checkers::WwwAvailability

Inherits:
WatchmonkeyCli::Checker show all
Defined in:
lib/watchmonkey_cli/checkers/www_availability.rb

Instance Attribute Summary

Attributes inherited from WatchmonkeyCli::Checker

#app

Instance Method Summary collapse

Methods inherited from WatchmonkeyCli::Checker

#_tolog, checker_name, checker_name=, #debug, descendants, #error, #info, inherited, #init, #initialize, #local, #rsafe, #safe, #spawn_sub, #start, #stop

Constructor Details

This class inherits a constructor from WatchmonkeyCli::Checker

Instance Method Details

#check!(result, page, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/watchmonkey_cli/checkers/www_availability.rb', line 15

def check! result, page, opts = {}
  begin
    resp = HTTParty.get(page, no_follow: true, verify: false)
    result.result = resp
  rescue HTTParty::RedirectionTooDeep => e
    result.result = e.response
    original_response = true
  rescue Errno::ECONNREFUSED => e
    result.error! "Failed to fetch #{page} (#{e.class}: #{e.message})"
    return
  end

  # status
  if opts[:status]
    stati = [*opts[:status]]
    result.error! "#{result.result.code} is not in #{stati}!" if !stati.include?(result.result.code.to_i)
  end

  # body
  if rx = opts[:body]
    if rx.is_a?(String)
      result.error! "body does not include #{rx}!" if !result.result.body.include?(rx)
    elsif rx.is_a?(Regexp)
      result.error! "body does not match #{rx}!" if !result.result.body.match(rx)
    end
  end

  # headers
  if opts[:headers]
    hdata = original_response ? result.result : result.result.headers
    opts[:headers].each do |k, v|
      if !(v.is_a?(Regexp) ? hdata[k].match(v) : hdata[k] == v)
        result.error! "header #{k} mismatches (expected `#{v}' got `#{hdata[k] || "nil"}')"
      end
    end
  end
end

#enqueue(page, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/watchmonkey_cli/checkers/www_availability.rb', line 6

def enqueue page, opts = {}
  app.enqueue(self, page, opts.except(:ssl_expiration))

  # if available enable ssl_expiration support
  if page.start_with?("https://") && opts[:ssl_expiration] != false
    spawn_sub("ssl_expiration", page, opts[:ssl_expiration].is_a?(Hash) ? opts[:ssl_expiration] : {})
  end
end