Module: MadMimiTwo::MadMimiMailer

Included in:
MadMimiMessage
Defined in:
lib/mad_mimi_two/mad_mimi_mailer.rb

Constant Summary collapse

SINGLE_SEND_URL =
'https://api.madmimi.com/mailer'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 16

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

Instance Method Details

#call_api!Object



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
125
126
127
128
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 88

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)
  }
  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



20
21
22
23
24
25
26
27
28
29
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 20

def check_status(msg_id)
  url = "#{SINGLE_SEND_URL}s/status/#{msg_id}?#{MadMimiTwo::MadMimiMessage.api_settings.madmimiurlencode}"
  begin
    client= HTTPClient.new
    res=client.get_content(url)
  rescue HTTPClient::BadResponseError
    res="problem retrieving status"
  end
  res
end

#content_for(content_type) ⇒ Object



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

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 71

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



48
49
50
51
52
53
54
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 48

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

#hidden(hidden = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 56

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

#post_request {|request| ... } ⇒ Object

Yields:

  • (request)


144
145
146
147
148
149
150
151
152
153
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 144

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



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

def promotion(promo = nil)
  if promo.nil?
    @promotion
  else
    @promotion = promo #promotion
  end
end

#serialize(recipients) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 155

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



64
65
66
67
68
69
70
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 64

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

#use_erb(use_erb = nil) ⇒ Object



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

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

#use_erb?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 168

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

#validate(content) ⇒ Object



138
139
140
141
142
# File 'lib/mad_mimi_two/mad_mimi_mailer.rb', line 138

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