File: gntp.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 (92 lines) | stat: -rw-r--r-- 2,816 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require "notiffany/notifier/base"

module Notiffany
  class Notifier
    # System notifications using the
    # [ruby_gntp](https://github.com/snaka/ruby_gntp) gem.
    #
    # This gem is available for OS X, Linux and Windows and sends system
    # notifications to the following system notification frameworks through the
    #
    # [Growl Network Transport
    # Protocol](http://www.growlforwindows.com/gfw/help/gntp.aspx):
    #
    # * [Growl](http://growl.info)
    # * [Growl for Windows](http://www.growlforwindows.com)
    # * [Growl for Linux](http://mattn.github.com/growl-for-linux)
    # * [Snarl](https://sites.google.com/site/snarlapp)
    class GNTP < Base
      DEFAULTS = {
        sticky: false
      }

      # Default options for the ruby gtnp client.
      CLIENT_DEFAULTS = {
        host:     "127.0.0.1",
        password: "",
        port:     23_053
      }

      def _supported_hosts
        %w(darwin linux linux-gnu freebsd openbsd sunos solaris mswin mingw
           cygwin)
      end

      def _gem_name
        "ruby_gntp"
      end

      def _check_available(_opts)
      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
      # @option opts [String] host the hostname or IP address to which to send
      #   a remote notification
      # @option opts [String] password the password used for remote
      #   notifications
      # @option opts [Integer] port the port to send a remote notification
      # @option opts [Boolean] sticky make the notification sticky
      #
      def _perform_notify(message, opts = {})
        opts = {
          name: opts[:type].to_s,
          text: message,
          icon: opts[:image]
        }.merge(opts)

        _gntp_client(opts).notify(opts)
      end

      private

      def _gntp_client(opts = {})
        @_client ||= begin
          gntp = ::GNTP.new(
            "Notiffany",
            opts.fetch(:host) { CLIENT_DEFAULTS[:host] },
            opts.fetch(:password) { CLIENT_DEFAULTS[:password] },
            opts.fetch(:port) { CLIENT_DEFAULTS[:port] }
          )

          gntp.register(
            app_icon: _image_path(:guard),
            notifications: [
              { name: "notify", enabled: true },
              { name: "failed", enabled: true },
              { name: "pending", enabled: true },
              { name: "success", enabled: true }
            ]
          )
          gntp
        end
      end
    end
  end
end