The notifier handles sending messages to different notifiers. Currently the following libraries are supported:
Please see the documentation of each notifier for more information about the requirements and configuration possibilities.
Guard knows four different notification types:
success
pending
failed
notify
The notification type selection is based on the image option that is sent to {notify}. Each image type has its own notification type, and notifications with custom images goes all sent as type `notify`. The `gntp` and `growl_notify` notifiers are able to register these types at Growl and allows customization of each notification type.
Guard can be configured to make use of more than one notifier at once.
@see Guard::Dsl
List of available notifiers, grouped by functionality
Add a notification library to be used.
@param [Symbol] name the name of the notifier to use @param [Hash] options the notifier options @option options [String] silent disable any error message @return [Boolean] if the notification could be added
# File lib/guard/notifier.rb, line 151 def add_notifier(name, opts = {}) return turn_off if name == :off notifier_class = _get_notifier_module(name) if notifier_class && notifier_class.available?(opts) self.notifiers = notifiers << { name: name, options: opts } true else false end end
Clear available notifications.
# File lib/guard/notifier.rb, line 86 def clear_notifiers ENV['GUARD_NOTIFIERS'] = nil end
Test if the notifications are on.
@return [Boolean] whether the notifications are on
# File lib/guard/notifier.rb, line 140 def enabled? ENV['GUARD_NOTIFY'] == 'true' end
# File lib/guard/notifier.rb, line 76 def notifiers ENV['GUARD_NOTIFIERS'] ? YAML::load(ENV['GUARD_NOTIFIERS']) : [] end
# File lib/guard/notifier.rb, line 80 def notifiers=(notifiers) ENV['GUARD_NOTIFIERS'] = YAML::dump(notifiers) end
Show a system notification with all configured notifiers.
@param [String] message the message to show @option opts [Symbol, String] image the image symbol or path to an image @option opts [String] title the notification title
# File lib/guard/notifier.rb, line 170 def notify(message, opts = {}) return unless enabled? notifiers.each do |notifier| notifier = _get_notifier_module(notifier[:name]).new(notifier[:options]) begin notifier.notify(message, opts.dup) rescue Exception => e ::Guard::UI.error "Error sending notification with #{ notifier.name }: #{ e.message }" ::Guard::UI.debug e.backtrace.join("\n") end end end
Toggle the system notifications on/off
# File lib/guard/notifier.rb, line 127 def toggle if enabled? ::Guard::UI.info 'Turn off notifications' turn_off else turn_on end end
Turn notifications off.
# File lib/guard/notifier.rb, line 115 def turn_off notifiers.each do |notifier| notifier_class = _get_notifier_module(notifier[:name]) notifier_class.turn_off if notifier_class.respond_to?(:turn_off) end ENV['GUARD_NOTIFY'] = 'false' end
Turn notifications on. If no notifications are defined in the `Guardfile` Guard auto detects the first available library.
@param [Hash] options the turn_on options @option options [Boolean] silent disable any logging
# File lib/guard/notifier.rb, line 96 def turn_on(opts = {}) _auto_detect_notification if notifiers.empty? && (!::Guard.options || ::Guard.options[:notify]) if notifiers.empty? turn_off else notifiers.each do |notifier| notifier_class = _get_notifier_module(notifier[:name]) ::Guard::UI.info "Guard is using #{ notifier_class.title } to send notifications." unless opts[:silent] notifier_class.turn_on if notifier_class.respond_to?(:turn_on) end ENV['GUARD_NOTIFY'] = 'true' end end
Generated with the Darkfish Rdoc Generator 2.