Module: TerminalNotifier
- Defined in:
- lib/terminal-notifier.rb
Constant Summary collapse
- BIN_PATH =
File.('../../vendor/terminal-notifier/terminal-notifier.app/Contents/MacOS/terminal-notifier', __FILE__)
- LIST_FIELDS =
[:group, :title, :subtitle, :message, :delivered_at].freeze
Class Method Summary collapse
-
.available? ⇒ Boolean
Returns wether or not the current platform is Mac OS X 10.8, or higher.
- .execute(verbose, options) ⇒ Object
-
.list(group = 'ALL', verbose = false) ⇒ Object
If a ‘group’ ID is given, and a notification for that group exists, returns a hash with details about the notification.
-
.notify(message, options = {}, verbose = false) ⇒ Object
Sends a User Notification and returns wether or not it was a success.
-
.remove(group = 'ALL', verbose = false) ⇒ Object
Removes a notification that was previously sent with the specified ‘group’ ID, if one exists.
Class Method Details
.available? ⇒ Boolean
Returns wether or not the current platform is Mac OS X 10.8, or higher.
5 6 7 8 9 10 |
# File 'lib/terminal-notifier.rb', line 5 def self.available? if @available.nil? @available = `uname`.strip == 'Darwin' && `sw_vers -productVersion`.strip >= '10.8' end @available end |
.execute(verbose, options) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/terminal-notifier.rb', line 12 def self.execute(verbose, ) if available? command = [BIN_PATH, *.map { |k,v| ["-#{k}", v.to_s] }.flatten] if RUBY_VERSION < '1.9' require 'shellwords' command = Shellwords.shelljoin(command) end result = '' IO.popen(command) do |stdout| output = stdout.read STDOUT.print output if verbose result << output end result else raise "terminal-notifier is only supported on Mac OS X 10.8, or higher." end end |
.list(group = 'ALL', verbose = false) ⇒ Object
If a ‘group’ ID is given, and a notification for that group exists, returns a hash with details about the notification.
If no ‘group’ ID is given, an array of hashes describing all notifications.
If no information is available this will return ‘nil`.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/terminal-notifier.rb', line 73 def list(group = 'ALL', verbose = false) output = TerminalNotifier.execute(verbose, :list => group) return if output.strip.empty? require 'time' notifications = output.split("\n")[1..-1].map do |line| LIST_FIELDS.zip(line.split("\t")).inject({}) do |hash, (key, value)| hash[key] = key == :delivered_at ? Time.parse(value) : (value unless value == '(null)') hash end end group == 'ALL' ? notifications : notifications.first end |
.notify(message, options = {}, verbose = false) ⇒ Object
Sends a User Notification and returns wether or not it was a success.
The available options are ‘:title`, `:group`, `:activate`, `:open`, and `:execute`. For a description of each option see:
https://github.com/alloy/terminal-notifier/blob/master/README.markdown
Examples are:
TerminalNotifier.notify('Hello World')
TerminalNotifier.notify('Hello World', :title => 'Ruby')
TerminalNotifier.notify('Hello World', :group => Process.pid)
TerminalNotifier.notify('Hello World', :activate => 'com.apple.Safari')
TerminalNotifier.notify('Hello World', :open => 'http://twitter.com/alloy')
TerminalNotifier.notify('Hello World', :execute => 'say "OMG"')
Raises if not supported on the current platform.
48 49 50 51 |
# File 'lib/terminal-notifier.rb', line 48 def notify(, = {}, verbose = false) TerminalNotifier.execute(verbose, .merge(:message => )) $?.success? end |
.remove(group = 'ALL', verbose = false) ⇒ Object
Removes a notification that was previously sent with the specified ‘group’ ID, if one exists.
If no ‘group’ ID is given, all notifications are removed.
58 59 60 61 |
# File 'lib/terminal-notifier.rb', line 58 def remove(group = 'ALL', verbose = false) TerminalNotifier.execute(verbose, :remove => group) $?.success? end |