Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #172 from snicker/master

Added GrowlAgent for sending Growl notifications over GNTP
  • Loading branch information...
commit 9fa907bec0d5bbd52a184c1c8ee0bb8aba83eb29 2 parents 882fe52 + d04251b
@cantino authored
View
1  Gemfile
@@ -10,6 +10,7 @@ gem "rufus-scheduler", :require => false
gem 'json', '>= 1.7.7'
gem 'jsonpath'
gem 'twilio-ruby'
+gem 'ruby-growl'
gem 'delayed_job'
gem 'delayed_job_active_record'#, "~> 0.3.3" # newer was giving a strange MySQL error
View
62 app/models/agents/growl_agent.rb
@@ -0,0 +1,62 @@
+require 'ruby-growl'
+
+module Agents
+ class GrowlAgent < Agent
+ attr_reader :growler
+
+ cannot_be_scheduled!
+ cannot_create_events!
+
+ description <<-MD
+ The GrowlAgent sends any events it receives to a Growl GNTP server immediately.
+
+ It is assumed that events have a `message` or `text` key, which will hold the body of the growl notification, and a `subject` key, which will have the headline of the Growl notification. You can use Event Formatting Agent if your event does not provide these keys.
+
+ Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
+ MD
+
+ def default_options
+ {
+ 'growl_server' => 'localhost',
+ 'growl_password' => '',
+ 'growl_app_name' => 'HuginnGrowl',
+ 'growl_notification_name' => 'Notification',
+ 'expected_receive_period_in_days' => "2"
+ }
+ end
+
+ def working?
+ last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
+ end
+
+ def validate_options
+ unless options['growl_server'].present? && options['expected_receive_period_in_days'].present?
+ errors.add(:base, "growl_server and expected_receive_period_in_days are required fields")
+ end
+ end
+
+ def register_growl
+ @growler = Growl.new options['growl_server'], options['growl_app_name'], "GNTP"
+ @growler.password = options['growl_password']
+ @growler.add_notification options['growl_notification_name']
+ end
+
+ def notify_growl(subject, message)
+ @growler.notify(options['growl_notification_name'],subject,message)
+ end
+
+ def receive(incoming_events)
+ register_growl
+ incoming_events.each do |event|
+ message = (event.payload['message'] || event.payload['text']).to_s
+ subject = event.payload['subject'].to_s
+ if message.present? && subject.present?
+ log "Sending Growl notification '#{subject}': '#{message}' to #{options['growl_server']} with event #{event.id}"
+ notify_growl(subject,message)
+ else
+ log "Event #{event.id} not sent, message and subject expected"
+ end
+ end
+ end
+ end
+end
View
122 spec/models/agents/growl_agent_spec.rb
@@ -0,0 +1,122 @@
+require 'spec_helper'
+
+describe Agents::GrowlAgent do
+ before do
+ @checker = Agents::GrowlAgent.new(:name => 'a growl agent',
+ :options => { :growl_server => 'localhost',
+ :growl_app_name => 'HuginnGrowlApp',
+ :growl_password => 'mypassword',
+ :growl_notification_name => 'Notification',
+ :expected_receive_period_in_days => '1' })
+ @checker.user = users(:bob)
+ @checker.save!
+
+ stub.any_instance_of(Growl).notify
+
+ @event = Event.new
+ @event.agent = agents(:bob_weather_agent)
+ @event.payload = { :subject => 'Weather Alert!', :message => 'Looks like its going to rain' }
+ @event.save!
+ end
+
+ describe "#working?" do
+ it "checks if events have been received within the expected receive period" do
+ @checker.should_not be_working # No events received
+ Agents::GrowlAgent.async_receive @checker.id, [@event.id]
+ @checker.reload.should be_working # Just received events
+ two_days_from_now = 2.days.from_now
+ stub(Time).now { two_days_from_now }
+ @checker.reload.should_not be_working # More time has passed than the expected receive period without any new events
+ end
+ end
+
+ describe "validation" do
+ before do
+ @checker.should be_valid
+ end
+
+ it "should validate presence of of growl_server" do
+ @checker.options[:growl_server] = ""
+ @checker.should_not be_valid
+ end
+
+ it "should validate presence of expected_receive_period_in_days" do
+ @checker.options[:expected_receive_period_in_days] = ""
+ @checker.should_not be_valid
+ end
+ end
+
+ describe "register_growl" do
+ it "should set the password for the Growl connection from the agent options" do
+ @checker.register_growl
+ @checker.growler.password.should eql(@checker.options[:growl_password])
+ end
+
+ it "should add a notification to the Growl connection" do
+ called = false
+ any_instance_of(Growl) do |obj|
+ called = true
+ mock(obj).add_notification(@checker.options[:growl_notification_name])
+ end
+
+ @checker.register_growl
+ called.should be_true
+ end
+ end
+
+ describe "notify_growl" do
+ before do
+ @checker.register_growl
+ end
+
+ it "should call Growl.notify with the correct notification name, subject, and message" do
+ message = "message"
+ subject = "subject"
+ called = false
+ any_instance_of(Growl) do |obj|
+ called = true
+ mock(obj).notify(@checker.options[:growl_notification_name],subject,message)
+ end
+ @checker.notify_growl(subject,message)
+ called.should be_true
+ end
+ end
+
+ describe "receive" do
+ def generate_events_array
+ events = []
+ (2..rand(7)).each do
+ events << @event
+ end
+ return events
+ end
+
+ it "should call register_growl once regardless of number of events received" do
+ mock.proxy(@checker).register_growl.once
+ @checker.receive(generate_events_array)
+ end
+
+ it "should call notify_growl one time for each event received" do
+ events = generate_events_array
+ events.each do |event|
+ mock.proxy(@checker).notify_growl(event.payload['subject'], event.payload['message'])
+ end
+ @checker.receive(events)
+ end
+
+ it "should not call notify_growl if message or subject are missing" do
+ event_without_a_subject = Event.new
+ event_without_a_subject.agent = agents(:bob_weather_agent)
+ event_without_a_subject.payload = { :message => 'Looks like its going to rain' }
+ event_without_a_subject.save!
+
+ event_without_a_message = Event.new
+ event_without_a_message.agent = agents(:bob_weather_agent)
+ event_without_a_message.payload = { :subject => 'Weather Alert YO!' }
+ event_without_a_message.save!
+
+ mock.proxy(@checker).notify_growl.never
+ @checker.receive([event_without_a_subject,event_without_a_message])
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.