Class: Howlr::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/howlr/message.rb

Defined Under Namespace

Classes: CallbackError, Recipient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Message

Initializes a new message with the given data. params should be a hash with values for the various Message attributes.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/howlr/message.rb', line 28

def initialize(data)
  self.from       = data[:from]
  self.subject    = data[:subject]
  self.body       = data[:body]
  self.author     = data[:author]
  self.content_type = data[:content_type] || 'text/plain'
  
  self.callback_url      = data[:callback_url]
  self.callback_method   = data[:callback_method]
  self.callback_username = data[:callback_username]
  self.callback_password = data[:callback_password]
  
  @recipients     = parse_recipients(data[:recipients])
  @id = "#{Time.now.to_i}#{rand(9999999)}".to_i
  @delivery_errors = []
  @sent = nil
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



21
22
23
# File 'lib/howlr/message.rb', line 21

def author
  @author
end

#bodyObject

Returns the value of attribute body.



21
22
23
# File 'lib/howlr/message.rb', line 21

def body
  @body
end

#callback_methodObject

Returns the value of attribute callback_method.



22
23
24
# File 'lib/howlr/message.rb', line 22

def callback_method
  @callback_method
end

#callback_passwordObject

Returns the value of attribute callback_password.



22
23
24
# File 'lib/howlr/message.rb', line 22

def callback_password
  @callback_password
end

#callback_urlObject

Returns the value of attribute callback_url.



22
23
24
# File 'lib/howlr/message.rb', line 22

def callback_url
  @callback_url
end

#callback_usernameObject

Returns the value of attribute callback_username.



22
23
24
# File 'lib/howlr/message.rb', line 22

def callback_username
  @callback_username
end

#content_typeObject

Returns the value of attribute content_type.



21
22
23
# File 'lib/howlr/message.rb', line 21

def content_type
  @content_type
end

#delivery_errorsObject (readonly)

Returns the value of attribute delivery_errors.



24
25
26
# File 'lib/howlr/message.rb', line 24

def delivery_errors
  @delivery_errors
end

#fromObject

Returns the value of attribute from.



21
22
23
# File 'lib/howlr/message.rb', line 21

def from
  @from
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/howlr/message.rb', line 24

def id
  @id
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



24
25
26
# File 'lib/howlr/message.rb', line 24

def recipients
  @recipients
end

#sentObject (readonly)

Returns the value of attribute sent.



24
25
26
# File 'lib/howlr/message.rb', line 24

def sent
  @sent
end

#subjectObject

Returns the value of attribute subject.



21
22
23
# File 'lib/howlr/message.rb', line 21

def subject
  @subject
end

Instance Method Details

#callback(recipient, result) ⇒ Object

If the message has a callback_url, this callback method is triggered for each delivery attempt (i.e. for every recipient, when #send is called).

The callback sends a REST request to the given callback_url with the given callback_method, submitting information about the result of the delivery attempt. Have a look at this method’s body for information about what data is submitted with the request.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/howlr/message.rb', line 97

def callback(recipient, result)
  auth = {}
  auth[:username] = callback_username if callback_username
  auth[:password] = callback_password if callback_password
  
  data = {}
  data[:message_id] = id
  data[:recipient_address] = recipient.address
  data[:recipient_protocol] = recipient.deliverer.protocol
  data[:send_success] = result
  data[:send_datetime] = sent
  data[:send_timestamp] = sent.to_i if sent.kind_of? Time
  data[:delivery_errors] = delivery_errors.join("; ")
  
  Restr.do(callback_method, callback_url, data, auth)
end

#parse_recipients(raw_recipients) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/howlr/message.rb', line 46

def parse_recipients(raw_recipients)
  $LOG.debug "Parsing recipients: #{raw_recipients.inspect}"
  
  raw_recipients = raw_recipients.split(/[,;]/) if raw_recipients.kind_of?(String)
  recipients = []
  raw_recipients.each do |raw|
    recipients << Recipient.parse(raw)
  end
  
  return recipients
end

#sendObject

Delivers the message to each recipient, stores the result of the overall delivery in @sent, and triggers the callback if a callback_url is present.

After running this, @sent will contain one of the following values:

nil

Nothing was not sent (the recipients list is probably empty)

Time

The message was sent successfully to all recipients at this time

false

There was a problem sending the message to at least one recipient. Check delivery_errors for more information about what caused the failure.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/howlr/message.rb', line 66

def send
  recipients.each do |r|
    next if r.nil?
    
    msg = self.dup
    msg.instance_variable_set(:@recipients, [r])
    
    $LOG.debug "Sending: #{msg.inspect}"
    
    result = r.deliverer.deliver(msg)
    @sent = Time.now.to_s if (result && (@sent || @sent.nil?))
    
    begin
      callback(r, result) unless callback_url.nil? || callback_url.blank?
    rescue => e
      $LOG.error("Error during callback: #{e}")
      e2 = CallbackError.new("Error during callback: #{e}")
      e2.underlying_error = e
      raise e2
    end
  end
end

#to_xml(options = {}) ⇒ Object

Serializes this Message into XML and returns a Builder::XmlMarkup object.

You should be able to call #to_s on the returned Builder::XmlMarkup object to get the XML as a String.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/howlr/message.rb', line 119

def to_xml(options = {})
  xml = options[:builder] ||= Builder::XmlMarkup.new(options)
  xml.instruct! unless options[:skip_instruct]
  
  xml.message(id ? {:id => id} : {}) do
    xml.recipients do
      recipients.each{|r| r.to_xml(options)}
    end
    
    h = {:from => from, :subject => subject, :author => author}
    h.each do |k,v|
      xml.tag!(k, v)
    end
    
    xml.sent sent
    
    unless sent.nil?
      xml.delivery_errors do
        delivery_errors.each do |err|
          xml.delivery_error err
        end
      end
    end
    
    xml.body {xml.cdata! body}
  end
end