Class: MoteSMS::ActionMailerTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/mote_sms/transports/action_mailer_transport.rb

Overview

MoteSMS::ActionMailerTransport provides a transport implementation which can be used in development to forward SMS as e-mails. This allows to test sending SMSes to an e-mail endpoint.

Examples:

# => forwards all SMS to [email protected]
MoteSMS.transport = MoteSMS::ActionMailerTransport.new "[email protected]"

# => also accepts a Proc as recipient
MoteSMS.transport = MoteSMS::ActionMailerTransport.new ->(msg) { "#{msg.from}@example.com" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipient, default_options = {}) ⇒ ActionMailerTransport

Public: Create a new ActionMailerTransport instance



32
33
34
35
# File 'lib/mote_sms/transports/action_mailer_transport.rb', line 32

def initialize(recipient, default_options = {})
  self.recipient = recipient
  self.default_options = default_options
end

Instance Attribute Details

#default_optionsObject

Public: Read/change the recipient used when delivering the message.

Read/change the from field used when delivering the message.

Also accepts a Proc.



29
30
31
# File 'lib/mote_sms/transports/action_mailer_transport.rb', line 29

def default_options
  @default_options
end

#recipientObject

Public: Read/change the recipient used when delivering the message.

Read/change the from field used when delivering the message.

Also accepts a Proc.



29
30
31
# File 'lib/mote_sms/transports/action_mailer_transport.rb', line 29

def recipient
  @recipient
end

Instance Method Details

#deliver(message, options = {}) ⇒ Object

Public: Sends message using ActionMailer to recipient.

message - The MoteSMS::Message instance to deliver. options - The Hash with additional, transport specific options.

Returns nothing.



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

def deliver(message, options = {})
  options = options.reverse_merge default_options
  to = recipient.respond_to?(:call) ? recipient.call(message) : recipient
  message.body message.body.encode(options[:charset]) if options[:charset].presence
  ActionMailerSMSMailer.forward_sms(to, message, options.compact).deliver_now
  message.to
end