activity-mailer 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/activity_mailer.rb +112 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b459e47e37506f20b93000b4ea5464d07702e35
4
+ data.tar.gz: c506f7e4149e410ac625641407d954c992c3e879
5
+ SHA512:
6
+ metadata.gz: f87704275c6f0703fc898f18df289d7461c8bea6c82d4c1d7226cc30dd6652a560f7ca2662959e460de0afa4514810527c914b8fd39d7965fc5d35d25d29c562
7
+ data.tar.gz: acae3d66fb646147811411c8019c9245924c98e67fe44dac251b7d0d906e30dc2861b75b69f77b4eb3e239d977ac62b618edb8a7651c719ad398640591f2bc62
@@ -0,0 +1,112 @@
1
+ # This class will do the following:
2
+ # a) Look up the "best match" template (i.e., if a language-specific version isn't found, use English)
3
+ # b) Send a message using the "best match" template
4
+ #
5
+ # Uses the Mandrill API, but largely masks it
6
+ #
7
+ # Template registration depends on an initially-created template in Mandrill with the tags service-subscription, activity-default, lang-en
8
+
9
+ class ActivityMailer
10
+ attr_accessor :mandrill_connection
11
+ attr_accessor :default_language
12
+ attr_accessor :supported_languages # This is only really used for default template creation
13
+ attr_accessor :mandrill_ip_pool
14
+
15
+ def initialize(mandrill_conn, service_name)
16
+ @mandrill_connection = mandrill_conn
17
+ @service_name = service_name
18
+ @service_label = "service-#{service_name}"
19
+ @default_language = "en"
20
+ @supported_languages = [@default_language]
21
+ @mandrill_ip_pool = nil
22
+ end
23
+
24
+ def self.shared_connection=(val)
25
+ @@shared_connection = val
26
+ end
27
+
28
+ def self.shared_connection
29
+ @@shared_connection
30
+ end
31
+
32
+ # This should only be done once
33
+ def create_default_template!(from_addr, from_name, subject)
34
+
35
+ @supported_languages.each do |lang|
36
+ name = name_from_labels("default", lang)
37
+ templ = @mandrill_connection.templates.add(name, from_addr, from_name, subject, "<div>FIXME</div>", "FIXME", true, [@service_label, "activity-default", "lang-#{lang}"])
38
+ end
39
+ end
40
+
41
+ def name_from_labels(activity, lang)
42
+ "#{@service_name.underscore.titleize}/#{activity.underscore.titleize}/#{lang.underscore}"
43
+ end
44
+
45
+ # Registers a new type of template
46
+ def register_template!(system_name)
47
+ default_templates = @mandrill_connection.templates.list(@service_label).select{|templ|
48
+ templ["labels"].include?("activity-default") && templ["published_at"] != nil
49
+ }
50
+ default_templates.each do |templ|
51
+ lang = begin
52
+ templ["labels"].select{|x| x.include?("lang-")}.first.split("-")[1]
53
+ rescue
54
+ if defined? Rails
55
+ Rails.logger.warn("Bad language for template")
56
+ end
57
+ nil
58
+ end
59
+ next if lang.nil?
60
+
61
+ new_templ_name = name_from_labels(system_name, lang)
62
+ new_templ = @mandrill_connection.templates.add(new_templ_name, templ["publish_from_email"], templ["publish_from_name"], templ["publish_subject"], templ["publish_code"], templ["publish_text"], true, [@service_label, "activity-#{system_name}", "lang-#{lang}"])
63
+ end
64
+ end
65
+
66
+ def templates_for(system_name, language = nil, best = true)
67
+ language = default_language if language.nil?
68
+ possible_templates = @mandrill_connection.templates.list(@service_label).select{|templ|
69
+ # must be of the right activity
70
+ templ["labels"].include?("activity-#{system_name}") &&
71
+ # if it is unpublished, don't include it
72
+ templ["published_at"] != nil
73
+ }
74
+
75
+ # Do I have one for my language?
76
+ lang_templates = possible_templates.select{|templ| templ["labels"].include?("lang-#{language}")}
77
+
78
+ # No? Then grab the one for the default language
79
+ if lang_templates.empty?
80
+ lang_templates = possible_templates.select{|templ| templ["labels"].include?("lang-#{default_language}")}
81
+ end
82
+
83
+ # Still none? Well, just send them all
84
+ if lang_templates.empty?
85
+ lang_templates = possible_templates
86
+ end
87
+
88
+ return lang_templates
89
+ end
90
+
91
+ # NOTE - this can throw exceptions - should rescue them
92
+ def deliver_email!(activity, lang, message_info, data = {})
93
+ templ_list = templates_for(activity, lang)
94
+ raise "No template found" if templ_list.empty?
95
+
96
+ # Log error if too many templates
97
+ if templ_list.size > 1
98
+ if(defined? Rails)
99
+ Rails.logger.warn("Too many templates for #{activity}/#{lang}!")
100
+ end
101
+ end
102
+
103
+ templ = templ_list.first
104
+
105
+ mandrill_data = []
106
+ data.each do |k, v|
107
+ mandrill_data.push({:name => k, :content => v})
108
+ end
109
+
110
+ @mandrill_connection.messages.send_template(templ["name"], data, message_info, false, @mandrill_ip_pool)
111
+ end
112
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activity-mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Bartlett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mandrill-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email: jonathan@newmedio.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/activity_mailer.rb
34
+ homepage: http://www.newmedio.com/
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.5.2.1
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Mandrill Templating Service Helper
58
+ test_files: []