Class: LitmusPaper::Dependency::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/litmus_paper/dependency/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ HTTP

Returns a new instance of HTTP.



4
5
6
7
8
9
10
# File 'lib/litmus_paper/dependency/http.rb', line 4

def initialize(uri, options = {})
  @uri = uri
  @expected_content = Regexp.new(options.fetch(:content, '.*'))
  @method = options.fetch(:method, 'GET')
  @ca_file = options[:ca_file]
  @timeout = options.fetch(:timeout_seconds, 2)
end

Instance Method Details

#_body_matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/litmus_paper/dependency/http.rb', line 55

def _body_matches?(response)
  (response.body =~ @expected_content) ? true : false
end

#_make_requestObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/litmus_paper/dependency/http.rb', line 29

def _make_request
  uri = URI.parse(@uri)
  request = Net::HTTP.const_get(@method.capitalize).new(uri.normalize.path)
  request["User-Agent"] = "Litmus Paper/#{LitmusPaper::VERSION}"
  request.set_form_data({})
  request.basic_auth(uri.user, uri.password) if uri.user || uri.password

  connection = Net::HTTP.new(uri.host, uri.port)
  connection.open_timeout = @timeout
  connection.read_timeout = @timeout
  if uri.scheme == "https"
    connection.use_ssl = true
    connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
    connection.ca_file = @ca_file unless @ca_file.nil?
    connection.verify_callback = proc { |preverify_ok, ssl_context| _verify_ssl_certificate(preverify_ok, ssl_context) }
  end

  connection.start do |http|
    http.request(request)
  end
end

#_successful_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/litmus_paper/dependency/http.rb', line 51

def _successful_response?(response)
  response.is_a? Net::HTTPSuccess
end

#_verify_ssl_certificate(preverify_ok, ssl_context) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/litmus_paper/dependency/http.rb', line 59

def _verify_ssl_certificate(preverify_ok, ssl_context)
  if preverify_ok != true || ssl_context.error != 0
    err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
    LitmusPaper.logger.info err_msg
    return false
  end
  true
end

#available?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/litmus_paper/dependency/http.rb', line 12

def available?
  response = _make_request
  success = _successful_response?(response)
  matches = _body_matches?(response)

  LitmusPaper.logger.info("Available check to #{@uri} failed with status #{response.code}") unless success
  LitmusPaper.logger.info("Available check to #{@uri} did not match #{@expected_content}") unless matches

  success && matches
rescue Timeout::Error
  LitmusPaper.logger.info("Timeout fetching #{@uri}")
  false
rescue => e
  LitmusPaper.logger.info("Available check to #{@uri} failed with #{e.message}")
  false
end

#to_sObject



68
69
70
# File 'lib/litmus_paper/dependency/http.rb', line 68

def to_s
  "Dependency::HTTP(#{@uri})"
end