@@ -0,0 +1,37 @@ |
||
| 1 |
+module EmailConcern |
|
| 2 |
+ extend ActiveSupport::Concern |
|
| 3 |
+ |
|
| 4 |
+ MAIN_KEYS = %w[title message text main value].map(&:to_sym) |
|
| 5 |
+ |
|
| 6 |
+ included do |
|
| 7 |
+ self.validate :validate_email_options |
|
| 8 |
+ end |
|
| 9 |
+ |
|
| 10 |
+ def validate_email_options |
|
| 11 |
+ errors.add(:base, "subject and expected_receive_period_in_days are required") unless options[:subject].present? && options[:expected_receive_period_in_days].present? |
|
| 12 |
+ end |
|
| 13 |
+ |
|
| 14 |
+ def working? |
|
| 15 |
+ last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? |
|
| 16 |
+ end |
|
| 17 |
+ |
|
| 18 |
+ def present(payload) |
|
| 19 |
+ if payload.is_a?(Hash) |
|
| 20 |
+ payload = ActiveSupport::HashWithIndifferentAccess.new(payload) |
|
| 21 |
+ MAIN_KEYS.each do |key| |
|
| 22 |
+ return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key)
|
|
| 23 |
+ end |
|
| 24 |
+ |
|
| 25 |
+ { :title => "Event", :entries => present_hash(payload) }
|
|
| 26 |
+ else |
|
| 27 |
+ { :title => payload.to_s, :entries => [] }
|
|
| 28 |
+ end |
|
| 29 |
+ end |
|
| 30 |
+ |
|
| 31 |
+ def present_hash(hash, skip_key = nil) |
|
| 32 |
+ hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact
|
|
| 33 |
+ end |
|
| 34 |
+ |
|
| 35 |
+ module ClassMethods |
|
| 36 |
+ end |
|
| 37 |
+end |
@@ -1,6 +1,7 @@ |
||
| 1 | 1 |
module Agents |
| 2 | 2 |
class DigestEmailAgent < Agent |
| 3 |
- MAIN_KEYS = %w[title message text main value].map(&:to_sym) |
|
| 3 |
+ include EmailConcern |
|
| 4 |
+ |
|
| 4 | 5 |
default_schedule "5am" |
| 5 | 6 |
|
| 6 | 7 |
cannot_create_events! |
@@ -22,14 +23,6 @@ module Agents |
||
| 22 | 23 |
} |
| 23 | 24 |
end |
| 24 | 25 |
|
| 25 |
- def working? |
|
| 26 |
- last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? |
|
| 27 |
- end |
|
| 28 |
- |
|
| 29 |
- def validate_options |
|
| 30 |
- errors.add(:base, "subject and expected_receive_period_in_days are required") unless options[:subject].present? && options[:expected_receive_period_in_days].present? |
|
| 31 |
- end |
|
| 32 |
- |
|
| 33 | 26 |
def receive(incoming_events) |
| 34 | 27 |
incoming_events.each do |event| |
| 35 | 28 |
self.memory[:queue] ||= [] |
@@ -49,22 +42,5 @@ module Agents |
||
| 49 | 42 |
self.memory[:events] = [] |
| 50 | 43 |
end |
| 51 | 44 |
end |
| 52 |
- |
|
| 53 |
- def present(payload) |
|
| 54 |
- if payload.is_a?(Hash) |
|
| 55 |
- payload = ActiveSupport::HashWithIndifferentAccess.new(payload) |
|
| 56 |
- MAIN_KEYS.each do |key| |
|
| 57 |
- return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key)
|
|
| 58 |
- end |
|
| 59 |
- |
|
| 60 |
- { :title => "Event", :entries => present_hash(payload) }
|
|
| 61 |
- else |
|
| 62 |
- { :title => payload.to_s, :entries => [] }
|
|
| 63 |
- end |
|
| 64 |
- end |
|
| 65 |
- |
|
| 66 |
- def present_hash(hash, skip_key = nil) |
|
| 67 |
- hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact
|
|
| 68 |
- end |
|
| 69 | 45 |
end |
| 70 | 46 |
end |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 |
module Agents |
| 2 | 2 |
class EventEmailAgent < Agent |
| 3 |
- MAIN_KEYS = %w[title message text main value].map(&:to_sym) |
|
| 3 |
+ include EmailConcern |
|
| 4 | 4 |
|
| 5 | 5 |
cannot_be_scheduled! |
| 6 | 6 |
cannot_create_events! |
@@ -22,36 +22,11 @@ module Agents |
||
| 22 | 22 |
} |
| 23 | 23 |
end |
| 24 | 24 |
|
| 25 |
- def working? |
|
| 26 |
- last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? |
|
| 27 |
- end |
|
| 28 |
- |
|
| 29 |
- def validate_options |
|
| 30 |
- errors.add(:base, "subject and expected_receive_period_in_days are required") unless options[:subject].present? && options[:expected_receive_period_in_days].present? |
|
| 31 |
- end |
|
| 32 |
- |
|
| 33 | 25 |
def receive(incoming_events) |
| 34 | 26 |
incoming_events.each do |event| |
| 35 | 27 |
log "Sending digest mail to #{user.email} with event #{event.id}"
|
| 36 | 28 |
SystemMailer.delay.send_message(:to => user.email, :subject => options[:subject], :headline => options[:headline], :groups => [present(event.payload)]) |
| 37 | 29 |
end |
| 38 | 30 |
end |
| 39 |
- |
|
| 40 |
- def present(payload) |
|
| 41 |
- if payload.is_a?(Hash) |
|
| 42 |
- payload = ActiveSupport::HashWithIndifferentAccess.new(payload) |
|
| 43 |
- MAIN_KEYS.each do |key| |
|
| 44 |
- return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key)
|
|
| 45 |
- end |
|
| 46 |
- |
|
| 47 |
- { :title => "Event", :entries => present_hash(payload) }
|
|
| 48 |
- else |
|
| 49 |
- { :title => payload.to_s, :entries => [] }
|
|
| 50 |
- end |
|
| 51 |
- end |
|
| 52 |
- |
|
| 53 |
- def present_hash(hash, skip_key = nil) |
|
| 54 |
- hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact
|
|
| 55 |
- end |
|
| 56 | 31 |
end |
| 57 | 32 |
end |