Class: Easymon::Checklist

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/easymon/checklist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = {}) ⇒ Checklist

Returns a new instance of Checklist.



11
12
13
14
# File 'lib/easymon/checklist.rb', line 11

def initialize(items={})
  self.items = items
  self.results = {}
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



8
9
10
# File 'lib/easymon/checklist.rb', line 8

def items
  @items
end

#resultsObject

Returns the value of attribute results.



9
10
11
# File 'lib/easymon/checklist.rb', line 9

def results
  @results
end

Instance Method Details

#as_json(*args) ⇒ Object



47
48
49
# File 'lib/easymon/checklist.rb', line 47

def as_json(*args)
  to_hash
end

#checkObject



16
17
18
19
20
21
22
23
24
# File 'lib/easymon/checklist.rb', line 16

def check
  self.results = items.inject({}) do |hash, (name, check)|
    check_result = []
    timing = Benchmark.realtime { check_result = check[:check].check }
    hash[name] = Easymon::Result.new(check_result, timing, check[:critical])
    hash
  end
  [self.success?, self.to_s]
end

#fetch(name) ⇒ Object

The following method could be implemented as a def_delegator by extending Forwardable, but since we want to catch IndexError and raise Easymon::NoSuchCheck, we’ll be explicit here.



64
65
66
67
68
# File 'lib/easymon/checklist.rb', line 64

def fetch(name)
  items.fetch(name)
rescue IndexError
  raise NoSuchCheck, "No check named '#{name}'"
end

#response_statusObject



56
57
58
# File 'lib/easymon/checklist.rb', line 56

def response_status
  success? ? :ok : :service_unavailable
end

#success?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/easymon/checklist.rb', line 51

def success?
  return false if results.empty?
  results.values.all?(&:success?)
end

#timingObject



26
27
28
# File 'lib/easymon/checklist.rb', line 26

def timing
  results.values.map{|r| r.timing}.inject(0, :+)
end

#to_hashObject



39
40
41
42
43
44
45
# File 'lib/easymon/checklist.rb', line 39

def to_hash
  combined = {:timing => Easymon.timing_to_ms(timing)}
  results.each do |name, result|
    combined[name] = result.to_hash
  end
  combined
end

#to_sObject



34
35
36
37
# File 'lib/easymon/checklist.rb', line 34

def to_s
  results.map{|name, result| "#{name}: #{result.to_s}"}.join("\n") +
  "\n - Total Time - " + Easymon.timing_to_ms(self.timing) + "ms"
end

#to_textObject



30
31
32
# File 'lib/easymon/checklist.rb', line 30

def to_text
  to_s
end