class Notiffany::Notifier

Constants

Env
NOTIFICATIONS_DISABLED
ONLY_NOTIFY
SUPPORTED

List of available notifiers, grouped by functionality

USING_NOTIFIER
YamlEnvStorage

TODO: use a socket instead of passing env variables to child processes (currently probably only used by guard-cucumber anyway)

Attributes

config[R]

Public Class Methods

new(opts) click to toggle source
# File lib/notiffany/notifier.rb, line 82
def initialize(opts)
  @config = Config.new(opts)
  @detected = Detected.new(SUPPORTED, config.env_namespace, config.logger)
  return if _client?

  _activate
rescue Detected::NoneAvailableError => e
  config.logger.info e.to_s
end

Public Instance Methods

active?() click to toggle source

Test if notifiers are currently turned on

# File lib/notiffany/notifier.rb, line 138
def active?
  _env.notify_active?
end
available() click to toggle source
# File lib/notiffany/notifier.rb, line 160
def available
  @detected.available
end
disconnect() click to toggle source
# File lib/notiffany/notifier.rb, line 92
def disconnect
  if _client?
    @detected = nil
    return
  end

  turn_off if active?
  @detected.reset unless @detected.nil?
  _env.notify_pid = nil
  @detected = nil
end
enabled?() click to toggle source

Test if the notifications can be enabled based on ENV

# File lib/notiffany/notifier.rb, line 133
def enabled?
  _env.notify?
end
notify(message, message_opts = {}) click to toggle source

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/notiffany/notifier.rb, line 148
def notify(message, message_opts = {})
  if _client?
    return unless enabled?
  else
    return unless active?
  end

  @detected.available.each do |notifier|
    notifier.notify(message, message_opts.dup)
  end
end
turn_off() click to toggle source

Turn notifications off.

# File lib/notiffany/notifier.rb, line 120
def turn_off
  _check_server!

  fail "Not active!" unless active?

  @detected.available.each do |obj|
    obj.turn_off if obj.respond_to?(:turn_off)
  end

  _env.notify_active = false
end
turn_on(options = {}) click to toggle source

Turn notifications on.

@param [Hash] options the turn_on options @option options [Boolean] silent disable any logging

# File lib/notiffany/notifier.rb, line 109
def turn_on(options = {})
  _check_server!
  return unless enabled?

  fail "Already active!" if active?

  _turn_on_notifiers(options)
  _env.notify_active = true
end

Private Instance Methods

_activate() click to toggle source
# File lib/notiffany/notifier.rb, line 191
def _activate
  _env.notify_pid = $$

  fail "Already connected" if active?

  return unless _notification_wanted?

  _detect_or_add_notifiers
  turn_on
end
_check_server!() click to toggle source
# File lib/notiffany/notifier.rb, line 170
def _check_server!
  _client? && fail(NotServer, ONLY_NOTIFY)
end
_client?() click to toggle source
# File lib/notiffany/notifier.rb, line 174
def _client?
  (pid = _env.notify_pid) && (pid != $$)
end
_detect_or_add_notifiers() click to toggle source
# File lib/notiffany/notifier.rb, line 178
def _detect_or_add_notifiers
  notifiers = config.notifiers
  return @detected.detect if notifiers.empty?

  notifiers.each do |name, notifier_options|
    @detected.add(name, notifier_options)
  end
end
_env() click to toggle source
# File lib/notiffany/notifier.rb, line 166
def _env
  @environment ||= Env.new(config.env_namespace)
end
_notification_wanted?() click to toggle source
# File lib/notiffany/notifier.rb, line 187
def _notification_wanted?
  enabled? && config.notify?
end
_turn_on_notifiers(options) click to toggle source
# File lib/notiffany/notifier.rb, line 202
def _turn_on_notifiers(options)
  silent = options[:silent]
  @detected.available.each do |obj|
    config.logger.debug(format(USING_NOTIFIER, obj.title)) unless silent
    obj.turn_on if obj.respond_to?(:turn_on)
  end
end