Class: MadMimiMailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/mad_mimi_mailer.rb

Defined Under Namespace

Classes: MadMimiError, ValidationError

Constant Summary collapse

VERSION =
'0.0.8'
SINGLE_SEND_URL =
'https://madmimi.com/mailer'
@@api_settings =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call_api!(mail, method) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
# File 'lib/mad_mimi_mailer.rb', line 68

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),
    'from' =>           mail.from,
    'hidden' =>         serialize(mail.hidden)
  }

  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
    params['body'] = mail.body.to_yaml
  end


  # In certain instances e.g. MadMimi cannot find the specified promotion - then it will just hang
  # no error code returned or anything like that
  Timeout::timeout(5) do
    
    response = post_request do |request|
      request.set_form_data(params)
    end
  rescue Timeout::Error
    raise MadMimiError, "Mad Mimi is not responding"
  end
  


  

  case response
  when Net::HTTPSuccess
    response.body
  else
    response.error!
  end
end

.content_for(mail, content_type) ⇒ Object



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

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mad_mimi_mailer.rb', line 51

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
    call_api!(mail, method)
  end
end

.method_missing(method_symbol, *parameters) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/mad_mimi_mailer.rb', line 43

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 {|request| ... } ⇒ Object

Yields:

  • (request)


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

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

.serialize(recipients) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mad_mimi_mailer.rb', line 141

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



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

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



31
32
33
34
35
36
37
# File 'lib/mad_mimi_mailer.rb', line 31

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

#promotion(promotion = nil) ⇒ Object

Custom Mailer attributes



15
16
17
18
19
20
21
# File 'lib/mad_mimi_mailer.rb', line 15

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

#use_erb(use_erb = nil) ⇒ Object



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

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