Class: Messagebus::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/messagebus/mailer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Mailer


3
4
5
# File 'lib/messagebus/mailer.rb', line 3

def initialize(api_key)
  @client = MessagebusApi::Messagebus.new(api_key)
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings


7
8
9
# File 'lib/messagebus/mailer.rb', line 7

def settings
  @settings
end

Instance Method Details

#deliver(message) ⇒ Object


18
19
20
# File 'lib/messagebus/mailer.rb', line 18

def deliver(message)
  deliver!(message)
end

#deliver!(message) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/messagebus/mailer.rb', line 22

def deliver!(message)
  msg = {:toEmail => message.to.first, :subject => message.subject, :fromEmail => AppConfig.mail.sender_address, :fromName => from_header_parse(message[:from].to_s)}

  if message.multipart?
    msg[:plaintextBody] = message.text_part.body.to_s if message.text_part
    msg[:htmlBody] = message.html_part.body.to_s if message.html_part
  else
    msg[:plaintextBody] = message.body.to_s
    msg[:htmlBody] = message.body.to_s
  end

  begin
    @client.add_message(msg, true)
  rescue => message_bus_api_error
    raise "Messagebus API error=#{message_bus_api_error}, message=#{msg.inspect}"
  end
end

#from_header_parse(string) ⇒ Object


14
15
16
# File 'lib/messagebus/mailer.rb', line 14

def from_header_parse(string)
  string.split('<')[0].delete('"')
end

#new(*settings) ⇒ Object


9
10
11
12
# File 'lib/messagebus/mailer.rb', line 9

def new(*settings)
    self.settings = {}
    self
end