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

Class Method Details

.included(base) ⇒ Object



20
21
22
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 20

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#add_email(list, email) ⇒ Object



51
52
53
54
55
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 51

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



154
155
156
157
158
159
160
161
162
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
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 154

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



91
92
93
94
95
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 91

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



197
198
199
200
201
202
203
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 197

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_messageObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 137

def deliver_mimi_message()
  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



114
115
116
117
118
119
120
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 114

def email_placeholders(ep = nil)
  if ep.nil?
    @email_placeholders
  else
    @email_placeholders = ep
  end
end

#get_audience_xmlObject



46
47
48
49
50
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 46

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_listsObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 80

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_promotionsObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 70

def get_promotions
  res={'select promotion'=>'select promotion'}
  1.upto(5)  { |i|   #only up to three pages now
    #puts "i is #{i}"
    tmp=get_promotions_internal(i)
    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.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 58

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



42
43
44
45
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 42

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



122
123
124
125
126
127
128
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 122

def hidden(hidden = nil)
  if hidden.nil?
    @hidden
  else
    @hidden = hidden
  end
end

#post_cmd(url, body) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 33

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

Yields:

  • (request)


211
212
213
214
215
216
217
218
219
220
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 211

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



99
100
101
102
103
104
105
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 99

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



24
25
26
27
28
29
30
31
32
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 24

def send_cmd(url)
  begin
      client= HTTPClient.new
      res=client.get_content(url)
    rescue HTTPClient::BadResponseError
      res="problem retrieving status: #{res.inspect}"
    end
    res
end

#serialize(recipients) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 222

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



130
131
132
133
134
135
136
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 130

def unconfirmed(value = nil)
  if value.nil?
    @unconfirmed
  else
    @unconfirmed = value
  end
end

#use_erb(use_erb = nil) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 107

def use_erb(use_erb = nil)
  if use_erb.nil?
    @use_erb
  else
    @use_erb = use_erb
  end
end

#use_erb?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 235

def use_erb?()
  self.use_erb || use_erb
end

#validate(content) ⇒ Object



205
206
207
208
209
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 205

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