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.

  • :post - The URL to post.

  • :payload - Request body parameters.

  • :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.

  • :follow_redirect - Follow 302 redirect or not (defaults to true)

Example

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UrlCheck

Returns a new instance of UrlCheck.

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/is_it_working/checks/url_check.rb', line 33

def initialize(options={})
  # TODO: Add support for PUT, DELETE & PATCH request methods

  raise ArgumentError.new(":get|:post must provide the URL to check") unless options[:get] || options[:post]
  # request method (get|post|put|delete)

  @request_method = if options.key?(:get)
                      :get
                    elsif options.key?(:post)
                      :post
                    else
                      :get # fallback

                    end
  @request_payload = options[:payload]
  @follow_redirect = options[:follow_redirect].nil? ? true : options[:follow_redirect]
  @uri = URI.parse(options[request_method])
  @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[request_method]
end

Instance Attribute Details

#follow_redirectObject (readonly)

Follow 302 redirect or not, default is true



31
32
33
# File 'lib/is_it_working/checks/url_check.rb', line 31

def follow_redirect
  @follow_redirect
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



29
30
31
# File 'lib/is_it_working/checks/url_check.rb', line 29

def request_method
  @request_method
end

#request_payloadObject (readonly)

Returns the value of attribute request_payload.



29
30
31
# File 'lib/is_it_working/checks/url_check.rb', line 29

def request_payload
  @request_payload
end

Instance Method Details

#call(status) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/is_it_working/checks/url_check.rb', line 56

def call(status)
  t = Time.now
  response = perform_http_request
  ok_msg = "#{request_method.upcase} #{@alias} responded with response '#{response.code} #{response.message}'"
  if response.is_a?(Net::HTTPSuccess)
    status.ok(ok_msg)
  # 200 ok for 302 unless follow_redirect

  elsif response.is_a?(Net::HTTPRedirection) && !follow_redirect
    status.ok(ok_msg)
  else
    status.fail("#{request_method.upcase} #{@alias} failed with response '#{response.code} #{response.message}'")
  end
rescue Timeout::Error
  status.fail("#{request_method.upcase} #{@alias} timed out after #{Time.now - t} seconds")
end