mysmallidea-mad_mimi_mailer 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/mad_mimi_mailer.rb +202 -0
  2. metadata +63 -0
@@ -0,0 +1,202 @@
1
+ require "action_mailer"
2
+ require "net/http"
3
+ require "net/https"
4
+
5
+ class MadMimiMailer < ActionMailer::Base
6
+ VERSION = '0.0.8'
7
+ SINGLE_SEND_URL = 'https://madmimi.com/mailer'
8
+
9
+ @@api_settings = {}
10
+ cattr_accessor :api_settings
11
+
12
+ @@default_parameters = {}
13
+ cattr_accessor :default_parameters
14
+
15
+ # Custom Mailer attributes
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
50
+
51
+ class << self
52
+
53
+ def method_missing(method_symbol, *parameters)
54
+ if method_symbol.id2name.match(/^deliver_(mimi_[_a-z]\w*)/)
55
+ deliver_mimi_mail($1, *parameters)
56
+ else
57
+ super
58
+ end
59
+ 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
+
127
+ # Add Audience List Membership
128
+ # POST to http://madmimi.com/audience_lists/NameOfList/add with 3 parameters:
129
+ # * Your Mad Mimi username
130
+ # * Your Mad Mimi API Key
131
+ # * email address of an existing audience member to add to the list
132
+ def add_audience_list_membership(email, list_name)
133
+ url = "http://madmimi.com/audience_lists/#{URI.escape(list_name)}/add"
134
+ params = {
135
+ 'username' => api_settings[:username],
136
+ 'api_key' => api_settings[:api_key],
137
+ 'email' => email
138
+ }
139
+ response = post_request(url) do |request|
140
+ request.set_form_data(params)
141
+ end
142
+
143
+ case response
144
+ when Net::HTTPSuccess
145
+ response.body
146
+ else
147
+ response.error!
148
+ end
149
+
150
+ end
151
+
152
+ def content_for(mail, content_type)
153
+ part = mail.parts.detect {|p| p.content_type == content_type }
154
+ if part
155
+ yield(part) if block_given?
156
+ part.body
157
+ end
158
+ end
159
+
160
+ def validate(content)
161
+ unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]")
162
+ raise ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
163
+ end
164
+ end
165
+
166
+ def post_request(url=SINGLE_SEND_URL)
167
+ uri = URI.parse(url)
168
+ request = Net::HTTP::Post.new(uri.path)
169
+ yield(request)
170
+ http = Net::HTTP.new(uri.host, uri.port)
171
+ http.use_ssl = uri.port == 443
172
+ http.start do |http|
173
+ http.request(request)
174
+ end
175
+ end
176
+
177
+ def serialize(recipients)
178
+ case recipients
179
+ when String
180
+ recipients
181
+ when Array
182
+ recipients.join(", ")
183
+ when NilClass
184
+ nil
185
+ else
186
+ raise "Please provide a String or an Array for recipients or bcc."
187
+ end
188
+ end
189
+ end
190
+
191
+ class ValidationError < StandardError; end
192
+ end
193
+
194
+ # Adding the response body to HTTPResponse errors to provide better error messages.
195
+ module Net
196
+ class HTTPResponse
197
+ def error!
198
+ message = @code + ' ' + @message.dump + ' ' + body
199
+ raise error_type().new(message, self)
200
+ end
201
+ end
202
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysmallidea-mad_mimi_mailer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 9
9
+ version: 0.0.9
10
+ platform: ruby
11
+ authors:
12
+ - Dave Hoover
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-09 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Use Mad Mimi to send beautiful HTML emails using the ActionMailer API.
22
+ email: dave@obtiva.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/mad_mimi_mailer.rb
31
+ has_rdoc: true
32
+ homepage: http://developer.madmimi.com/
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 1
53
+ - 2
54
+ version: "1.2"
55
+ requirements: []
56
+
57
+ rubyforge_project: mad_mimi_mailer
58
+ rubygems_version: 1.3.6
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Extending ActionMailer::Base for Mad Mimi integration.
62
+ test_files: []
63
+