Class: Mail::Madmimi::Sender

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mail/madmimi/sender.rb

Defined Under Namespace

Classes: MadmimiError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Sender

Returns a new instance of Sender.



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

def initialize(settings = {})
  unless settings[:username] && settings[:api_key]
    raise MadmimiError, "Missing username or api_key"
  end

  self.settings = settings
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



7
8
9
# File 'lib/mail/madmimi/sender.rb', line 7

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



20
21
22
23
# File 'lib/mail/madmimi/sender.rb', line 20

def deliver!(mail)
  resp = self.class.post '/mailer', body: email_post_body(mail)
  parse_response(resp)
end

#email_post_body(mail) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mail/madmimi/sender.rb', line 32

def email_post_body(mail)
  options = settings.merge(
    recipients:     mail[:to].to_s.gsub(",", ";"),
    promotion_name: mail[:promotion_name].to_s,
    subject:        mail.subject.to_s,
    raw_html:       html_data(mail).to_s
  )

  options.merge!(from: mail[:from].to_s) unless mail[:from].to_s.empty?
  options.merge!(bcc:  mail[:bcc].to_s)  unless mail[:bcc].to_s.empty?

  boolean_params = [:check_suppressed, :track_links, :hidden,
    :skip_placeholders, :remove_unsubscribe]

  boolean_params.each do |param|
    options.merge!(param => mail[param].to_s) unless mail[param].to_s.empty?
  end

  options
end

#html_data(mail) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/mail/madmimi/sender.rb', line 53

def html_data(mail)
  html_data = mail.find_first_mime_type('text/html')

  if html_data
    html_data.body
  else
    mail.mime_type == 'text/html' ? mail.body : nil
  end
end

#parse_response(resp) ⇒ Object



25
26
27
28
29
30
# File 'lib/mail/madmimi/sender.rb', line 25

def parse_response(resp)
  case resp.headers["status"].to_i
    when 200..299 then resp.parsed_response
    else raise MadmimiError.new "status=#{resp.headers["status"]}; " + resp.parsed_response
  end
end