Class: MadMimiMailer
- Inherits:
-
ActionMailer::Base
- Object
- ActionMailer::Base
- MadMimiMailer
- Defined in:
- lib/mad_mimi_mailer.rb
Defined Under Namespace
Classes: ValidationError
Constant Summary collapse
- VERSION =
'0.0.8'
- SINGLE_SEND_URL =
'https://madmimi.com/mailer'
- @@api_settings =
{}
- @@default_parameters =
{}
Class Method Summary collapse
-
.add_audience_list_membership(email, list_name) ⇒ Object
Add Audience List Membership POST to madmimi.com/audience_lists/NameOfList/add with 3 parameters: * Your Mad Mimi username * Your Mad Mimi API Key * email address of an existing audience member to add to the list.
- .call_api!(mail, method) ⇒ Object
- .content_for(mail, content_type) ⇒ Object
- .deliver_mimi_mail(method, *parameters) ⇒ Object
- .method_missing(method_symbol, *parameters) ⇒ Object
- .post_request(url = SINGLE_SEND_URL) {|request| ... } ⇒ Object
- .serialize(recipients) ⇒ Object
- .validate(content) ⇒ Object
Instance Method Summary collapse
- #hidden(hidden = nil) ⇒ Object
-
#promotion(promotion = nil) ⇒ Object
Custom Mailer attributes.
- #unconfirmed(value = nil) ⇒ Object
- #use_erb(use_erb = nil) ⇒ Object
Class Method Details
.add_audience_list_membership(email, list_name) ⇒ Object
Add Audience List Membership POST to madmimi.com/audience_lists/NameOfList/add with 3 parameters:
-
Your Mad Mimi username
-
Your Mad Mimi API Key
-
email address of an existing audience member to add to the list
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mad_mimi_mailer.rb', line 132 def add_audience_list_membership(email, list_name) url = "http://madmimi.com/audience_lists/#{URI.escape(list_name)}/add" params = { 'username' => api_settings[:username], 'api_key' => api_settings[:api_key], 'email' => email } response = post_request(url) do |request| request.set_form_data(params) end case response when Net::HTTPSuccess response.body else response.error! end end |
.call_api!(mail, method) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mad_mimi_mailer.rb', line 85 def call_api!(mail, method) params = { 'username' => api_settings[:username], 'api_key' => api_settings[:api_key], 'promotion_name' => mail.promotion || method.to_s.sub(/^mimi_/, ''), 'recipients' => serialize(mail.recipients), 'subject' => mail.subject, 'bcc' => serialize(mail.bcc || default_parameters[:bcc]), 'from' => (mail.from || default_parameters[:from]), 'hidden' => serialize(mail.hidden) } params['unconfirmed'] = '1' if mail.unconfirmed if mail.use_erb if mail.parts.any? params['raw_plain_text'] = content_for(mail, "text/plain") params['raw_html'] = content_for(mail, "text/html") { |html| validate(html.body) } else validate(mail.body) params['raw_html'] = mail.body end else stringified_default_body = (default_parameters[:body] || {}).stringify_keys! stringified_mail_body = (mail.body || {}).stringify_keys! body_hash = stringified_default_body.merge(stringified_mail_body) params['body'] = body_hash.to_yaml end response = post_request do |request| request.set_form_data(params) end case response when Net::HTTPSuccess response.body else response.error! end end |
.content_for(mail, content_type) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/mad_mimi_mailer.rb', line 152 def content_for(mail, content_type) part = mail.parts.detect {|p| p.content_type == content_type } if part yield(part) if block_given? part.body end end |
.deliver_mimi_mail(method, *parameters) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mad_mimi_mailer.rb', line 61 def deliver_mimi_mail(method, *parameters) mail = new mail.__send__(method, *parameters) if mail.use_erb mail.create!(method, *parameters) end return unless perform_deliveries if delivery_method == :test deliveries << (mail.mail ? mail.mail : mail) else if (all_recipients = mail.recipients).is_a? Array all_recipients.each do |recipient| mail.recipients = recipient call_api!(mail, method) end else call_api!(mail, method) end end end |
.method_missing(method_symbol, *parameters) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/mad_mimi_mailer.rb', line 53 def method_missing(method_symbol, *parameters) if method_symbol.id2name.match(/^deliver_(mimi_[_a-z]\w*)/) deliver_mimi_mail($1, *parameters) else super end end |
.post_request(url = SINGLE_SEND_URL) {|request| ... } ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/mad_mimi_mailer.rb', line 166 def post_request(url=SINGLE_SEND_URL) uri = URI.parse(url) request = Net::HTTP::Post.new(uri.path) yield(request) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.port == 443 http.start do |http| http.request(request) end end |
.serialize(recipients) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mad_mimi_mailer.rb', line 177 def serialize(recipients) case recipients when String recipients when Array recipients.join(", ") when NilClass nil else raise "Please provide a String or an Array for recipients or bcc." end end |
.validate(content) ⇒ Object
160 161 162 163 164 |
# File 'lib/mad_mimi_mailer.rb', line 160 def validate(content) unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]") raise ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]" end end |
Instance Method Details
#hidden(hidden = nil) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/mad_mimi_mailer.rb', line 33 def hidden(hidden = nil) if hidden.nil? @hidden else @hidden = hidden end end |
#promotion(promotion = nil) ⇒ Object
Custom Mailer attributes
17 18 19 20 21 22 23 |
# File 'lib/mad_mimi_mailer.rb', line 17 def promotion(promotion = nil) if promotion.nil? @promotion else @promotion = promotion end end |
#unconfirmed(value = nil) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/mad_mimi_mailer.rb', line 41 def unconfirmed(value = nil) if value.nil? @unconfirmed else @unconfirmed = value end end |
#use_erb(use_erb = nil) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/mad_mimi_mailer.rb', line 25 def use_erb(use_erb = nil) if use_erb.nil? @use_erb else @use_erb = use_erb end end |