mad_mimi_mailer 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mad_mimi_mailable.rb +154 -0
- data/lib/mad_mimi_mailer.rb +4 -140
- metadata +4 -3
@@ -0,0 +1,154 @@
|
|
1
|
+
module MadMimiMailable
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
# Custom Mailer attributes
|
8
|
+
|
9
|
+
def promotion(promotion = nil)
|
10
|
+
if promotion.nil?
|
11
|
+
@promotion
|
12
|
+
else
|
13
|
+
@promotion = promotion
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def use_erb(use_erb = nil)
|
18
|
+
if use_erb.nil?
|
19
|
+
@use_erb
|
20
|
+
else
|
21
|
+
@use_erb = use_erb
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def hidden(hidden = nil)
|
26
|
+
if hidden.nil?
|
27
|
+
@hidden
|
28
|
+
else
|
29
|
+
@hidden = hidden
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def unconfirmed(value = nil)
|
34
|
+
if value.nil?
|
35
|
+
@unconfirmed
|
36
|
+
else
|
37
|
+
@unconfirmed = value
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module ClassMethods
|
42
|
+
def method_missing(method_symbol, *parameters)
|
43
|
+
if method_symbol.id2name.match(/^deliver_([_a-z]\w*)/)
|
44
|
+
deliver_mimi_mail($1, *parameters)
|
45
|
+
else
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def deliver_mimi_mail(method, *parameters)
|
51
|
+
mail = new
|
52
|
+
mail.__send__(method, *parameters)
|
53
|
+
|
54
|
+
if mail.use_erb
|
55
|
+
mail.create!(method, *parameters)
|
56
|
+
end
|
57
|
+
|
58
|
+
return unless perform_deliveries
|
59
|
+
|
60
|
+
if delivery_method == :test
|
61
|
+
deliveries << (mail.mail ? mail.mail : mail)
|
62
|
+
else
|
63
|
+
if (all_recipients = mail.recipients).is_a? Array
|
64
|
+
all_recipients.each do |recipient|
|
65
|
+
mail.recipients = recipient
|
66
|
+
call_api!(mail, method)
|
67
|
+
end
|
68
|
+
else
|
69
|
+
call_api!(mail, method)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def call_api!(mail, method)
|
75
|
+
params = {
|
76
|
+
'username' => MadMimiMailer.api_settings[:username],
|
77
|
+
'api_key' => MadMimiMailer.api_settings[:api_key],
|
78
|
+
'promotion_name' => mail.promotion || method.to_s.sub(/^mimi_/, ''),
|
79
|
+
'recipients' => serialize(mail.recipients),
|
80
|
+
'subject' => mail.subject,
|
81
|
+
'bcc' => serialize(mail.bcc || MadMimiMailer.default_parameters[:bcc]),
|
82
|
+
'from' => (mail.from || MadMimiMailer.default_parameters[:from]),
|
83
|
+
'hidden' => serialize(mail.hidden)
|
84
|
+
}
|
85
|
+
|
86
|
+
params['unconfirmed'] = '1' if mail.unconfirmed
|
87
|
+
|
88
|
+
if mail.use_erb
|
89
|
+
if mail.parts.any?
|
90
|
+
params['raw_plain_text'] = content_for(mail, "text/plain")
|
91
|
+
params['raw_html'] = content_for(mail, "text/html") { |html| validate(html.body) }
|
92
|
+
else
|
93
|
+
validate(mail.body)
|
94
|
+
params['raw_html'] = mail.body
|
95
|
+
end
|
96
|
+
else
|
97
|
+
stringified_default_body = (MadMimiMailer.default_parameters[:body] || {}).stringify_keys!
|
98
|
+
stringified_mail_body = (mail.body || {}).stringify_keys!
|
99
|
+
body_hash = stringified_default_body.merge(stringified_mail_body)
|
100
|
+
params['body'] = body_hash.to_yaml
|
101
|
+
end
|
102
|
+
|
103
|
+
response = post_request do |request|
|
104
|
+
request.set_form_data(params)
|
105
|
+
end
|
106
|
+
|
107
|
+
case response
|
108
|
+
when Net::HTTPSuccess
|
109
|
+
response.body
|
110
|
+
else
|
111
|
+
response.error!
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def content_for(mail, content_type)
|
116
|
+
part = mail.parts.detect {|p| p.content_type == content_type }
|
117
|
+
if part
|
118
|
+
yield(part) if block_given?
|
119
|
+
part.body
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def validate(content)
|
124
|
+
unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]")
|
125
|
+
raise MadMimiMailer::ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def post_request
|
130
|
+
url = URI.parse(SINGLE_SEND_URL)
|
131
|
+
request = Net::HTTP::Post.new(url.path)
|
132
|
+
yield(request)
|
133
|
+
http = Net::HTTP.new(url.host, url.port)
|
134
|
+
http.use_ssl = true
|
135
|
+
http.start do |http|
|
136
|
+
http.request(request)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def serialize(recipients)
|
141
|
+
case recipients
|
142
|
+
when String
|
143
|
+
recipients
|
144
|
+
when Array
|
145
|
+
recipients.join(", ")
|
146
|
+
when NilClass
|
147
|
+
nil
|
148
|
+
else
|
149
|
+
raise "Please provide a String or an Array for recipients or bcc."
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
data/lib/mad_mimi_mailer.rb
CHANGED
@@ -2,8 +2,10 @@ require "action_mailer"
|
|
2
2
|
require "net/http"
|
3
3
|
require "net/https"
|
4
4
|
|
5
|
+
require "mad_mimi_mailable"
|
6
|
+
|
5
7
|
class MadMimiMailer < ActionMailer::Base
|
6
|
-
VERSION = '0.0
|
8
|
+
VERSION = '0.1.0'
|
7
9
|
SINGLE_SEND_URL = 'https://madmimi.com/mailer'
|
8
10
|
|
9
11
|
@@api_settings = {}
|
@@ -12,44 +14,9 @@ class MadMimiMailer < ActionMailer::Base
|
|
12
14
|
@@default_parameters = {}
|
13
15
|
cattr_accessor :default_parameters
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
def promotion(promotion = nil)
|
18
|
-
if promotion.nil?
|
19
|
-
@promotion
|
20
|
-
else
|
21
|
-
@promotion = promotion
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def use_erb(use_erb = nil)
|
26
|
-
if use_erb.nil?
|
27
|
-
@use_erb
|
28
|
-
else
|
29
|
-
@use_erb = use_erb
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def hidden(hidden = nil)
|
34
|
-
if hidden.nil?
|
35
|
-
@hidden
|
36
|
-
else
|
37
|
-
@hidden = hidden
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def unconfirmed(value = nil)
|
42
|
-
if value.nil?
|
43
|
-
@unconfirmed
|
44
|
-
else
|
45
|
-
@unconfirmed = value
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
# Class methods
|
17
|
+
include MadMimiMailable
|
50
18
|
|
51
19
|
class << self
|
52
|
-
|
53
20
|
def method_missing(method_symbol, *parameters)
|
54
21
|
if method_symbol.id2name.match(/^deliver_(mimi_[_a-z]\w*)/)
|
55
22
|
deliver_mimi_mail($1, *parameters)
|
@@ -57,109 +24,6 @@ class MadMimiMailer < ActionMailer::Base
|
|
57
24
|
super
|
58
25
|
end
|
59
26
|
end
|
60
|
-
|
61
|
-
def deliver_mimi_mail(method, *parameters)
|
62
|
-
mail = new
|
63
|
-
mail.__send__(method, *parameters)
|
64
|
-
|
65
|
-
if mail.use_erb
|
66
|
-
mail.create!(method, *parameters)
|
67
|
-
end
|
68
|
-
|
69
|
-
return unless perform_deliveries
|
70
|
-
|
71
|
-
if delivery_method == :test
|
72
|
-
deliveries << (mail.mail ? mail.mail : mail)
|
73
|
-
else
|
74
|
-
if (all_recipients = mail.recipients).is_a? Array
|
75
|
-
all_recipients.each do |recipient|
|
76
|
-
mail.recipients = recipient
|
77
|
-
call_api!(mail, method)
|
78
|
-
end
|
79
|
-
else
|
80
|
-
call_api!(mail, method)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def call_api!(mail, method)
|
86
|
-
params = {
|
87
|
-
'username' => api_settings[:username],
|
88
|
-
'api_key' => api_settings[:api_key],
|
89
|
-
'promotion_name' => mail.promotion || method.to_s.sub(/^mimi_/, ''),
|
90
|
-
'recipients' => serialize(mail.recipients),
|
91
|
-
'subject' => mail.subject,
|
92
|
-
'bcc' => serialize(mail.bcc || default_parameters[:bcc]),
|
93
|
-
'from' => (mail.from || default_parameters[:from]),
|
94
|
-
'hidden' => serialize(mail.hidden)
|
95
|
-
}
|
96
|
-
|
97
|
-
params['unconfirmed'] = '1' if mail.unconfirmed
|
98
|
-
|
99
|
-
if mail.use_erb
|
100
|
-
if mail.parts.any?
|
101
|
-
params['raw_plain_text'] = content_for(mail, "text/plain")
|
102
|
-
params['raw_html'] = content_for(mail, "text/html") { |html| validate(html.body) }
|
103
|
-
else
|
104
|
-
validate(mail.body)
|
105
|
-
params['raw_html'] = mail.body
|
106
|
-
end
|
107
|
-
else
|
108
|
-
stringified_default_body = (default_parameters[:body] || {}).stringify_keys!
|
109
|
-
stringified_mail_body = (mail.body || {}).stringify_keys!
|
110
|
-
body_hash = stringified_default_body.merge(stringified_mail_body)
|
111
|
-
params['body'] = body_hash.to_yaml
|
112
|
-
end
|
113
|
-
|
114
|
-
response = post_request do |request|
|
115
|
-
request.set_form_data(params)
|
116
|
-
end
|
117
|
-
|
118
|
-
case response
|
119
|
-
when Net::HTTPSuccess
|
120
|
-
response.body
|
121
|
-
else
|
122
|
-
response.error!
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def content_for(mail, content_type)
|
127
|
-
part = mail.parts.detect {|p| p.content_type == content_type }
|
128
|
-
if part
|
129
|
-
yield(part) if block_given?
|
130
|
-
part.body
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def validate(content)
|
135
|
-
unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]")
|
136
|
-
raise ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def post_request
|
141
|
-
url = URI.parse(SINGLE_SEND_URL)
|
142
|
-
request = Net::HTTP::Post.new(url.path)
|
143
|
-
yield(request)
|
144
|
-
http = Net::HTTP.new(url.host, url.port)
|
145
|
-
http.use_ssl = true
|
146
|
-
http.start do |http|
|
147
|
-
http.request(request)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
def serialize(recipients)
|
152
|
-
case recipients
|
153
|
-
when String
|
154
|
-
recipients
|
155
|
-
when Array
|
156
|
-
recipients.join(", ")
|
157
|
-
when NilClass
|
158
|
-
nil
|
159
|
-
else
|
160
|
-
raise "Please provide a String or an Array for recipients or bcc."
|
161
|
-
end
|
162
|
-
end
|
163
27
|
end
|
164
28
|
|
165
29
|
class ValidationError < StandardError; end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.9
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Dave Hoover
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-07 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -28,6 +28,7 @@ extra_rdoc_files: []
|
|
28
28
|
|
29
29
|
files:
|
30
30
|
- lib/mad_mimi_mailer.rb
|
31
|
+
- lib/mad_mimi_mailable.rb
|
31
32
|
has_rdoc: true
|
32
33
|
homepage: http://developer.madmimi.com/
|
33
34
|
licenses: []
|