kev_the_dev_mad_mimi_mailer 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mad_mimi_mailer.rb +167 -0
- metadata +56 -0
@@ -0,0 +1,167 @@
|
|
1
|
+
require "action_mailer"
|
2
|
+
require "net/http"
|
3
|
+
require "net/https"
|
4
|
+
require 'timeout'
|
5
|
+
|
6
|
+
class MadMimiMailer < ActionMailer::Base
|
7
|
+
VERSION = '0.0.8'
|
8
|
+
SINGLE_SEND_URL = 'https://madmimi.com/mailer'
|
9
|
+
|
10
|
+
@@api_settings = {}
|
11
|
+
cattr_accessor :api_settings
|
12
|
+
|
13
|
+
# Custom Mailer attributes
|
14
|
+
|
15
|
+
def promotion(promotion = nil)
|
16
|
+
if promotion.nil?
|
17
|
+
@promotion
|
18
|
+
else
|
19
|
+
@promotion = promotion
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def use_erb(use_erb = nil)
|
24
|
+
if use_erb.nil?
|
25
|
+
@use_erb
|
26
|
+
else
|
27
|
+
@use_erb = use_erb
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def hidden(hidden = nil)
|
32
|
+
if hidden.nil?
|
33
|
+
@hidden
|
34
|
+
else
|
35
|
+
@hidden = hidden
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Class methods
|
40
|
+
|
41
|
+
class << self
|
42
|
+
|
43
|
+
def method_missing(method_symbol, *parameters)
|
44
|
+
if method_symbol.id2name.match(/^deliver_(mimi_[_a-z]\w*)/)
|
45
|
+
deliver_mimi_mail($1, *parameters)
|
46
|
+
else
|
47
|
+
super
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def deliver_mimi_mail(method, *parameters)
|
52
|
+
mail = new
|
53
|
+
mail.__send__(method, *parameters)
|
54
|
+
|
55
|
+
if mail.use_erb
|
56
|
+
mail.create!(method, *parameters)
|
57
|
+
end
|
58
|
+
|
59
|
+
return unless perform_deliveries
|
60
|
+
|
61
|
+
if delivery_method == :test
|
62
|
+
deliveries << (mail.mail ? mail.mail : mail)
|
63
|
+
else
|
64
|
+
call_api!(mail, method)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def call_api!(mail, method)
|
69
|
+
params = {
|
70
|
+
'username' => api_settings[:username],
|
71
|
+
'api_key' => api_settings[:api_key],
|
72
|
+
'promotion_name' => mail.promotion || method.to_s.sub(/^mimi_/, ''),
|
73
|
+
'recipients' => serialize(mail.recipients),
|
74
|
+
'subject' => mail.subject,
|
75
|
+
'bcc' => serialize(mail.bcc),
|
76
|
+
'from' => mail.from,
|
77
|
+
'hidden' => serialize(mail.hidden)
|
78
|
+
}
|
79
|
+
|
80
|
+
if mail.use_erb
|
81
|
+
if mail.parts.any?
|
82
|
+
params['raw_plain_text'] = content_for(mail, "text/plain")
|
83
|
+
params['raw_html'] = content_for(mail, "text/html") { |html| validate(html.body) }
|
84
|
+
else
|
85
|
+
validate(mail.body)
|
86
|
+
params['raw_html'] = mail.body
|
87
|
+
end
|
88
|
+
else
|
89
|
+
params['body'] = mail.body.to_yaml
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
# In certain instances e.g. MadMimi cannot find the specified promotion - then it will just hang
|
94
|
+
# no error code returned or anything like that
|
95
|
+
Timeout::timeout(5) do
|
96
|
+
|
97
|
+
response = post_request do |request|
|
98
|
+
request.set_form_data(params)
|
99
|
+
end
|
100
|
+
rescue Timeout::Error
|
101
|
+
raise MadMimiError, "Mad Mimi is not responding"
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
case response
|
109
|
+
when Net::HTTPSuccess
|
110
|
+
response.body
|
111
|
+
else
|
112
|
+
response.error!
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def content_for(mail, content_type)
|
117
|
+
part = mail.parts.detect {|p| p.content_type == content_type }
|
118
|
+
if part
|
119
|
+
yield(part) if block_given?
|
120
|
+
part.body
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def validate(content)
|
125
|
+
unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]")
|
126
|
+
raise ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def post_request
|
131
|
+
url = URI.parse(SINGLE_SEND_URL)
|
132
|
+
request = Net::HTTP::Post.new(url.path)
|
133
|
+
yield(request)
|
134
|
+
http = Net::HTTP.new(url.host, url.port)
|
135
|
+
http.use_ssl = true
|
136
|
+
http.start do |http|
|
137
|
+
http.request(request)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def serialize(recipients)
|
142
|
+
case recipients
|
143
|
+
when String
|
144
|
+
recipients
|
145
|
+
when Array
|
146
|
+
recipients.join(", ")
|
147
|
+
when NilClass
|
148
|
+
nil
|
149
|
+
else
|
150
|
+
raise "Please provide a String or an Array for recipients or bcc."
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
class ValidationError < StandardError; end
|
156
|
+
class MadMimiError < StandardError; end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Adding the response body to HTTPResponse errors to provide better error messages.
|
160
|
+
module Net
|
161
|
+
class HTTPResponse
|
162
|
+
def error!
|
163
|
+
message = @code + ' ' + @message.dump + ' ' + body
|
164
|
+
raise error_type().new(message, self)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kev_the_dev_mad_mimi_mailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dave Hoover
|
8
|
+
- Kevin Edwards
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2010-01-12 00:00:00 +00:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Use Mad Mimi to send beautiful HTML emails using the ActionMailer API.
|
18
|
+
email: dave@obtiva.com kev.j.edwards@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- lib/mad_mimi_mailer.rb
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://developer.madmimi.com/
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "1.2"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.5
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Extending ActionMailer::Base for Mad Mimi integration.
|
55
|
+
test_files: []
|
56
|
+
|