Class: Guard::Notifier::TerminalNotifier

Inherits:
Base
  • Object
show all
Defined in:
lib/guard/notifiers/terminal_notifier.rb

Overview

System notifications using the [terminal-notifier-guard](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.

Examples:

Add the `terminal-notifier-guard` gem to your `Gemfile`

group :development
  gem 'terminal-notifier-guard'
end

Add the `:terminal_notifier` notifier to your `Guardfile`

notification :terminal_notifier

Display application name as subtitle

notification :terminal_notifier, subtitle: “MyApp"

Use iTerm2 for notifications

notification :terminal_notifier, activate: "com.googlecode.iterm2"

Constant Summary

Constants inherited from Base

Base::HOSTS

Instance Attribute Summary

Attributes inherited from Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

_supported_host?, #images_path, #initialize, name, #name, #normalize_standard_options!, require_gem_safely, title, #title

Constructor Details

This class inherits a constructor from Guard::Notifier::Base

Class Method Details

._register!(opts) ⇒ Boolean

Detects if the terminal-notifier-guard gem is available and if not, displays an error message unless `opts` is true.

Returns:

  • (Boolean)

    whether or not the terminal-notifier-guard gem is available



72
73
74
75
76
77
78
79
80
81
# File 'lib/guard/notifiers/terminal_notifier.rb', line 72

def self._register!(opts)
  if ::TerminalNotifier::Guard.available?
    true
  else
    unless opts[:silent]
      ::Guard::UI.error 'The :terminal_notifier only runs on Mac OS X 10.8 and later.'
    end
    false
  end
end

.available?(opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/guard/notifiers/terminal_notifier.rb', line 37

def self.available?(opts = {})
  super and require_gem_safely(opts) and _register!(opts)
end

.gem_nameObject



33
34
35
# File 'lib/guard/notifiers/terminal_notifier.rb', line 33

def self.gem_name
  'terminal-notifier-guard'
end

.supported_hostsObject



29
30
31
# File 'lib/guard/notifiers/terminal_notifier.rb', line 29

def self.supported_hosts
  %w[darwin]
end

Instance Method Details

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

Shows a system notification.

Parameters:

  • message (String)

    the notification message body

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

    additional notification library options

Options Hash (opts):

  • type (String)

    the notification type. Either 'success', 'pending', 'failed' or 'notify'

  • title (String)

    the notification title

  • image (String)

    the path to the notification image (ignored)

  • app_name (String)

    name of your app

  • execute (String)

    a command

  • activate (String)

    an app bundle

  • open (String)

    some url or file



54
55
56
57
58
59
60
61
62
63
# File 'lib/guard/notifiers/terminal_notifier.rb', line 54

def notify(message, opts = {})
  title = opts[:title] || options[:title]
  super
  self.class.require_gem_safely

  opts.delete(:image)
  opts[:title] = title || [opts.delete(:app_name) { 'Guard' }, opts[:type].downcase.capitalize].join(' ')

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