Class: ActivityMailer

Inherits:
Object
  • Object
show all
Defined in:
lib/activity_mailer.rb

Overview

This class will do the following: a) Look up the “best match” template (i.e., if a language-specific version isn’t found, use English) b) Send a message using the “best match” template

Uses the Mandrill API, but largely masks it

Template registration depends on an initially-created template in Mandrill with the tags service-subscription, activity-default, lang-en

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mandrill_conn, service_name) ⇒ ActivityMailer

Returns a new instance of ActivityMailer.



15
16
17
18
19
20
21
22
# File 'lib/activity_mailer.rb', line 15

def initialize(mandrill_conn, service_name)
	@mandrill_connection = mandrill_conn
	@service_name = service_name
	@service_label = "service-#{service_name}"
	@default_language = "en"
	@supported_languages = [@default_language]
	@mandrill_ip_pool = nil
end

Instance Attribute Details

#default_languageObject

Returns the value of attribute default_language.



11
12
13
# File 'lib/activity_mailer.rb', line 11

def default_language
  @default_language
end

#mandrill_connectionObject

Returns the value of attribute mandrill_connection.



10
11
12
# File 'lib/activity_mailer.rb', line 10

def mandrill_connection
  @mandrill_connection
end

#mandrill_ip_poolObject

Returns the value of attribute mandrill_ip_pool.



13
14
15
# File 'lib/activity_mailer.rb', line 13

def mandrill_ip_pool
  @mandrill_ip_pool
end

#supported_languagesObject

This is only really used for default template creation



12
13
14
# File 'lib/activity_mailer.rb', line 12

def supported_languages
  @supported_languages
end

Class Method Details

.shared_connectionObject



28
29
30
# File 'lib/activity_mailer.rb', line 28

def self.shared_connection
	@@shared_connection
end

.shared_connection=(val) ⇒ Object



24
25
26
# File 'lib/activity_mailer.rb', line 24

def self.shared_connection=(val)
	@@shared_connection = val
end

Instance Method Details

#create_default_template!(from_addr, from_name, subject) ⇒ Object

This should only be done once



33
34
35
36
37
38
39
# File 'lib/activity_mailer.rb', line 33

def create_default_template!(from_addr, from_name, subject)
	return false unless templates_for("default").empty?
	@supported_languages.each do |lang|
		name = name_from_labels("default", lang)
		templ = @mandrill_connection.templates.add(name, from_addr, from_name, subject, "<html><body>Base Template</body></html>", "Base Template", true, [@service_label, "activity-default", "lang-#{lang}"])
	end
end

#deliver_email!(activity, lang, message_info, data = {}) ⇒ Object

NOTE - this can throw exceptions - should rescue them



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/activity_mailer.rb', line 93

def deliver_email!(activity, lang, message_info, data = {})
	templ_list = templates_for(activity, lang)
	raise "No template found" if templ_list.empty?

	# Log error if too many templates
	if templ_list.size > 1
		if(defined? Rails)
			Rails.logger.warn("Too many templates for #{activity}/#{lang}!") 
		end
	end

	templ = templ_list.first

	message_info[:global_merge_vars] ||= []
	data.each do |k, v|
		message_info[:global_merge_vars].push({:name => k, :content => v})
	end

	@mandrill_connection.messages.send_template(templ["name"], data, message_info, false, @mandrill_ip_pool)
end

#name_from_labels(activity, lang) ⇒ Object



41
42
43
# File 'lib/activity_mailer.rb', line 41

def name_from_labels(activity, lang)
	"#{@service_name.underscore.titleize}/#{activity.underscore.titleize}/#{lang.underscore}"
end

#register_template!(system_name, from_addr = nil, from_name = nil, subject = nil, html = nil, text = nil) ⇒ Object

Registers a new type of template



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/activity_mailer.rb', line 46

def register_template!(system_name, from_addr = nil, from_name = nil, subject = nil, html = nil, text = nil)
	return false unless templates_for(system_name).empty?
	default_templates = @mandrill_connection.templates.list(@service_label).select{|templ| 
		templ["labels"].include?("activity-default") && templ["published_at"] != nil
	}
	default_templates.each do |templ|
		lang = begin
			templ["labels"].select{|x| x.include?("lang-")}.first.split("-")[1]
		rescue
			if defined? Rails
				Rails.logger.warn("Bad language for template")
			end
			nil
		end
		next if lang.nil?

		new_templ_name = name_from_labels(system_name, lang)
		new_templ = @mandrill_connection.templates.add(new_templ_name, (from_addr || templ["publish_from_email"]), (from_name || templ["publish_from_name"]), (subject || templ["publish_subject"]), (html || templ["publish_code"]), (text || templ["publish_text"]), true, [@service_label, "activity-#{system_name}", "lang-#{lang}"])
	end
end

#templates_for(system_name, language = nil, best = true) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/activity_mailer.rb', line 67

def templates_for(system_name, language = nil, best = true)
	language = default_language if language.nil?
	possible_templates = @mandrill_connection.templates.list(@service_label).select{|templ| 
		# must be of the right activity
		templ["labels"].include?("activity-#{system_name}") && 
		# if it is unpublished, don't include it
		templ["published_at"] != nil
	}

	# Do I have one for my language?
	lang_templates = possible_templates.select{|templ| templ["labels"].include?("lang-#{language}")}

	# No? Then grab the one for the default language
	if lang_templates.empty?
		lang_templates = possible_templates.select{|templ| templ["labels"].include?("lang-#{default_language}")}
	end

	# Still none? Well, just send them all
	if lang_templates.empty?
		lang_templates = possible_templates
	end

	return lang_templates
end