Permalink
Browse files

Add support for the `list` command to the Ruby lib.

  • Loading branch information...
1 parent 4145da7 commit 2b3055f7f4f31d7722f6adf9bce22c4c62e6d0e3 @alloy alloy committed Aug 10, 2012
View
@@ -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
```
@@ -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
@@ -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
@@ -92,7 +92,7 @@
</CommandLineArgument>
<CommandLineArgument
argument = "-message &apos;Execute: rake spec&apos;"
- isEnabled = "NO">
+ isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-activate com.apple.Safari"

0 comments on commit 2b3055f

Please sign in to comment.