module Guard::Notifier

def _auto_detect_notification


is available in each notification group.
the list of supported notification gems and picks the first that
Auto detect the available notification library. This goes through
def _auto_detect_notification
  self.notifiers = []
  available = nil
  NOTIFIERS.each do |group|
    notifier_added = group.find { |name, klass| add_notifier(name, silent: true) }
    available ||= notifier_added
  end
  ::Guard::UI.info('Guard could not detect any of the supported notification libraries.') unless available
end

def _get_notifier_module(name)

Returns:
  • (Module) - the notifier module

Parameters:
  • name (Symbol) -- the notifier name
def _get_notifier_module(name)
  NOTIFIERS.each do |group|
    if notifier = group.find { |n, _| n == name }
      return notifier.last
    end
  end
  nil
end

def add_notifier(name, opts = {})

Returns:
  • (Boolean) - if the notification could be added

Options Hash: (**options)
  • silent (String) -- disable any error message

Parameters:
  • options (Hash) -- the notifier options
  • name (Symbol) -- the name of the notifier to use
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

def clear_notifiers


Clear available notifications.
def clear_notifiers
  ENV['GUARD_NOTIFIERS'] = nil
end

def enabled?

Returns:
  • (Boolean) - whether the notifications are on
def enabled?
  ENV['GUARD_NOTIFY'] == 'true'
end

def notifiers

def notifiers
  ENV['GUARD_NOTIFIERS'] ? YAML::load(ENV['GUARD_NOTIFIERS']) : []
end

def notifiers=(notifiers)

def notifiers=(notifiers)
  ENV['GUARD_NOTIFIERS'] = YAML::dump(notifiers)
end

def notify(message, opts = {})

Options Hash: (**opts)
  • title (String) -- the notification title
  • image (Symbol, String) -- the image symbol or path to an image

Parameters:
  • message (String) -- the message to show
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)
    rescue Exception => e
      ::Guard::UI.error "Error sending notification with #{ notifier.name }: #{ e.message }"
    end
  end
end

def toggle


Toggle the system notifications on/off
def toggle
  if enabled?
    ::Guard::UI.info 'Turn off notifications'
    turn_off
  else
    turn_on
  end
end

def turn_off


Turn notifications off.
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

def turn_on


Guard auto detects the first available library.
Turn notifications on. If no notifications are defined in the `Guardfile`
def turn_on
  _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."
      notifier_class.turn_on if notifier_class.respond_to?(:turn_on)
    end
    ENV['GUARD_NOTIFY'] = 'true'
  end
end