Class: Notiffany::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/notiffany/notifier.rb,
lib/notiffany/notifier/base.rb,
lib/notiffany/notifier/file.rb,
lib/notiffany/notifier/gntp.rb,
lib/notiffany/notifier/tmux.rb,
lib/notiffany/notifier/emacs.rb,
lib/notiffany/notifier/growl.rb,
lib/notiffany/notifier/config.rb,
lib/notiffany/notifier/detected.rb,
lib/notiffany/notifier/libnotify.rb,
lib/notiffany/notifier/rb_notifu.rb,
lib/notiffany/notifier/notifysend.rb,
lib/notiffany/notifier/tmux/client.rb,
lib/notiffany/notifier/emacs/client.rb,
lib/notiffany/notifier/tmux/session.rb,
lib/notiffany/notifier/terminal_title.rb,
lib/notiffany/notifier/terminal_notifier.rb,
lib/notiffany/notifier/tmux/notification.rb

Defined Under Namespace

Classes: Base, Config, Detected, Emacs, File, GNTP, Growl, Libnotify, NotServer, Notifu, NotifySend, TerminalNotifier, TerminalTitle, Tmux

Constant Summary collapse

NOTIFICATIONS_DISABLED =
"Notifications disabled by GUARD_NOTIFY" \
" environment variable"
USING_NOTIFIER =
"Notiffany is using %s to send notifications."
ONLY_NOTIFY =
"Only notify() is available from a child process"
SUPPORTED =

List of available notifiers, grouped by functionality

[
  {
    gntp: GNTP,
    growl: Growl,
    terminal_notifier: TerminalNotifier,
    libnotify: Libnotify,
    notifysend: NotifySend,
    notifu: Notifu
  },
  { emacs: Emacs },
  { tmux: Tmux },
  { terminal_title: TerminalTitle },
  { file: File }
]
Env =
Nenv::Builder.build do
  create_method(:notify?) { |data| data != "false" }
  create_method(:notify_pid) { |data| data && Integer(data) }
  create_method(:notify_pid=)
  create_method(:notify_active?)
  create_method(:notify_active=)
end
YamlEnvStorage =

TODO: use a socket instead of passing env variables to child processes (currently probably only used by guard-cucumber anyway)

Nenv::Builder.build do
  create_method(:notifiers=) { |data| YAML.dump(data || []) }
  create_method(:notifiers) { |data| data ? YAML.load(data) : [] }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Notifier

Returns a new instance of Notifier.



82
83
84
85
86
87
88
89
90
# File 'lib/notiffany/notifier.rb', line 82

def initialize(opts)
  @config = Config.new(opts)
  @detected = Detected.new(SUPPORTED, config.env_namespace, config.logger)
  return if _client?

  _activate
rescue Detected::NoneAvailableError => e
  config.logger.info e.to_s
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



80
81
82
# File 'lib/notiffany/notifier.rb', line 80

def config
  @config
end

Instance Method Details

#active?Boolean

Test if notifiers are currently turned on

Returns:

  • (Boolean)


138
139
140
# File 'lib/notiffany/notifier.rb', line 138

def active?
  _env.notify_active?
end

#availableObject



160
161
162
# File 'lib/notiffany/notifier.rb', line 160

def available
  @detected.available
end

#disconnectObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/notiffany/notifier.rb', line 92

def disconnect
  if _client?
    @detected = nil
    return
  end

  turn_off if active?
  @detected.reset unless @detected.nil?
  _env.notify_pid = nil
  @detected = nil
end

#enabled?Boolean

Test if the notifications can be enabled based on ENV

Returns:

  • (Boolean)


133
134
135
# File 'lib/notiffany/notifier.rb', line 133

def enabled?
  _env.notify?
end

#notify(message, message_opts = {}) ⇒ Object

Show a system notification with all configured notifiers.

Parameters:

  • message (String)

    the message to show

  • opts (Hash)

    a customizable set of options



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/notiffany/notifier.rb', line 148

def notify(message, message_opts = {})
  if _client?
    return unless enabled?
  else
    return unless active?
  end

  @detected.available.each do |notifier|
    notifier.notify(message, message_opts.dup)
  end
end

#turn_offObject

Turn notifications off.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/notiffany/notifier.rb', line 120

def turn_off
  _check_server!

  fail "Not active!" unless active?

  @detected.available.each do |obj|
    obj.turn_off if obj.respond_to?(:turn_off)
  end

  _env.notify_active = false
end

#turn_on(options = {}) ⇒ Object

Turn notifications on.

Parameters:

  • options (Hash) (defaults to: {})

    the turn_on options

Options Hash (options):

  • silent (Boolean)

    disable any logging



109
110
111
112
113
114
115
116
117
# File 'lib/notiffany/notifier.rb', line 109

def turn_on(options = {})
  _check_server!
  return unless enabled?

  fail "Already active!" if active?

  _turn_on_notifiers(options)
  _env.notify_active = true
end