Module: MadMimiTwo::MadMimiMailer
- Included in:
- MadMimiMessage
- Defined in:
- lib/mad_mimi_two/mad_mimi_mailer.rb
Constant Summary collapse
- API_URL =
'https://api.madmimi.com/'
- API_URL_HTTP =
'http://api.madmimi.com/'
- SINGLE_SEND_URL =
"#{API_URL}mailer"
Class Method Summary collapse
Instance Method Summary collapse
- #add_email(list, email) ⇒ Object
- #call_api! ⇒ Object
-
#check_status(msg_id) ⇒ Object
check the status of a sent email.
- #content_for(content_type) ⇒ Object
- #deliver_mimi_message ⇒ Object
- #email_placeholders(ep = nil) ⇒ Object
- #get_audience_xml ⇒ Object
- #get_lists ⇒ Object
- #get_promotions ⇒ Object
-
#get_promotions_internal(page) ⇒ Object
/audience_lists/name_of_list/add?email=email_to_add get a hash of promotion names on mad mimi.
- #get_promotions_xml(page) ⇒ Object
- #hidden(hidden = nil) ⇒ Object
- #post_cmd(url, body) ⇒ Object
- #post_request {|request| ... } ⇒ Object
-
#promotion(promo = nil) ⇒ Object
Custom Mailer attributes.
-
#send_cmd(url) ⇒ Object
send http get url via http client wrapped with exception rescue code.
- #serialize(recipients) ⇒ Object
- #unconfirmed(value = nil) ⇒ Object
- #use_erb(use_erb = nil) ⇒ Object
- #use_erb? ⇒ Boolean
- #validate(content) ⇒ Object
Class Method Details
.included(base) ⇒ Object
21 22 23 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 21 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#add_email(list, email) ⇒ Object
60 61 62 63 64 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 60 def add_email(list,email) url="#{API_URL_HTTP}audience_lists/#{list}/add?email=#{email}&#{MadMimiTwo::MadMimiMessage.api_settings.madmimiurlencode}" #puts url xml_list=post_cmd(url,"") end |
#call_api! ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 163 def call_api!() params = { 'username' => MadMimiTwo::MadMimiMessage.api_settings[:username], 'api_key' => MadMimiTwo::MadMimiMessage.api_settings[:api_key], 'promotion_name' => self.promotion, # scott || method.to_s.sub(/^#{method_prefix}_/, ''), 'recipients' => serialize(self[:to].to_s.split(',')), #removed to_a, needs comma 'subject' => self[:subject].to_s, 'bcc' => serialize(self[:bcc].to_s.split(',') || MadMimiMailer.default_parameters[:bcc]), 'from' => (self[:from].to_s || MadMimiMailer.default_parameters[:from]), 'hidden' => serialize(self.hidden), 'reply_to' => self[:reply_to].to_s# if !self[:reply_to].nil? } params['body']= self.email_placeholders.to_yaml # puts "params: #{params.inspect} to string #{params.to_s}" #scott params['unconfirmed'] = '1' if mail.unconfirmed #scott) if use_erb?(mail) #scott if mail.parts.any? #scott params['raw_plain_text'] = content_for(mail, "text/plain") #scott params['raw_html'] = content_for(mail, "text/html") { |html| validate(html.body) } #scott else #scott validate(mail.body) #scott params['raw_html'] = mail.body #scott end #scott else #scott stringified_default_body = (MadMimiMailer.default_parameters[:body] || {}).stringify_keys! #scott stringified_mail_body = (mail.body || {}).stringify_keys! #scott body_hash = stringified_default_body.merge(stringified_mail_body) #scott params['body'] = body_hash.to_yaml #scott end response = post_request do |request| request.set_form_data(params) end case response when Net::HTTPSuccess response.body else response.error! end end |
#check_status(msg_id) ⇒ Object
check the status of a sent email
100 101 102 103 104 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 100 def check_status(msg_id) url = "#{SINGLE_SEND_URL}s/status/#{msg_id}?#{MadMimiTwo::MadMimiMessage.api_settings.madmimiurlencode}" #puts url send_cmd(url) end |
#content_for(content_type) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 206 def content_for( content_type) part = self.parts.detect {|p| p.content_type == content_type } if part yield(part) if block_given? part.body end end |
#deliver_mimi_message ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 146 def () mail = self return unless perform_deliveries if delivery_method == :test deliveries << (mail.mail ? mail.mail : mail) else if (all_recipients = mail[:to]).is_a? Array all_recipients.each do |recipient| mail.recipients = recipient call_api!() end else call_api!() end end end |
#email_placeholders(ep = nil) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 123 def email_placeholders(ep = nil) if ep.nil? @email_placeholders else @email_placeholders = ep end end |
#get_audience_xml ⇒ Object
55 56 57 58 59 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 55 def get_audience_xml url="#{API_URL_HTTP}audience_lists/lists.xml?#{MadMimiTwo::MadMimiMessage.api_settings.madmimiurlencode}" #puts url xml_list=send_cmd(url) end |
#get_lists ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 89 def get_lists xml_list=get_audience_xml res={'select list'=>'select list'} reader = Nokogiri::XML::Reader(xml_list) reader.each do |node| res=res.merge({ node.attribute('name') => node.attribute('id')}) # eventually will want hash end res end |
#get_promotions ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 79 def get_promotions res={'select promotion'=>'select promotion'} 1.upto(7) { |i| #only up to three pages now tmp=get_promotions_internal(i) puts "i is #{i} tmp size is #{tmp.size}" break if tmp.empty? res= res.merge(tmp) } res end |
#get_promotions_internal(page) ⇒ Object
/audience_lists/name_of_list/add?email=email_to_add get a hash of promotion names on mad mimi. Not certain why mad mimi returns mailing details in this call but we throw it away.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 67 def get_promotions_internal(page) xml_list=get_promotions_xml(page) res={} # puts "xml_list is #{xml_list}" if !xml_list.nil? && xml_list != 'problem retrieving status: nil' then reader = Nokogiri::XML::Reader(xml_list) reader.each do |node| res=res.merge({ node.attribute('name') => node.attribute('name') }) if node.name=='promotion' # eventually will want hash end end res end |
#get_promotions_xml(page) ⇒ Object
51 52 53 54 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 51 def get_promotions_xml(page) url="#{API_URL}promotions.xml?page=#{page}&#{MadMimiTwo::MadMimiMessage.api_settings.madmimiurlencode}" xml_list=send_cmd(url) end |
#hidden(hidden = nil) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 131 def hidden(hidden = nil) if hidden.nil? @hidden else @hidden = hidden end end |
#post_cmd(url, body) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 42 def post_cmd(url,body) begin client= HTTPClient.new res=client.post_content(url,body) rescue HTTPClient::BadResponseError res="problem posting status: #{res.inspect}" end res end |
#post_request {|request| ... } ⇒ Object
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 220 def post_request url = URI.parse(SINGLE_SEND_URL) request = Net::HTTP::Post.new(url.path) yield(request) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.start do |http| http.request(request) end end |
#promotion(promo = nil) ⇒ Object
Custom Mailer attributes
108 109 110 111 112 113 114 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 108 def promotion(promo = nil) if promo.nil? @promotion else @promotion = promo #promotion end end |
#send_cmd(url) ⇒ Object
send http get url via http client wrapped with exception rescue code
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 25 def send_cmd(url) flag=true begin client= HTTPClient.new client.receive_timeout(100) res=client.get_content(url) rescue HTTPClient::BadResponseError res="problem retrieving status: #{res.inspect}" rescue HTTPClient::ReceiveTimeoutError res="problem retrieving status: #{res.inspect}" res=client.get_content(url) if flag flag=false end res end |
#serialize(recipients) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 231 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 |
#unconfirmed(value = nil) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 139 def unconfirmed(value = nil) if value.nil? @unconfirmed else @unconfirmed = value end end |
#use_erb(use_erb = nil) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 116 def use_erb(use_erb = nil) if use_erb.nil? @use_erb else @use_erb = use_erb end end |
#use_erb? ⇒ Boolean
244 245 246 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 244 def use_erb?() self.use_erb || use_erb end |
#validate(content) ⇒ Object
214 215 216 217 218 |
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 214 def validate(content) unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]") raise MadMimiMailer::ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]" end end |