class Notiffany::Notifier::Detected

@private api

Constants

NO_SUPPORTED_NOTIFIERS

Public Class Methods

new(supported, env_namespace, logger) click to toggle source
# File lib/notiffany/notifier/detected.rb, line 47
def initialize(supported, env_namespace, logger)
  @supported = supported
  @environment = YamlEnvStorage.new(env_namespace)
  @logger = logger
end

Public Instance Methods

add(name, opts) click to toggle source

Called when user has notifier-specific config. Honor the config by warning if something is wrong

# File lib/notiffany/notifier/detected.rb, line 82
def add(name, opts)
  _add(name, opts)
rescue Notifier::Base::UnavailableError => e
  @logger.warning("Notiffany: #{name} not available (#{e.message}).")
end
available() click to toggle source
# File lib/notiffany/notifier/detected.rb, line 74
def available
  @available ||= _notifiers.map do |entry|
    _to_module(entry[:name]).new(entry[:options])
  end
end
detect() click to toggle source
# File lib/notiffany/notifier/detected.rb, line 57
def detect
  return unless _notifiers.empty?
  @supported.each do |group|
    group.detect do |name, _|
      begin
        _add(name, {})
        true
      rescue Notifier::Base::UnavailableError => e
        @logger.debug "Notiffany: #{name} not available (#{e.message})."
        false
      end
    end
  end

  fail NoneAvailableError, NO_SUPPORTED_NOTIFIERS if _notifiers.empty?
end
reset() click to toggle source
# File lib/notiffany/notifier/detected.rb, line 53
def reset
  @environment.notifiers = []
end

Private Instance Methods

_add(name, opts) click to toggle source
# File lib/notiffany/notifier/detected.rb, line 90
def _add(name, opts)
  @available = nil
  all = _notifiers

  # Silently skip if it's already available, because otherwise
  # we'd have to do :turn_off, then configure, then :turn_on
  names = all.map(&:first).map(&:last)
  unless names.include?(name)
    fail UnknownNotifier, name unless (klass = _to_module(name))

    klass.new(opts) # raises if unavailable
    @environment.notifiers = all << { name: name, options: opts }
  end

  # Just overwrite the options (without turning the notifier off or on),
  # so those options will be passed in next calls to notify()
  all.each { |item| item[:options] = opts if item[:name] == name }
end
_notifiers() click to toggle source
# File lib/notiffany/notifier/detected.rb, line 117
def _notifiers
  @environment.notifiers
end
_to_module(name) click to toggle source
# File lib/notiffany/notifier/detected.rb, line 109
def _to_module(name)
  @supported.each do |group|
    next unless (notifier = group.detect { |n, _| n == name })
    return notifier.last
  end
  nil
end