Class: Guard::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/notifier.rb

Constant Summary collapse

DEPRECATED_IMPLICIT_CONNECT =
"Calling Notiffany::Notifier.notify()"\
" without a prior Notifier.connect() is"\
" deprecated"

Class Method Summary collapse

Class Method Details

.connect(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/guard/notifier.rb', line 6

def self.connect(options = {})
  @notifier ||= nil
  fail "Already connected!" if @notifier
  begin
    opts = options.merge(namespace: "guard", logger: UI)
    @notifier = Notiffany.connect(opts)
  rescue Notiffany::Notifier::Detected::UnknownNotifier => e
    UI.error "Failed to setup notification: #{e.message}"
    fail
  end
end

.detectedObject

Used by dsl describer



65
66
67
68
69
# File 'lib/guard/notifier.rb', line 65

def self.detected
  @notifier.available.map do |mod|
    { name: mod.name.to_sym, options: mod.options }
  end
end

.disconnectObject



18
19
20
21
# File 'lib/guard/notifier.rb', line 18

def self.disconnect
  @notifier && @notifier.disconnect
  @notifier = nil
end

.notify(message, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/guard/notifier.rb', line 27

def self.notify(message, options = {})
  unless @notifier
    # TODO: reenable again?
    # UI.deprecation(DEPRECTED_IMPLICIT_CONNECT)
    connect(notify: true)
  end

  @notifier.notify(message, options)
rescue RuntimeError => e
  UI.error "Notification failed for #{@notifier.class.name}: #{e.message}"
  UI.debug e.backtrace.join("\n")
end

.supportedObject

Used by dsl describer



60
61
62
# File 'lib/guard/notifier.rb', line 60

def self.supported
  Notiffany::Notifier::SUPPORTED.inject(:merge)
end

.toggleObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/guard/notifier.rb', line 44

def self.toggle
  unless @notifier.enabled?
    UI.error NOTIFICATIONS_DISABLED
    return
  end

  if @notifier.active?
    UI.info "Turn off notifications"
    @notifier.turn_off
    return
  end

  @notifier.turn_on
end

.turn_onObject



40
41
42
# File 'lib/guard/notifier.rb', line 40

def self.turn_on
  @notifier.turn_on
end