Class: Rapporteur::Checker
- Inherits:
-
Object
- Object
- Rapporteur::Checker
- 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
-
#add_check(object_or_nil_with_block = nil, &block) ⇒ Object
Public: Add a pre-built or custom check to your status endpoint.
-
#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.
-
#add_message(name, message, i18n_options = {}) ⇒ Object
Public: Adds a status message for inclusion in the success response.
-
#as_json(_args = {}) ⇒ Object
Internal: Returns a hash of messages suitable for conversion into JSON.
-
#clear ⇒ Object
Public: Empties all configured checks from the checker.
-
#errors ⇒ Object
Internal: Used by Rails’ JSON serialization to render error messages.
-
#halt! ⇒ Object
Public: Checks can call this method to halt any further processing.
-
#initialize ⇒ Checker
constructor
A new instance of Checker.
-
#read_attribute_for_serialization(key) ⇒ Object
(also: #read_attribute_for_validation)
Internal: Used by Rails’ JSON serialization.
-
#run ⇒ Object
Public: This is the primary execution point for this class.
Constructor Details
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.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rapporteur/checker.rb', line 33 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.
117 118 119 120 |
# File 'lib/rapporteur/checker.rb', line 117 def add_error(name, , = {}) errors.add(name, , ) 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.(:repository, '[email protected]/user/repo.git')
checker.(:load, 0.934)
checker.(:load, :too_high, :measurement => '0.934')
Returns self.
144 145 146 147 |
# File 'lib/rapporteur/checker.rb', line 144 def (name, , = {}) .add(name, , ) self end |
#as_json(_args = {}) ⇒ Object
Internal: Returns a hash of messages suitable for conversion into JSON.
152 153 154 |
# File 'lib/rapporteur/checker.rb', line 152 def as_json(_args = {}) .to_hash end |
#clear ⇒ Object
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.
51 52 53 54 |
# File 'lib/rapporteur/checker.rb', line 51 def clear check_list.clear self end |
#errors ⇒ Object
Internal: Used by Rails’ JSON serialization to render error messages.
159 160 161 |
# File 'lib/rapporteur/checker.rb', line 159 def errors Thread.current[:rapporteur_errors] ||= MessageList.new(: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.
65 66 67 |
# File 'lib/rapporteur/checker.rb', line 65 def halt! @halted = true end |
#read_attribute_for_serialization(key) ⇒ Object Also known as: read_attribute_for_validation
Internal: Used by Rails’ JSON serialization.
166 167 168 |
# File 'lib/rapporteur/checker.rb', line 166 def read_attribute_for_serialization(key) [key] end |
#run ⇒ Object
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.
75 76 77 78 79 80 81 82 |
# File 'lib/rapporteur/checker.rb', line 75 def run reset check_list.each do |object| object.call(self) break if @halted end self end |