Sha256: 3092e895e25df803215b2f2a0cf61850df84fc35d2c19775103f120542d061fd

Contents?: true

Size: 1.7 KB

Versions: 23

Compression:

Stored size: 1.7 KB

Contents

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.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

Version data entries

23 entries across 23 versions & 8 rubygems

Version Path
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/guard-2.15.0/lib/guard/notifier.rb
alimentos-alu0100945645-0.1.0 vendor/bundle/ruby/2.3.0/gems/guard-2.15.0/lib/guard/notifier.rb
alimentos-alu0100945645-1.0.0 vendor/bundle/ruby/2.3.0/gems/guard-2.15.0/lib/guard/notifier.rb
guard-2.15.0 lib/guard/notifier.rb
guard-2.14.2 lib/guard/notifier.rb
comiditaULL-0.1.1 vendor/bundle/ruby/2.3.0/gems/guard-2.14.1/lib/guard/notifier.rb
comidita_ull-0.1.1 vendor/bundle/ruby/2.3.0/gems/guard-2.14.1/lib/guard/notifier.rb
comidita_ull-0.1.0 vendor/bundle/ruby/2.3.0/gems/guard-2.14.1/lib/guard/notifier.rb
guard-2.14.1 lib/guard/notifier.rb
gameboard-3.1.0 vendor/bundle/ruby/2.3.0/gems/guard-2.14.0/lib/guard/notifier.rb
gameboard-3.0.0 vendor/bundle/ruby/2.3.0/gems/guard-2.14.0/lib/guard/notifier.rb
guard-2.14.0 lib/guard/notifier.rb
mastermind_adeybee-0.1.4 vendor/bundle/ruby/2.2.0/gems/guard-2.13.0/lib/guard/notifier.rb
mastermind_adeybee-0.1.3 vendor/bundle/ruby/2.2.0/gems/guard-2.13.0/lib/guard/notifier.rb
mastermind_adeybee-0.1.2 vendor/bundle/ruby/2.2.0/gems/guard-2.13.0/lib/guard/notifier.rb
mastermind_adeybee-0.1.1 vendor/bundle/ruby/2.2.0/gems/guard-2.13.0/lib/guard/notifier.rb
geminfo-0.1.0 path/gems/guard-2.13.0/lib/guard/notifier.rb
guard-2.13.0 lib/guard/notifier.rb
guard-2.12.9 lib/guard/notifier.rb
guard-2.12.8 lib/guard/notifier.rb