1
0

email_digest_agent.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. module Agents
  2. class EmailDigestAgent < Agent
  3. include EmailConcern
  4. default_schedule "5am"
  5. cannot_create_events!
  6. description <<-MD
  7. The EmailDigestAgent collects any Events sent to it and sends them all via email when scheduled.
  8. By default, the will have a `subject` and an optional `headline` before listing the Events. If the Events'
  9. payloads contain a `message`, that will be highlighted, otherwise everything in
  10. their payloads will be shown.
  11. You can specify one or more `recipients` for the email, or skip the option in order to send the email to your
  12. account's default email address.
  13. 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.
  14. MD
  15. def default_options
  16. {
  17. 'subject' => "You have some notifications!",
  18. 'headline' => "Your notifications:",
  19. 'expected_receive_period_in_days' => "2"
  20. }
  21. end
  22. def receive(incoming_events)
  23. incoming_events.each do |event|
  24. self.memory['queue'] ||= []
  25. self.memory['queue'] << event.payload
  26. self.memory['events'] ||= []
  27. self.memory['events'] << event.id
  28. end
  29. end
  30. def check
  31. if self.memory['queue'] && self.memory['queue'].length > 0
  32. ids = self.memory['events'].join(",")
  33. groups = self.memory['queue'].map { |payload| present(payload) }
  34. log "Sending digest mail to #{user.email} with events [#{ids}]"
  35. recipients.each do |recipient|
  36. SystemMailer.send_message(:to => recipient, :subject => interpolated['subject'], :headline => interpolated['headline'], :groups => groups).deliver_later
  37. end
  38. self.memory['queue'] = []
  39. self.memory['events'] = []
  40. end
  41. end
  42. end
  43. end