Class: MadMimiMailer

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

Defined Under Namespace

Classes: ValidationError

Constant Summary collapse

VERSION =
'0.0.5'
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



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

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
    unless mail.body.include?("[[peek_image]]")
      raise ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
    end
    params['raw_html'] = mail.body
  else
    params['body'] = mail.body.to_yaml
  end
  
  response = post_request do |request|
    request.set_form_data(params)
  end
  
  case response
  when Net::HTTPSuccess
    response
  else
    response.error!
  end
end

.deliver_mimi_mail(method, *parameters) ⇒ Object



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

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

.method_missing(method_symbol, *parameters) ⇒ Object



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

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)


101
102
103
104
105
106
107
108
109
110
# File 'lib/mad_mimi_mailer.rb', line 101

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



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mad_mimi_mailer.rb', line 112

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

Instance Method Details

#hidden(hidden = nil) ⇒ Object



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

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

#promotion(promotion = nil) ⇒ Object

Custom Mailer attributes



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

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

#use_erb(use_erb = nil) ⇒ Object



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

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