Class: MadMimiMailer

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

Constant Summary collapse

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



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

def call_api!(mail, method)
  response = post_request do |request|
    request.set_form_data(
      '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,
      'body' =>           mail.body.to_yaml
    )       
  end
  
  case response
  when Net::HTTPSuccess
    response
  else
    response.error!
  end
end

.deliver_mimi_mail(method, *parameters) ⇒ Object



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

def deliver_mimi_mail(method, *parameters)
  mail = new      
  mail.__send__(method, *parameters)
        
  return unless perform_deliveries

  if delivery_method == :test
    deliveries << mail
  else
    call_api!(mail, method)
  end
end

.method_missing(method_symbol, *parameters) ⇒ Object



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

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)


67
68
69
70
71
72
73
74
75
76
# File 'lib/mad_mimi_mailer.rb', line 67

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



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

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

#promotion(promotion = nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/mad_mimi_mailer.rb', line 12

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