Permalink
Browse files

Use growlnotify's wait flag to determine if a notification was clicked.

And take an action accordingly.
  • Loading branch information...
1 parent 363f427 commit 8175cf28b33d849f35cad4e2748c6c9b0ad2683e @dzello dzello committed Mar 19, 2012
Showing with 43 additions and 20 deletions.
  1. +24 −3 lib/kilt.rb
  2. +19 −17 spec/kilt_spec.rb
View
@@ -18,7 +18,8 @@ def update
activities = fetch_activities
activities.reverse.each do |activity|
if activity['id'] > @id.to_i && activity['author'] != @skip_author
- notify_about activity['description']
+ activity['stories'] and activity['stories'][0] and (id = activity['stories'][0]['id'])
+ notify_about activity['description'], id
end
end
update_id_from activities
@@ -44,16 +45,36 @@ def fetch_activities
'X-TrackerToken' => @token).body)['activities']
end
- def notify_about(message)
+ def notify_about(message, id=nil)
title = 'Pivotal Tracker'
case RUBY_PLATFORM
when /linux/
system "notify-send '#{title}' '#{message}' --icon #{Kilt::ICON}"
when /darwin/
- system "growlnotify -t '#{title}' -m '#{message}' --image #{Kilt::ICON}"
+ if defined?(NO_FORK)
+ osx_notify(title, message, id)
+ else
+ # fork here so that the child process can wait for a click, but
+ # the main process can continue to display notifications
+ # growlnotify bug - the user must click within 10 seconds for the process
+ # to return "successfully" - see:
+ # http://groups.google.com/group/growldiscuss/browse_thread/thread/9b5af76d3c1667d9
+ fork do
+ osx_notify(title, message, id)
+ exit
+ end
+ end
when /mswin|mingw|win32/
Snarl.show_message title, message, Kilt::ICON
end
end
+ def osx_notify(title, message, id)
+ system "growlnotify -t -w -s '#{title}' -m '#{message}' --image #{Kilt::ICON}"
+ # if the return code is greater than 0 it was clicked before 10s
+ # only handle the click its a valid tracker id
+ if id and $?.to_i > 0
+ system "open https://www.pivotaltracker.com/story/show/#{id}"
+ end
+ end
end
View
@@ -26,19 +26,21 @@
end
it "should fetch new activities" do
- @scheduler.stub(:every) do |time, block|
- block.call
- end
+ @scheduler.stub(:every) {}
RestClient.should_receive(:get).exactly(2).times do
mock(Object, :body => latests_activities)
end
- Kilt.init 'fegegege'
+
+ @client = Kilt.init 'fegegege'
+ @client.stub!(:system)
+ @client.update
end
end
context "on update" do
before :each do
@client = Kilt.init('fake')
+ @client.stub!(:system)
@client.instance_variable_set "@id", '25906311'
end
@@ -48,32 +50,32 @@
end
it "should notifify about each new activity" do
- @client.should_receive(:system).exactly(2).times
+ @client.should_receive(:notify_about).exactly(2).times {}
@client.update
end
it "should not notify a notification who's author matches skip_author" do
@client.instance_variable_set "@skip_author", 'Superman'
- @client.should_receive(:system).exactly(1).times
+ @client.should_receive(:notify_about).exactly(1).times {}
@client.update
end
context "on os x" do
before :all do
- silence_warnings { RUBY_PLATFORM = "darwin" }
+ silence_warnings { RUBY_PLATFORM = "darwin"; NO_FORK = "yep" }
end
-
+
it "should notify growl calling growlnotify with 'Pivotal Tracker' as the name the application, the author and the action" do
- regexp = /growlnotify -t \'Pivotal Tracker\' -m \'\S+. finished lorem ipsum\' --image \S+.pivotal\.png/
- @client.should_receive(:system).with(regexp).twice
+ regexp = /growlnotify -t -w -s \'Pivotal Tracker\' -m \'\S+. finished lorem ipsum\' --image \S+.pivotal\.png/
+ @client.should_receive(:system).with(regexp).twice {}
@client.update
end
it "should notify newer activities at least" do
- regexp = /growlnotify -t \'Pivotal Tracker\' -m \'SpongeBog finished lorem ipsum\' --image \S+.pivotal\.png/
- regexp2 = /growlnotify -t \'Pivotal Tracker\' -m \'Superman finished lorem ipsum\' --image \S+.pivotal\.png/
- @client.should_receive(:system).with(regexp).ordered
- @client.should_receive(:system).with(regexp2).ordered
+ regexp = /growlnotify -t -w -s \'Pivotal Tracker\' -m \'SpongeBog finished lorem ipsum\' --image \S+.pivotal\.png/
+ regexp2 = /growlnotify -t -w -s \'Pivotal Tracker\' -m \'Superman finished lorem ipsum\' --image \S+.pivotal\.png/
+ @client.should_receive(:system).with(regexp).ordered {}
+ @client.should_receive(:system).with(regexp2).ordered {}
@client.update
end
end
@@ -85,15 +87,15 @@
it "should notify libnotify calling notify-send with 'Pivotal Tracker' as the name the application, the author and the action" do
regexp = /notify-send \'Pivotal Tracker\' \'\S+. finished lorem ipsum\' --icon \S+.pivotal\.png/
- @client.should_receive(:system).with(regexp).twice
+ @client.should_receive(:system).with(regexp).twice {}
@client.update
end
it "should notify newer activities at least" do
regexp = /notify-send \'Pivotal Tracker\' \'SpongeBog finished lorem ipsum\' --icon \S+.pivotal\.png/
regexp2 = /notify-send \'Pivotal Tracker\' \'Superman finished lorem ipsum\' --icon \S+.pivotal\.png/
- @client.should_receive(:system).with(regexp).ordered
- @client.should_receive(:system).with(regexp2).ordered
+ @client.should_receive(:system).with(regexp).ordered {}
+ @client.should_receive(:system).with(regexp2).ordered {}
@client.update
end
end

0 comments on commit 8175cf2

Please sign in to comment.