Class: WatchmonkeyCli::Checkers::WwwAvailability

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

Constant Summary

Constants included from Helper

Helper::BYTE_UNITS

Instance Attribute Summary

Attributes inherited from WatchmonkeyCli::Checker

#app

Instance Method Summary collapse

Methods inherited from WatchmonkeyCli::Checker

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

Methods included from Helper

#human_filesize, #human_number, #human_seconds

Constructor Details

This class inherits a constructor from WatchmonkeyCli::Checker

Instance Method Details

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



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
52
53
54
# File 'lib/watchmonkey_cli/checkers/www_availability.rb', line 17

def check! result, page, opts = {}
  begin
    resp = HTTParty.get(page, no_follow: true, verify: false, timeout: opts[:timeout] || 20)
    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
  fixed_body = result.result.body.force_encoding("utf-8")
  if rx = opts[:body]
    if rx.is_a?(String)
      result.error! "body does not include `#{rx}'!" if !fixed_body.include?(rx)
    elsif rx.is_a?(Regexp)
      result.error! "body does not match `#{rx}'!" if !fixed_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
14
15
# 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 && !app.running?
    sopts = { timeout: opts[:timeout] }.merge(opts[:ssl_expiration].is_a?(Hash) ? opts[:ssl_expiration] : {})
    stags = (opts[:tags] || []).reject{|t| t.to_s.start_with?("WMC-") }
    blank_config(stags).ssl_expiration(page, sopts)
  end
end