Skip to content

Commit

Permalink
Add support for the list command to the Ruby lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Aug 10, 2012
1 parent 4145da7 commit 2b3055f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Ruby/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
30 changes: 30 additions & 0 deletions Ruby/lib/terminal-notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions Ruby/spec/terminal-notifier_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'time'
require 'rubygems'
require 'bacon'
require 'mocha'
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
</CommandLineArgument>
<CommandLineArgument
argument = "-message &apos;Execute: rake spec&apos;"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-activate com.apple.Safari"
Expand Down

0 comments on commit 2b3055f

Please sign in to comment.