Module: TerminalNotifier::Guard
- Defined in:
- lib/terminal-notifier-guard.rb
Constant Summary collapse
- VERSION =
"1.7.0"- ICONS_PATH =
File.("../../icons", __FILE__)
- GUARD_ICON =
File.join(ICONS_PATH, 'Guard.icns')
- 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.
- .bin_path ⇒ Object
- .deprecation_check ⇒ Object
- .execute(verbose, options) ⇒ Object
- .failed(message, options = {}, verbose = false) ⇒ Object
- .icon(type = :notify) ⇒ Object
-
.installed? ⇒ Boolean
Whether or not the terminal notifier is installed.
-
.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.
- .osx_version ⇒ Object
- .pending(message, options = {}, verbose = false) ⇒ Object
-
.remove(group = 'ALL', verbose = false) ⇒ Object
Removes a notification that was previously sent with the specified ‘group’ ID, if one exists.
- .success(message, options = {}, verbose = false) ⇒ Object
- .terminal_notifier_version ⇒ Object
Class Method Details
.available? ⇒ Boolean
Returns wether or not the current platform is Mac OS X 10.8, or higher.
30 31 32 33 34 35 36 37 |
# File 'lib/terminal-notifier-guard.rb', line 30 def self.available? deprecation_check if @available.nil? @available = `uname`.strip == 'Darwin' && osx_version >= Gem::Version.new('10.9') end @available end |
.bin_path ⇒ Object
44 45 46 47 48 |
# File 'lib/terminal-notifier-guard.rb', line 44 def self.bin_path ENV["TERMINAL_NOTIFIER_BIN"] || begin @@binary ||= `which terminal-notifier`.chomp end end |
.deprecation_check ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/terminal-notifier-guard.rb', line 19 def self.deprecation_check if osx_version <= Gem::Version.new('10.8') raise "OSX 10.8 is no longer supported by this gem. Please revert to version <= 1.5.3." end if terminal_notifier_version < Gem::Version.new('1.6.0') puts "Notice: Your terminal-notifier is older than what terminal-notifier-guard supports, consider upgrading." end end |
.execute(verbose, options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/terminal-notifier-guard.rb', line 50 def self.execute(verbose, ) if available? && installed? .merge!({ :contentImage=> GUARD_ICON, :appIcon => icon(.delete(:type)) }) 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." if !available? raise "TerminalNotifier not installed. Please do so by running `brew install terminal-notifier`" if !installed? end end |
.failed(message, options = {}, verbose = false) ⇒ Object
95 96 97 98 |
# File 'lib/terminal-notifier-guard.rb', line 95 def failed(, = {}, verbose = false) TerminalNotifier::Guard.execute(verbose, .merge(:message => , :type => :failed)) $?.success? end |
.icon(type = :notify) ⇒ Object
113 114 115 116 117 |
# File 'lib/terminal-notifier-guard.rb', line 113 def icon(type = :notify) type ||= :notify file_name = "#{type}.icns".capitalize File.join(ICONS_PATH, file_name) end |
.installed? ⇒ Boolean
Whether or not the terminal notifier is installed
40 41 42 |
# File 'lib/terminal-notifier-guard.rb', line 40 def self.installed? File.exist? bin_path 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`.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/terminal-notifier-guard.rb', line 139 def list(group = 'ALL', verbose = false) output = TerminalNotifier::Guard.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::Guard.notify('Hello World')
TerminalNotifier::Guard.notify('Hello World', :title => 'Ruby')
TerminalNotifier::Guard.notify('Hello World', :group => Process.pid)
TerminalNotifier::Guard.notify('Hello World', :activate => 'com.apple.Safari')
TerminalNotifier::Guard.notify('Hello World', :open => 'http://twitter.com/alloy')
TerminalNotifier::Guard.notify('Hello World', :execute => 'say "OMG"')
Raises if not supported on the current platform.
89 90 91 92 |
# File 'lib/terminal-notifier-guard.rb', line 89 def notify(, = {}, verbose = false) TerminalNotifier::Guard.execute(verbose, .merge(:message => )) $?.success? end |
.osx_version ⇒ Object
7 8 9 |
# File 'lib/terminal-notifier-guard.rb', line 7 def self.osx_version Gem::Version.new(`sw_vers -productVersion`.strip) end |
.pending(message, options = {}, verbose = false) ⇒ Object
101 102 103 104 |
# File 'lib/terminal-notifier-guard.rb', line 101 def pending(, = {}, verbose = false) TerminalNotifier::Guard.execute(verbose, .merge(:message => , :type => :pending)) $?.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.
124 125 126 127 |
# File 'lib/terminal-notifier-guard.rb', line 124 def remove(group = 'ALL', verbose = false) TerminalNotifier::Guard.execute(verbose, :remove => group) $?.success? end |
.success(message, options = {}, verbose = false) ⇒ Object
107 108 109 110 |
# File 'lib/terminal-notifier-guard.rb', line 107 def success(, = {}, verbose = false) TerminalNotifier::Guard.execute(verbose, .merge(:message => , :type => :success)) $?.success? end |
.terminal_notifier_version ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/terminal-notifier-guard.rb', line 11 def self.terminal_notifier_version return Gem::Version("0.0.0") unless installed? # invoke the help option since the binary otherwise may get stuck Gem::Version.new(`#{bin_path} -help`.lines.first.match(/\d\.\d\.\d/)[0]) rescue Gem::Version.new("0.0.0") end |