class OkComputer::Check

Constants

CheckNotDefined

Attributes

failure_occurred[RW]

nil by default, only set to true if the check deems itself failed

message[RW]

nil by default, set by check to control the output

registrant_name[RW]

to be set by Registry upon registration

time[RW]

Float::NAN by default, set by run to the elapsed time to run check

Public Instance Methods

<=>(check) click to toggle source
# File lib/ok_computer/check.rb, line 49
def <=>(check)
  if check.is_a?(CheckCollection)
    -1
  else
    registrant_name.to_s <=> check.registrant_name.to_s
  end
end
clear() click to toggle source

Public: Clear any prior failures

# File lib/ok_computer/check.rb, line 77
def clear
  self.failure_occurred = false
  self.message = nil
  self.time = Float::NAN
end
mark_failure() click to toggle source

Public: Mark that this check has failed in some way

# File lib/ok_computer/check.rb, line 65
def mark_failure
  self.failure_occurred = true
end
mark_message(message) click to toggle source

Public: Capture the desired message to display

message - Text of the message to display for this check

# File lib/ok_computer/check.rb, line 72
def mark_message(message)
  self.message = message
end
run() click to toggle source

Public: Run the check

# File lib/ok_computer/check.rb, line 15
def run
  clear
  with_benchmarking do
    check
  end
  OkComputer.logger.info "[okcomputer] #{to_text}"
end
success?() click to toggle source

Public: Whether the check passed

Returns a boolean

# File lib/ok_computer/check.rb, line 60
def success?
  not failure_occurred
end
to_json(*args) click to toggle source

Public: The JSON output of performing the check

Returns a String containing JSON

# File lib/ok_computer/check.rb, line 43
def to_json(*args)
  # NOTE swallowing the arguments that Rails passes by default since we don't care. This may prove to be a bad idea
  # Rails passes stuff like this: {:prefixes=>["ok_computer", "application"], :template=>"show", :layout=>#<Proc>}]
  {registrant_name => {:message => message, :success => success?, :time => time}}.to_json
end
to_text() click to toggle source

Public: The text output of performing the check

Returns a String

# File lib/ok_computer/check.rb, line 35
def to_text
  passfail = success? ? "passed" : "failed"
  I18n.t("okcomputer.check.#{passfail}", registrant_name: registrant_name, message: message, time: "#{time ? sprintf('%.3f', time) : '?'}s")
end
with_benchmarking() { || ... } click to toggle source

Private: Benchmark the time it takes to run the block

# File lib/ok_computer/check.rb, line 84
def with_benchmarking
  self.time = Benchmark.realtime do
    yield
  end
end

Private Instance Methods

check() click to toggle source

Private: Perform the appropriate check

Your subclass of Check must define its own check method. This method must return the string to render when performing the check.

# File lib/ok_computer/check.rb, line 27
def check
  raise(CheckNotDefined, "Your subclass must define its own #check.")
end