Module: Growl

Defined in:
lib/growl/growl.rb,
lib/growl/version.rb

Defined Under Namespace

Classes: Base, Error

Constant Summary collapse

BIN =
'growlnotify'
VERSION =
'1.0.3'

Class Method Summary collapse

Class Method Details

.exec(*args) ⇒ Object

Execute args against the binary.



54
55
56
# File 'lib/growl/growl.rb', line 54

def self.exec *args
  Kernel.system BIN, *args
end

.installed?Boolean

Check if the binary is installed and accessable.

Returns:

  • (Boolean)


68
69
70
# File 'lib/growl/growl.rb', line 68

def self.installed?
  version rescue false
end

.new(*args, &block) ⇒ Object

Return an instance of Growl::Base or nil when not installed.



75
76
77
78
# File 'lib/growl/growl.rb', line 75

def self.new *args, &block
  return unless installed?
  Base.new *args, &block
end

.normalize_icon!(options = {}) ⇒ Object

Normalize the icon option in options. This performs the following operations in order to allow for the :icon key to work with a variety of values:

  • path to an icon sets :iconpath

  • path to an image sets :image

  • capitalized word sets :appIcon

  • filename uses extname as :icon

  • otherwise treated as :icon



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/growl/growl.rb', line 91

def self.normalize_icon! options = {}
  return unless options.include? :icon
  icon = options.delete(:icon).to_s
  if File.exists? icon
    if File.extname(icon) == '.icns'
      options[:iconpath] = icon
    else
      options[:image] = icon
    end
  else
    if icon.capitalize == icon
      options[:appIcon] = icon
    elsif !(ext = File.extname(icon)).empty?
      options[:icon] = ext[1..-1]
    else
      options[:icon] = icon
    end
  end
end

.notify(message = nil, options = {}, &block) ⇒ Object

Display a growl notification message, with options documented below. Alternatively a block may be passed which is then instance evaluated or yielded to the block.

This method is simply returns nil when growlnotify is not installed, as growl notifications should never be the only means of communication between your application and your user.

Examples

Growl.notify 'Hello'
Growl.notify 'Hello', :title => 'TJ Says:', :sticky => true
Growl.notify { |n| n.message = 'Hello'; n.sticky! }
Growl.notify { self.message = 'Hello'; sticky! }


30
31
32
33
34
35
# File 'lib/growl/growl.rb', line 30

def notify message = nil, options = {}, &block
  return unless Growl.installed?
  options.merge! :message => message if message
  Growl.normalize_icon! options
  Growl.new(options, &block).run
end

.versionObject

Return the version triple of the binary.



61
62
63
# File 'lib/growl/growl.rb', line 61

def self.version
  @version ||= `#{BIN} --version`.split[1]
end