Browse Source

Growl agent to add support for sending Growl notifications to network destinations

snicker 11 years ago
parent
commit
b944eb0e16
2 changed files with 62 additions and 0 deletions
  1. 1 0
      Gemfile
  2. 61 0
      app/models/agents/growl_agent.rb

+ 1 - 0
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

+ 61 - 0
app/models/agents/growl_agent.rb

@@ -0,0 +1,61 @@
+require 'ruby-growl'
+
+module Agents
+  class GrowlAgent < Agent
+
+    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
+      {
+          'growlserver' => 'localhost',
+          'growlpassword' => '',
+          'growlappname' => 'HuginnGrowl',
+          'growlnotificationname' => '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['growlserver'].present? && options['expected_receive_period_in_days'].present?
+        errors.add(:base, "growlserver and expected_receive_period_in_days are required fields")
+      end
+    end
+    
+    def register_growl
+      @growler = Growl.new options['growlserver'], options['growlappname'], "GNTP"
+      @growler.password = options['growlpassword']
+      @growler.add_notification options['growlnotificationname']
+    end
+    
+    def notify_growl(subject, message)
+      @growler.notify(options['growlnotificationname'],subject,message)
+    end
+
+    def receive(incoming_events)
+      incoming_events.each do |event|
+        register_growl
+        message = (event.payload['message'] || event.payload['text']).to_s
+        subject = event.payload['subject'].to_s
+        if message != "" && subject != ""
+          log "Sending Growl notification '#{subject}': '#{message}' to #{options['growlserver']} with event #{event.id}"
+          notify_growl(subject,message)
+        else
+          log "Event #{event.id} not sent, message and subject expected"
+        end
+      end
+    end
+  end
+end