diff --git a/Ruby/README.markdown b/Ruby/README.markdown index 5f11fca..24e2e85 100644 --- a/Ruby/README.markdown +++ b/Ruby/README.markdown @@ -25,7 +25,11 @@ 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.notify('Hello World', :group => Process.pid) -TerminalNotifier.remove('previous Process.pid') + +TerminalNotifier.remove(Process.pid) + +TerminalNotifier.list(Process.pid) +TerminalNotifier.list ``` diff --git a/Ruby/lib/terminal-notifier.rb b/Ruby/lib/terminal-notifier.rb index 40512ba..a9f65c8 100644 --- a/Ruby/lib/terminal-notifier.rb +++ b/Ruby/lib/terminal-notifier.rb @@ -69,4 +69,34 @@ def verbose_remove(group) TerminalNotifier.execute_with_options(:remove => group) end module_function :verbose_remove + + # 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`. + def list(group = :all) + TerminalNotifier.silence_stdout { TerminalNotifier.verbose_list(group) } + end + module_function :list + + LIST_FIELDS = [:group, :title, :subtitle, :message, :delivered_at].freeze + + # The same as `list`, but sends the output from the tool to STDOUT. + def verbose_list(group = :all) + output = TerminalNotifier.execute_with_options(:list => (group == :all ? 'ALL' : group)) + return if output.strip.empty? + + 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 + module_function :verbose_list end diff --git a/Ruby/spec/terminal-notifier_spec.rb b/Ruby/spec/terminal-notifier_spec.rb index b13298f..2bee4f4 100644 --- a/Ruby/spec/terminal-notifier_spec.rb +++ b/Ruby/spec/terminal-notifier_spec.rb @@ -1,3 +1,4 @@ +require 'time' require 'rubygems' require 'bacon' require 'mocha' @@ -28,4 +29,39 @@ TerminalNotifier.expects(:execute_with_options).with(:remove => 'important stuff') TerminalNotifier.remove('important stuff') end + + it "returns `nil` if no notification was found to list info for" do + TerminalNotifier.expects(:execute_with_options).with(:list => 'important stuff').returns('') + TerminalNotifier.list('important stuff').should == nil + end + + it "returns info about a notification posted in a specific group" do + TerminalNotifier.expects(:execute_with_options).with(:list => 'important stuff'). + returns("GroupID\tTitle\tSubtitle\tMessage\tDelivered At\n" \ + "important stuff\tTerminal\t(null)\tExecute: rake spec\t2012-08-06 19:45:30 +0000") + TerminalNotifier.list('important stuff').should == { + :group => 'important stuff', + :title => 'Terminal', :subtitle => nil, :message => 'Execute: rake spec', + :delivered_at => Time.parse('2012-08-06 19:45:30 +0000') + } + end + + it "by default returns a list of all notification" do + TerminalNotifier.expects(:execute_with_options).with(:list => 'ALL'). + returns("GroupID\tTitle\tSubtitle\tMessage\tDelivered At\n" \ + "important stuff\tTerminal\t(null)\tExecute: rake spec\t2012-08-06 19:45:30 +0000\n" \ + "(null)\t(null)\tSubtle\tBe subtle!\t2012-08-07 19:45:30 +0000") + TerminalNotifier.list.should == [ + { + :group => 'important stuff', + :title => 'Terminal', :subtitle => nil, :message => 'Execute: rake spec', + :delivered_at => Time.parse('2012-08-06 19:45:30 +0000') + }, + { + :group => nil, + :title => nil, :subtitle => 'Subtle', :message => 'Be subtle!', + :delivered_at => Time.parse('2012-08-07 19:45:30 +0000') + } + ] + end end diff --git a/Terminal Notifier.xcodeproj/xcshareddata/xcschemes/Terminal Notifier.xcscheme b/Terminal Notifier.xcodeproj/xcshareddata/xcschemes/Terminal Notifier.xcscheme index 2dba179..c746d11 100644 --- a/Terminal Notifier.xcodeproj/xcshareddata/xcschemes/Terminal Notifier.xcscheme +++ b/Terminal Notifier.xcodeproj/xcshareddata/xcschemes/Terminal Notifier.xcscheme @@ -92,7 +92,7 @@ + isEnabled = "YES">