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.
Files changed (2) hide show
  1. data/lib/mad_mimi_mailer.rb +173 -0
  2. metadata +56 -0
@@ -0,0 +1,173 @@
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
+ response = post_request do |request|
96
+ request.set_form_data(params)
97
+ end
98
+
99
+
100
+
101
+
102
+ case response
103
+ when Net::HTTPSuccess
104
+ response.body
105
+ else
106
+ response.error!
107
+ end
108
+ end
109
+
110
+ def content_for(mail, content_type)
111
+ part = mail.parts.detect {|p| p.content_type == content_type }
112
+ if part
113
+ yield(part) if block_given?
114
+ part.body
115
+ end
116
+ end
117
+
118
+ def validate(content)
119
+ unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]")
120
+ raise ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
121
+ end
122
+ end
123
+
124
+ def post_request
125
+ url = URI.parse(SINGLE_SEND_URL)
126
+ request = Net::HTTP::Post.new(url.path)
127
+ yield(request)
128
+ http = Net::HTTP.new(url.host, url.port)
129
+ http.use_ssl = true
130
+
131
+ begin
132
+
133
+ Timeout::timeout(5) do
134
+ http.start do |http|
135
+ http.request(request)
136
+ end
137
+ end
138
+ rescue Timeout::Error
139
+ #handle stuff here
140
+ raise MadMimiError, "Mad Mimi is not responding"
141
+ end
142
+
143
+
144
+
145
+ end
146
+
147
+ def serialize(recipients)
148
+ case recipients
149
+ when String
150
+ recipients
151
+ when Array
152
+ recipients.join(", ")
153
+ when NilClass
154
+ nil
155
+ else
156
+ raise "Please provide a String or an Array for recipients or bcc."
157
+ end
158
+ end
159
+ end
160
+
161
+ class ValidationError < StandardError; end
162
+ class MadMimiError < StandardError; end
163
+ end
164
+
165
+ # Adding the response body to HTTPResponse errors to provide better error messages.
166
+ module Net
167
+ class HTTPResponse
168
+ def error!
169
+ message = @code + ' ' + @message.dump + ' ' + body
170
+ raise error_type().new(message, self)
171
+ end
172
+ end
173
+ 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
+