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.



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

def response_body
  @response_body
end

#response_codeObject (readonly)

Returns the value of attribute response_code.



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

def response_code
  @response_code
end

#response_timeObject (readonly)

Returns the value of attribute response_time.



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

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/outpost/scouts/http.rb', line 45

def execute
  previous_time = Time.now
  if @request_head
    response = @http_class.request_head(@host, @path, @port)
  else
    uri = (@ssl ? URI::HTTPS : URI::HTTP).build({
      host: @host,
      port: @port,
      path: @path
    })
    http = @http_class.new(uri.host, uri.port)
    http.use_ssl = @ssl

    request = @http_class::Get.new(uri.request_uri)
    response = http.request(request)
  end

  @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

  • :ssl (Boolean)

    Should we use ssl? host.

  • :http_class (String)

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



34
35
36
37
38
39
40
41
# File 'lib/outpost/scouts/http.rb', line 34

def setup(options)
  @host         = options.fetch(:host)
  @path         = options.fetch(:path, '/')
  @ssl          = options.fetch(:ssl, false)
  @port         = options.fetch(:port, (@ssl ? 443 : 80))
  @http_class   = options.fetch(:http_class, Net::HTTP)
  @request_head = options.fetch(:request_head, false)
end