Class: Poller::HTTP::HttpProbe

Inherits:
Object
  • Object
show all
Defined in:
lib/poller/http/http_probe.rb

Instance Method Summary collapse

Constructor Details

#initialize(url_s, matcher, proxy_hostname = nil, proxy_port = nil, proxy_user = nil, proxy_pwd = nil) ⇒ HttpProbe

Returns a new instance of HttpProbe.



29
30
31
32
33
# File 'lib/poller/http/http_probe.rb', line 29

def initialize(url_s, matcher, proxy_hostname = nil, proxy_port = nil, proxy_user = nil, proxy_pwd = nil)
  @uri = URI.parse(url_s)
  @matcher = matcher
  @proxy = Net::HTTP::Proxy(proxy_hostname, proxy_port, proxy_user, proxy_pwd).new(@uri.host, @uri.port)
end

Instance Method Details

#sampleObject

Raises:

  • (RuntimeError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/poller/http/http_probe.rb', line 35

def sample
  begin
    # support SSL, not veryfing the SSL certificates (out of scope from my perspective)
    if @uri.scheme == 'https'
      @proxy.use_ssl = true
      @proxy.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    request = Net::HTTP::Get.new(@uri.request_uri)
    # support basic auth if userinfo appears in url
    if @uri.userinfo
      request.basic_auth(@uri.user, @uri.password)
    end
    @http_response = @proxy.request(request)
    return @http_response if @http_response.class == Net::HTTPOK
  rescue Exception => e
    raise RuntimeError, "#sample caught an Exception of class #{e.class} with message: #{e.message}"
  end
  raise RuntimeError, "HTTP request failed, the error class is: #{@http_response.class}"
end

#satisfied?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/poller/http/http_probe.rb', line 55

def satisfied?
  return false if @http_response.nil?
  @matcher.matches?(@http_response.body)
end