module Guard::Notifier

def add_notification(name, options = { }, silent = false)

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

Parameters:
  • options (Hash) -- the notifier options
  • silent (Boolean) -- disable any error message
  • name (Symbol) -- the name of the notifier to use
def add_notification(name, options = { }, silent = false)
  return turn_off if name == :off
  if NOTIFIERS.has_key?(name) && NOTIFIERS[name].available?(silent)
    self.notifications = notifications << { :name => name, :options => options }
    true
  else
    false
  end
end

def auto_detect_notification


is available.
the list of supported notification gems and picks the first that
Auto detect the available notification library. This goes through
def auto_detect_notification
  available = [:growl_notify, :gntp, :growl, :libnotify, :notifu].any? { |notifier| add_notification(notifier, { }, true) }
  ::Guard::UI.info('Guard could not detect any of the supported notification libraries.') unless available
end

def clear_notifications


Clear available notifications.
def clear_notifications
  ENV['GUARD_NOTIFICATIONS'] = nil
end

def enabled?

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

def image_path(image)

Returns:
  • (String) - the image path

Parameters:
  • image (Symbol, String) -- the image symbol or path to an image
def image_path(image)
  case image
  when :failed
    images_path.join('failed.png').to_s
  when :pending
    images_path.join('pending.png').to_s
  when :success
    images_path.join('success.png').to_s
  else
    image
  end
end

def images_path

Returns:
  • (Pathname) - the path to the images directory
def images_path
  @images_path ||= Pathname.new(File.dirname(__FILE__)).join('../../images')
end

def notification_type(image)

Returns:
  • (String) - the notification type

Parameters:
  • image (Symbol, String) -- the image symbol or path to an image
def notification_type(image)
  case image
  when :failed
    'failed'
  when :pending
    'pending'
  when :success
    'success'
  else
    'notify'
  end
end

def notifications

Returns:
  • (Hash) - the notifications
def notifications
  ENV['GUARD_NOTIFICATIONS'] ? YAML::load(ENV['GUARD_NOTIFICATIONS']) : []
end

def notifications=(notifications)

Parameters:
  • notifications (Array) -- the notifications
def notifications=(notifications)
  ENV['GUARD_NOTIFICATIONS'] = YAML::dump(notifications)
end

def notify(message, options = { })

Options Hash: (**options)
  • 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, options = { })
  if enabled?
    type  = notification_type(options[:image] || :success)
    image = image_path(options.delete(:image) || :success)
    title = options.delete(:title) || 'Guard'
    notifications.each do |notification|
      begin
        NOTIFIERS[notification[:name]].notify(type, title, message, image, options.merge(notification[:options]))
      rescue Exception => e
        ::Guard::UI.error "Error sending notification with #{ notification[:name] }: #{ e.message }"
      end
    end
  end
end

def turn_off


Turn notifications off.
def turn_off
  ENV['GUARD_NOTIFY'] = 'false'
end

def turn_on


library.
in the `Guardfile` Guard auto detects the first available
Turn notifications on. If no notifications are defined
def turn_on
  auto_detect_notification if notifications.empty? && (!::Guard.options || ::Guard.options[:notify])
  if notifications.empty?
    ENV['GUARD_NOTIFY'] = 'false'
  else
    notifications.each do |notification|
      ::Guard::UI.info "Guard uses #{ NOTIFIERS[notification[:name]].to_s.split('::').last } to send notifications."
    end
    ENV['GUARD_NOTIFY'] = 'true'
  end
end