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

Attributes included from Dsl::Validation

#defaults, #should_bes, #validates, #variants

Instance Method Summary collapse

Methods inherited from Defer

#get_value_safe

Methods inherited from Eye::Checker

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

Methods included from Dsl::Validation

#inherited, #param, #validate

Constructor Details

#initialize(*args) ⇒ Http

Returns a new instance of Http.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eye/checker/http.rb', line 17

def initialize(*args)
  super

  @uri = URI.parse(url) rescue URI.parse('http://127.0.0.1')
  @pattern = pattern
  @kind = case kind
            when Fixnum then Net::HTTPResponse::CODE_TO_OBJ[kind]
            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.



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

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
49
# 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.class.is_a?(Net::OpenTimeout) ? "OpenTimeout<#{@open_timeout}>" : "ReadTimeout<#{@read_timeout}>"
    {:exception => mes}
  else
    error "#{ex.message}"
    {:exception => "Timeout<#{@open_timeout},#{@read_timeout}>"}
  end

rescue => ex
  {:exception => "Error<#{ex.message}>"}
end

#good?(value) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eye/checker/http.rb', line 51

def good?(value)
  return false unless value[:result]

  unless value[:result].kind_of?(@kind)
    return false
  end

  if @pattern
    matched = if @pattern.is_a?(Regexp)
      @pattern === value[:result].body
    else
      value[:result].body.include?(@pattern.to_s)
    end
    value[:notice] = "missing '#{@pattern.to_s}'" unless matched
    matched
  else
    true
  end
end

#human_value(value) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/eye/checker/http.rb', line 71

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