lib/terminal-notifier-guard.rb in terminal-notifier-guard-1.6.0 vs lib/terminal-notifier-guard.rb in terminal-notifier-guard-1.6.1

- old
+ new

@@ -1,40 +1,42 @@ -%w(failed notify pending success).each do |type| - require File.expand_path("../terminal_notifier/guard/#{type}", __FILE__) -end - module TerminalNotifier module Guard - include TerminalNotifier::Guard::Notify - include TerminalNotifier::Guard::Success - include TerminalNotifier::Guard::Failed - include TerminalNotifier::Guard::Pending + VERSION = "1.6.1" + BIN_PATH = "/usr/local/Cellar/terminal-notifier/1.6.1/terminal-notifier.app/Contents/MacOS/terminal-notifier" + ICONS_PATH = File.expand_path("../../icons", __FILE__) + GUARD_ICON = File.join(ICONS_PATH, 'guard.png') + def self.osx_version + Gem::Version.new(`sw_vers -productVersion`.strip) + end + + 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 + end + # Returns wether or not the current platform is Mac OS X 10.8, or higher. def self.available? + deprecation_check if @available.nil? @available = `uname`.strip == 'Darwin' && - Gem::Version.new(`sw_vers -productVersion`.strip) >= Gem::Version.new('10.8') + osx_version >= Gem::Version.new('10.9') end @available end + # Whether or not the terminal notifier is installed + def self.installed? + return true if File.exist? BIN_PATH + end + def self.execute(verbose, options) - if available? - case options[:type] - when :failed - bin_path = TerminalNotifier::Guard::Failed::BIN_PATH - when :success - bin_path = TerminalNotifier::Guard::Success::BIN_PATH - when :pending - bin_path = TerminalNotifier::Guard::Pending::BIN_PATH - else - bin_path = TerminalNotifier::Guard::Notify::BIN_PATH - end - options.delete(:type) if options[:type] + if available? && installed? + options.merge!({ :appIcon => GUARD_ICON, :contentImage => icon(options.delete(:type)) }) - command = [bin_path, *options.map { |k,v| ["-#{k}", v.to_s] }.flatten] + command = [BIN_PATH, *options.map { |k,v| ["-#{k}", v.to_s] }.flatten] if RUBY_VERSION < '1.9' require 'shellwords' command = Shellwords.shelljoin(command) end result = '' @@ -43,11 +45,12 @@ STDOUT.print output if verbose result << output end result else - raise "terminal-notifier is only supported on Mac OS X 10.8, or higher." + 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 # Sends a User Notification and returns wether or not it was a success. # @@ -56,16 +59,16 @@ # # 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"') + # 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. def notify(message, options = {}, verbose = false) TerminalNotifier::Guard.execute(verbose, options.merge(:message => message)) $?.success? @@ -87,9 +90,16 @@ def success(message, options = {}, verbose = false) TerminalNotifier::Guard.execute(verbose, options.merge(:message => message, :type => :success)) $?.success? end module_function :success + + def icon(type = :notify) + type ||= :notify + file_name = "#{type}.icns".capitalize + File.join(ICONS_PATH, file_name) + end + module_function :icon # 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.