Class: Eye::Checker::Http

Inherits:
Defer show all
Defined in:
lib/eye/checker/http.rb

Constant Summary

Constants inherited from Eye::Checker

TYPES

Instance Attribute Summary collapse

Attributes inherited from Eye::Checker

#check_count, #options, #pid, #process, #type, #value, #values

Instance Method Summary collapse

Methods inherited from Defer

#get_value_safe

Methods inherited from Eye::Checker

#check, #check_name, create, #defer, #fire, get_class, #get_value_safe, #inspect, #last_human_values, #logger_sub_tag, #logger_tag, #max_tries, #min_tries, name_and_class, #previous_value, register, requires, #run_in_process_context, validate!

Methods included from Dsl::Validation

included

Constructor Details

#initialize(*args) ⇒ Http

Returns a new instance of Http.



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



16
17
18
# File 'lib/eye/checker/http.rb', line 16

def uri
  @uri
end

Instance Method Details

#get_valueObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# 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