Class: Outbox::Messages::Base

Inherits:
Object
  • Object
show all
Includes:
Outbox::MessageClients, Outbox::MessageFields
Defined in:
lib/outbox/messages/base.rb

Direct Known Subclasses

Email

Instance Method Summary collapse

Methods included from Outbox::MessageFields

#[], #[]=, #fields, #fields=, included, #validate_fields

Methods included from Outbox::MessageClients

#client, included

Constructor Details

#initialize(fields = nil, &block) ⇒ Base

Make a new message. Every message type can be created using a hash, block, or direct assignment.

message = Email.new to: '[email protected]', from: '[email protected]'
message = Email.new do
  to '[email protected]'
  from '[email protected]'
end
message = Email.new
message.to = '[email protected]'
message.from = '[email protected]'


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/outbox/messages/base.rb', line 18

def initialize(fields = nil, &block)
  @fields = {}
  @client = self.class.default_client and self.class.default_client.dup

  self.fields = self.class.defaults

  if block_given?
    instance_eval(&block)
  else
    self.fields = fields unless fields.nil?
  end
end

Instance Method Details

#audience=(audience) ⇒ Object

Sets the ‘audience’ for this message. All message types must implement this method. By default, this is an alias for a ‘to’ field if present.



33
34
35
# File 'lib/outbox/messages/base.rb', line 33

def audience=(audience)
  self.to = audience if self.respond_to?(:to=)
end

#deliver(audience = nil) ⇒ Object

Validates the current message and delivers the message using the defined client.



39
40
41
42
43
# File 'lib/outbox/messages/base.rb', line 39

def deliver(audience = nil)
  self.audience = audience if audience
  validate_fields
  client.deliver(self)
end