File: terminal_notifier.rb

package info (click to toggle)
ruby-notiffany 0.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 580 kB
  • sloc: ruby: 3,094; makefile: 10; sh: 4
file content (59 lines) | stat: -rw-r--r-- 1,829 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
require "notiffany/notifier/base"

module Notiffany
  class Notifier
    # System notifications using the
    #
    # [terminal-notifier](https://github.com/Springest/terminal-notifier-guard)
    #
    # gem.
    #
    # This gem is available for OS X 10.8 Mountain Lion and sends notifications
    # to the OS X notification center.
    class TerminalNotifier < Base
      DEFAULTS = { app_name: "Notiffany" }

      ERROR_ONLY_OSX10 = "The :terminal_notifier only runs"\
        " on Mac OS X 10.8 and later."

      def _supported_hosts
        %w(darwin)
      end

      def _gem_name
        "terminal-notifier-guard"
      end

      def _check_available(_opts = {})
        return if ::TerminalNotifier::Guard.available?
        fail UnavailableError, ERROR_ONLY_OSX10
      end

      # Shows a system notification.
      #
      # @param [String] message the notification message body
      # @param [Hash] opts additional notification library options
      # @option opts [String] type the notification type. Either 'success',
      #   'pending', 'failed' or 'notify'
      # @option opts [String] title the notification title
      # @option opts [String] image the path to the notification image (ignored)
      # @option opts [String] app_name name of your app
      # @option opts [String] execute a command
      # @option opts [String] activate an app bundle
      # @option opts [String] open some url or file
      #
      def _perform_notify(message, opts = {})
        title = [opts[:app_name], opts[:type].downcase.capitalize].join(" ")
        opts = {
          title: title
        }.merge(opts)
        opts[:message] = message
        opts[:title] ||= title
        opts.delete(:image)
        opts.delete(:app_name)

        ::TerminalNotifier::Guard.execute(false, opts)
      end
    end
  end
end