Class: IsItWorking::UrlCheck
- Inherits:
-
Object
- Object
- IsItWorking::UrlCheck
- 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:hostkey 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
-
#follow_redirect ⇒ Object
readonly
Follow 302 redirect or not, default is true.
-
#request_method ⇒ Object
readonly
Returns the value of attribute request_method.
-
#request_payload ⇒ Object
readonly
Returns the value of attribute request_payload.
Instance Method Summary collapse
- #call(status) ⇒ Object
-
#initialize(options = {}) ⇒ UrlCheck
constructor
A new instance of UrlCheck.
Constructor Details
#initialize(options = {}) ⇒ UrlCheck
Returns a new instance of UrlCheck.
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(={}) # TODO: Add support for PUT, DELETE & PATCH request methods raise ArgumentError.new(":get|:post must provide the URL to check") unless [:get] || [:post] # request method (get|post|put|delete) @request_method = if .key?(:get) :get elsif .key?(:post) :post else :get # fallback end @request_payload = [:payload] @follow_redirect = [:follow_redirect].nil? ? true : [:follow_redirect] @uri = URI.parse([request_method]) @headers = [:headers] || {} @proxy = [:proxy] @username = [:username] @password = [:password] @open_timeout = [:open_timeout] || 5 @read_timeout = [:read_timeout] || 10 @alias = [:alias] || [request_method] end |
Instance Attribute Details
#follow_redirect ⇒ Object (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_method ⇒ Object (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_payload ⇒ Object (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 |