Class: Outpost::Scouts::Http

Inherits:
Outpost::Scout show all
Extended by:
Expectations::ResponseBody, Expectations::ResponseCode, Expectations::ResponseTime
Defined in:
lib/outpost/scouts/http.rb

Overview

Uses ruby’s own Net:HTTP to send HTTP requests and evaluate response body, response time and response code.

Constant Summary

Constants included from Expectations::ResponseTime

Expectations::ResponseTime::RESPONSE_TIME_MAPPING

Instance Attribute Summary collapse

Attributes inherited from Outpost::Scout

#report_data

Instance Method Summary collapse

Methods included from Expectations::ResponseCode

evaluate_response_code, extended

Methods included from Expectations::ResponseBody

evaluate_response_body, extended

Methods included from Expectations::ResponseTime

evaluate_response_time, extended

Methods inherited from Outpost::Scout

expect, expectations, #gather_reporting_data, #initialize, report_data, #run

Constructor Details

This class inherits a constructor from Outpost::Scout

Instance Attribute Details

#response_bodyObject (readonly)

Returns the value of attribute response_body.



21
22
23
# File 'lib/outpost/scouts/http.rb', line 21

def response_body
  @response_body
end

#response_codeObject (readonly)

Returns the value of attribute response_code.



21
22
23
# File 'lib/outpost/scouts/http.rb', line 21

def response_code
  @response_code
end

#response_timeObject (readonly)

Returns the value of attribute response_time.



21
22
23
# File 'lib/outpost/scouts/http.rb', line 21

def response_time
  @response_time
end

Instance Method Details

#executeObject

Runs the scout, connecting to the host and getting the response code, body and time.



41
42
43
44
45
46
47
48
49
50
# File 'lib/outpost/scouts/http.rb', line 41

def execute
  previous_time = Time.now
  response = @http_class.get_response(@host, @path, @port)

  @response_time = (Time.now - previous_time) * 1000 # Miliseconds
  @response_code = response.code.to_i
  @response_body = response.body
rescue SocketError, Errno::ECONNREFUSED
  @response_code = @response_body = @response_time = nil
end

#setup(options) ⇒ Object

Configure the scout with given options.

Parameters:

  • Options (Hash)

    to setup the scout

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :host (String)

    The host that will be connected to.

  • :port (Number)

    The port that will be used to.

  • :path (String)

    The path that will be fetched from the host.

  • :http_class (String)

    The class that will be used to fetch the page, defaults to Net::HTTP



32
33
34
35
36
37
# File 'lib/outpost/scouts/http.rb', line 32

def setup(options)
  @host       = options[:host]
  @port       = options[:port]       || 80
  @path       = options[:path]       || '/'
  @http_class = options[:http_class] || Net::HTTP
end