Class: IsItWorking::UrlCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/is_it_working/checks/url_check.rb

Overview

Check if getting a URL returns a successful response. Only responses in the range 2xx or 304 are considered successful. Redirects will not be followed.

Available options are:

  • :get - The URL to get.

  • :headers - Hash of headers to send with the request

  • :proxy - Hash of proxy server information. The hash must contain a :host key and may contain :port, :username, and :password

  • :username - Username to use for Basic Authentication

  • :password - Password to use for Basic Authentication

  • :open_timeout - Time in seconds to wait for opening the connection (defaults to 5 seconds)

  • :read_timeout - Time in seconds to wait for data from the connection (defaults to 10 seconds)

  • :alias - Alias used for reporting in case making the URL known to the world could provide a security risk.

Example

IsItWorking::Handler.new do |h|
  h.check :url, :get => "http://services.example.com/api", :headers => {"Accept" => "text/xml"}
end

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UrlCheck

Returns a new instance of UrlCheck.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/is_it_working/checks/url_check.rb', line 25

def initialize(options={})
  raise ArgumentError.new(":get must provide the URL to check") unless options[:get]
  @uri = URI.parse(options[:get])
  @headers = options[:headers] || {}
  @proxy = options[:proxy]
  @username = options[:username]
  @password = options[:password]
  @open_timeout = options[:open_timeout] || 5
  @read_timeout = options[:read_timeout] || 10
  @alias = options[:alias] || options[:get]
end

Instance Method Details

#call(status) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/is_it_working/checks/url_check.rb', line 37

def call(status)
  t = Time.now
  response = perform_http_request
  if response.is_a?(Net::HTTPSuccess)
    status.ok("GET #{@alias} responded with response '#{response.code} #{response.message}'")
  else
    status.fail("GET #{@alias} failed with response '#{response.code} #{response.message}'")
  end
rescue Timeout::Error
  status.fail("GET #{@alias} timed out after #{Time.now - t} seconds")
end