Class: Easymon::Checklist
- Inherits:
-
Object
- Object
- Easymon::Checklist
- Extended by:
- Forwardable
- Defined in:
- lib/easymon/checklist.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
-
#results ⇒ Object
Returns the value of attribute results.
Instance Method Summary collapse
- #as_json(*args) ⇒ Object
- #check ⇒ Object
-
#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.
-
#initialize(items = {}) ⇒ Checklist
constructor
A new instance of Checklist.
- #response_status ⇒ Object
- #success? ⇒ Boolean
- #timing ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #to_text ⇒ Object
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
#items ⇒ Object
Returns the value of attribute items.
8 9 10 |
# File 'lib/easymon/checklist.rb', line 8 def items @items end |
#results ⇒ Object
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 |
#check ⇒ Object
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_status ⇒ Object
56 57 58 |
# File 'lib/easymon/checklist.rb', line 56 def response_status success? ? :ok : :service_unavailable end |
#success? ⇒ Boolean
51 52 53 54 |
# File 'lib/easymon/checklist.rb', line 51 def success? return false if results.empty? results.values.all?(&:success?) end |
#timing ⇒ Object
26 27 28 |
# File 'lib/easymon/checklist.rb', line 26 def timing results.values.map{|r| r.timing}.inject(0, :+) end |
#to_hash ⇒ Object
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_s ⇒ Object
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_text ⇒ Object
30 31 32 |
# File 'lib/easymon/checklist.rb', line 30 def to_text to_s end |