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


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

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


68
69
70
71
72
# File 'lib/guard/notifier.rb', line 68

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

.disconnectObject


21
22
23
24
# File 'lib/guard/notifier.rb', line 21

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

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


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/guard/notifier.rb', line 30

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


63
64
65
# File 'lib/guard/notifier.rb', line 63

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

.toggleObject


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/guard/notifier.rb', line 47

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


43
44
45
# File 'lib/guard/notifier.rb', line 43

def self.turn_on
  @notifier.turn_on
end