class Eye::Checker::Http

Attributes

uri[R]

Public Class Methods

new(*args) click to toggle source
Calls superclass method Eye::Checker::new
# File lib/eye/checker/http.rb, line 18
def initialize(*args)
  super

  @uri = URI.parse(url)
  @proxy_uri = URI.parse(proxy_url) if proxy_url
  @kind = case kind
    when Integer then Net::HTTPResponse::CODE_TO_OBJ[kind.to_s]
    when String, Symbol then Net.const_get("HTTP#{kind.to_s.camelize}") rescue Net::HTTPSuccess
    else Net::HTTPSuccess
  end
  @open_timeout = (open_timeout || 3).to_f
  @read_timeout = (read_timeout || timeout || 15).to_f
end

Public Instance Methods

get_value() click to toggle source
# File lib/eye/checker/http.rb, line 32
def get_value
  res = session.start { |http| http.get(@uri.request_uri) }
  { result: res }

rescue Timeout::Error => ex
  debug { ex.inspect }

  if defined?(Net::OpenTimeout) # for ruby 2.0
    mes = ex.is_a?(Net::OpenTimeout) ? "OpenTimeout<#{@open_timeout}>" : "ReadTimeout<#{@read_timeout}>"
    { exception: mes }
  else
    { exception: "Timeout<#{@open_timeout},#{@read_timeout}>" }
  end

rescue => ex
  { exception: "Error<#{ex.message}>" }
end
good?(value) click to toggle source
# File lib/eye/checker/http.rb, line 50
def good?(value)
  return false unless value[:result]
  return false unless value[:result].is_a?(@kind)

  if pattern
    matched = if pattern.is_a?(Regexp)
      !!value[:result].body.match(pattern)
    else
      value[:result].body.include?(pattern.to_s)
    end
    value[:notice] = "missing '#{pattern}'" unless matched
    matched
  else
    true
  end
end
human_value(value) click to toggle source
# File lib/eye/checker/http.rb, line 67
def human_value(value)
  if !value.is_a?(Hash)
    '-'
  elsif value[:exception]
    value[:exception]
  else
    body_size = value[:result].body.size / 1024
    msg = "#{value[:result].code}=#{body_size}Kb"
    msg += "<#{value[:notice]}>" if value[:notice]
    msg
  end
end

Private Instance Methods

net_http() click to toggle source
# File lib/eye/checker/http.rb, line 95
def net_http
  if @proxy_uri
    Net::HTTP.new(@uri.host, @uri.port, @proxy_uri.host, @proxy_uri.port)
  else
    Net::HTTP.new(@uri.host, @uri.port)
  end
end
session() click to toggle source
# File lib/eye/checker/http.rb, line 82
def session
  net_http.tap do |session|
    if @uri.scheme == 'https'
      require 'net/https'
      session.use_ssl = true
      session.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    session.open_timeout = @open_timeout
    session.read_timeout = @read_timeout
  end
end