growl_agent.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module Agents
  2. class GrowlAgent < Agent
  3. include FormConfigurable
  4. attr_reader :growler
  5. cannot_be_scheduled!
  6. cannot_create_events!
  7. can_dry_run!
  8. gem_dependency_check { defined?(Growl) }
  9. description <<-MD
  10. The Growl Agent sends any events it receives to a Growl GNTP server immediately.
  11. #{'## Include `ruby-growl` in your Gemfile to use this Agent!' if dependencies_missing?}
  12. The option `message`, which will hold the body of the growl notification, and the `subject` option,
  13. which will have the headline of the Growl notification are required. All other options are optional.
  14. When `callback_url` is set to a URL clicking on the notification will open the link in your default browser.
  15. Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between
  16. Events being received by this Agent.
  17. Have a look at the [Wiki](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) to learn
  18. more about liquid templating.
  19. MD
  20. def default_options
  21. {
  22. 'growl_server' => 'localhost',
  23. 'growl_password' => '',
  24. 'growl_app_name' => 'HuginnGrowl',
  25. 'growl_notification_name' => 'Notification',
  26. 'expected_receive_period_in_days' => "2",
  27. 'subject' => '{{subject}}',
  28. 'message' => '{{message}}',
  29. 'sticky' => 'false',
  30. 'priority' => '0'
  31. }
  32. end
  33. form_configurable :growl_server
  34. form_configurable :growl_password
  35. form_configurable :growl_app_name
  36. form_configurable :growl_notification_name
  37. form_configurable :expected_receive_period_in_days
  38. form_configurable :subject
  39. form_configurable :message, type: :text
  40. form_configurable :sticky, type: :boolean
  41. form_configurable :priority
  42. form_configurable :callback_url
  43. def working?
  44. last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  45. end
  46. def validate_options
  47. unless options['growl_server'].present? && options['expected_receive_period_in_days'].present?
  48. errors.add(:base, "growl_server and expected_receive_period_in_days are required fields")
  49. end
  50. end
  51. def register_growl
  52. @growler = Growl::GNTP.new(interpolated['growl_server'], interpolated['growl_app_name'])
  53. @growler.password = interpolated['growl_password']
  54. @growler.add_notification(interpolated['growl_notification_name'])
  55. end
  56. def notify_growl(subject:, message:, priority:, sticky:, callback_url:)
  57. @growler.notify(interpolated['growl_notification_name'], subject, message, priority, sticky, nil, callback_url)
  58. end
  59. def receive(incoming_events)
  60. incoming_events.each do |event|
  61. interpolate_with(event) do
  62. register_growl
  63. message = interpolated[:message]
  64. subject = interpolated[:subject]
  65. if message.present? && subject.present?
  66. log "Sending Growl notification '#{subject}': '#{message}' to #{interpolated(event)['growl_server']} with event #{event.id}"
  67. notify_growl(subject: subject,
  68. message: message,
  69. priority: interpolated[:priority].to_i,
  70. sticky: boolify(interpolated[:sticky]) || false,
  71. callback_url: interpolated[:callback_url].presence)
  72. else
  73. log "Event #{event.id} not sent, message and subject expected"
  74. end
  75. end
  76. end
  77. end
  78. end
  79. end