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



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

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



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
196
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 155

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



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

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



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

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



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

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



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

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

#get_audience_xmlObject



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

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



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

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



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

def get_promotions
  res={'select promotion'=>'select promotion'}
  1.upto(5)  { |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.



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

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



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

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



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

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

#post_cmd(url, body) ⇒ Object



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

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)


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

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



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

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
33
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 24

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

#serialize(recipients) ⇒ Object



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

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



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

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

#use_erb(use_erb = nil) ⇒ Object



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

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

#use_erb?Boolean

Returns:

  • (Boolean)


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

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

#validate(content) ⇒ Object



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

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