Class: Rapporteur::Checker

Inherits:
Object
  • Object
show all
Extended by:
CheckerDeprecations
Defined in:
lib/rapporteur/checker.rb

Overview

The center of the Rapporteur library, Checker manages holding and running the custom checks, holding any application error messages, and provides the controller with that data for rendering.

Instance Method Summary collapse

Constructor Details

#initializeChecker

Returns a new instance of Checker.



10
11
12
13
14
15
# File 'lib/rapporteur/checker.rb', line 10

def initialize
  @messages = MessageList.new(:messages)
  @errors = MessageList.new(:errors)
  @check_list = CheckList.new
  reset
end

Instance Method Details

#add_check(object_or_nil_with_block = nil, &block) ⇒ Object

Public: Add a pre-built or custom check to your status endpoint. These checks are used to test the state of the world of the application, and need only respond to ‘#call`.

Once added, the given check will be called and passed an instance of this checker. If everything is good, do nothing! If there is a problem, use ‘add_error` to add an error message to the checker.

Examples

Rapporteur.add_check { |checker|
  checker.add_error(:luck, :bad) if rand(2) == 1
}

Returns self. Raises ArgumentError if the given check does not respond to call.



35
36
37
38
39
40
41
42
43
44
# File 'lib/rapporteur/checker.rb', line 35

def add_check(object_or_nil_with_block=nil, &block)
  if block_given?
    @check_list.add(block)
  elsif object_or_nil_with_block.respond_to?(:call)
    @check_list.add(object_or_nil_with_block)
  else
    raise ArgumentError, "A check must respond to #call."
  end
  self
end

#add_error(name, message, i18n_options = {}) ⇒ Object

Public: Add an error message to the checker in order to have it rendered in the status request.

It is suggested that you use I18n and locale files for these messages, as is done with the pre-built checks. If you’re using I18n, you’ll need to define ‘rapporteur.errors.<your key>.<your message>`.

name - A Symbol which indicates the attribute, name, or other identifier

for the check being run.

message - A String/Symbol/Proc to identify the message to provide in a

check failure situation.

A String is presented as given.
A Symbol will attempt to translate through I18n or default to the String-form of the symbol.
A Proc which will be called and the return value will be presented as returned.

i18n_options - A Hash of options (likely variable key/value pairs) to

pass to I18n during translation.

Examples

checker.add_error(:you, "failed.")
checker.add_error(:using, :i18n_key_is_better)
checker.add_error(:using, :i18n_with_variable, :custom_variable => 'pizza')

en:
  rapporteur:
    errors:
      using:
        i18n_key_is_better: 'Look, localization!'
        i18n_with_variable: 'Look, %{custom_variable}!'

Returns self.



119
120
121
122
# File 'lib/rapporteur/checker.rb', line 119

def add_error(name, message, i18n_options={})
  @errors.add(name, message, i18n_options)
  self
end

#add_message(name, message, i18n_options = {}) ⇒ Object

Public: Adds a status message for inclusion in the success response.

name - A String containing the name or identifier for your message. This

is unique and may be overriden by other checks using the name
message name key.

message - A String/Symbol/Proc containing the message to present to the

user in a successful check sitaution.

A String is presented as given.
A Symbol will attempt to translate through I18n or default to the String-form of the symbol.
A Proc which will be called and the return value will be presented as returned.

Examples

checker.add_message(:repository, '[email protected]/user/repo.git')
checker.add_message(:load, 0.934)
checker.add_message(:load, :too_high, :measurement => '0.934')

Returns self.



146
147
148
149
# File 'lib/rapporteur/checker.rb', line 146

def add_message(name, message, i18n_options={})
  @messages.add(name, message, i18n_options)
  self
end

#as_json(args = {}) ⇒ Object

Internal: Returns a hash of messages suitable for conversion into JSON.



154
155
156
# File 'lib/rapporteur/checker.rb', line 154

def as_json(args={})
  @messages.to_hash
end

#clearObject

Public: Empties all configured checks from the checker. This may be useful for testing and for cases where you might’ve built up some basic checks but for one reason or another (environment constraint) need to start from scratch.

Returns self.



53
54
55
56
# File 'lib/rapporteur/checker.rb', line 53

def clear
  @check_list.clear
  self
end

#errorsObject

Internal: Used by Rails’ JSON serialization to render error messages.



161
162
163
# File 'lib/rapporteur/checker.rb', line 161

def errors
  @errors
end

#halt!Object

Public: Checks can call this method to halt any further processing. This is useful for critical or fatal check failures.

For example, if load is too high on a machine you may not want to run any other checks.

Returns true.



67
68
69
# File 'lib/rapporteur/checker.rb', line 67

def halt!
  @halted = true
end

#read_attribute_for_serialization(key) ⇒ Object Also known as: read_attribute_for_validation

Internal: Used by Rails’ JSON serialization.



168
169
170
# File 'lib/rapporteur/checker.rb', line 168

def read_attribute_for_serialization(key)
  @messages[key]
end

#runObject

Public: This is the primary execution point for this class. Use run to exercise the configured checker and collect any application errors or data for rendering.

Returns self.



77
78
79
80
81
82
83
84
# File 'lib/rapporteur/checker.rb', line 77

def run
  reset
  @check_list.each do |object|
    object.call(self)
    break if @halted
  end
  self
end