File: notifier.rb

package info (click to toggle)
ruby-guard 2.18.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,344 kB
  • sloc: ruby: 9,256; makefile: 6
file content (71 lines) | stat: -rw-r--r-- 1,752 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require "notiffany/notifier"
require "guard/ui"

module Guard
  class Notifier
    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

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

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

    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

    def self.turn_on
      @notifier.turn_on
    end

    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

    # Used by dsl describer
    def self.supported
      Notiffany::Notifier::SUPPORTED.inject(:merge)
    end

    # Used by dsl describer
    def self.detected
      @notifier.available.map do |mod|
        { name: mod.name.to_sym, options: mod.options }
      end
    end
  end
end