Class: Rapporteur::Checker
- Inherits:
-
Object
- Object
- Rapporteur::Checker
- Extended by:
- CheckerDeprecations
- Includes:
- ActiveModel::Validations
- 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(key, message, options = {}) ⇒ Object
Public: Add an error message to the checker in order to have it rendered in the status request.
-
#add_message(name, message) ⇒ 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.
-
#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, specifically in ActionController::Responder.
-
#run ⇒ Object
Public: This is the primary execution point for this class.
Constructor Details
#initialize ⇒ Checker
Returns a new instance of Checker.
13 14 15 16 17 |
# File 'lib/rapporteur/checker.rb', line 13 def initialize @messages = Hash.new @checks = Set.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("Bad luck.") if rand(2) == 1
}
Returns self. Raises ArgumentError if the given check does not respond to call.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rapporteur/checker.rb', line 37 def add_check(object_or_nil_with_block=nil, &block) if block_given? @checks << block elsif object_or_nil_with_block.respond_to?(:call) @checks << object_or_nil_with_block else raise ArgumentError, "A check must respond to #call." end self end |
#add_error(key, message, 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 I19n, you’ll need to define ‘activemodel.errors.models.rapporteur/checker.attributes.base.<your key>`.
Examples
checker.add_error("You failed.")
checker.add_error(:i18n_key_is_better)
Returns self.
102 103 104 105 106 107 |
# File 'lib/rapporteur/checker.rb', line 102 def add_error(key, , ={}) [:scope] = [:rapporteur, :errors, key] [:default] = [, .to_s.humanize] errors.add(key, , ) self end |
#add_message(name, message) ⇒ 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 or Numeric for the value of the message.
Examples
checker.(:repository, '[email protected]/user/repo.git')
checker.(:load, 0.934)
Returns self.
124 125 126 127 |
# File 'lib/rapporteur/checker.rb', line 124 def (name, ) @messages[name] = self end |
#as_json(args = {}) ⇒ Object
Internal: Returns a hash of messages suitable for conversion into JSON.
132 133 134 |
# File 'lib/rapporteur/checker.rb', line 132 def as_json(args={}) @messages 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.
55 56 57 58 |
# File 'lib/rapporteur/checker.rb', line 55 def clear @checks.clear self 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.
69 70 71 |
# File 'lib/rapporteur/checker.rb', line 69 def halt! @halted = true end |
#read_attribute_for_serialization(key) ⇒ Object Also known as: read_attribute_for_validation
Internal: Used by Rails’ JSON serialization, specifically in ActionController::Responder.
140 141 142 |
# File 'lib/rapporteur/checker.rb', line 140 def read_attribute_for_serialization(key) @messages[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.
79 80 81 82 83 84 85 86 |
# File 'lib/rapporteur/checker.rb', line 79 def run reset @checks.each do |object| object.call(self) break if @halted end self end |