Class: Postal::Message

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

Constant Summary collapse

ATTRIBUTES =

Set a has of all the attributes from the API that should be exposed through the Message class.

{
  :status => [:status, :status],
  :last_delivery_attempt => [:status, :last_delivery_attempt, :timestamp],
  :held? => [:status, :held, :boolean],
  :hold_expiry => [:status, :hold_expiry, :timestamp],
  :rcpt_to => [:details, :rcpt_to],
  :mail_from => [:details, :mail_from],
  :subject => [:details, :subject],
  :message_id => [:details, :message_id],
  :timestamp => [:details, :timestamp, :timestamp],
  :direction => [:details, :direction],
  :size => [:details, :size],
  :bounce? => [:details, :bounce, :boolean],
  :bounce_for_id => [:details, :bounce],
  :tag => [:details, :tag],
  :received_with_ssl? => [:details, :received_with_ssl, :boolean],
  :inspected? => [:inspection, :inspected, :boolean],
  :spam? => [:inspection, :spam, :boolean],
  :spam_score => [:inspection, :spam_score],
  :threat? => [:inspection, :thret, :boolean],
  :threat_details => [:inspection, :threat_details],
  :plain_body => [:plain_body],
  :html_body => [:html_body],
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes) ⇒ Message

 Initialize a new message object with the client and a set of initial attributes.



37
38
39
40
# File 'lib/postal/message.rb', line 37

def initialize(client, attributes)
  @client = client
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Catch calls to any of the default attributes for a message and return the data however we’d like it



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/postal/message.rb', line 89

def method_missing(name, *args, &block)
  if mapping = ATTRIBUTES[name.to_sym]
    expansion, attribute, type = mapping
    value = from_expansion(expansion, attribute)
    case type
    when :timestamp
      value ? Time.at(value) : nil
    when :boolean
      value == 1
    else
      value
    end
  else
    super
  end
end

Class Method Details

.find_with_scope(scope, id) ⇒ Object

Find a specific messsage with the given scope



11
12
13
14
15
16
17
18
19
20
# File 'lib/postal/message.rb', line 11

def self.find_with_scope(scope, id)
  api = scope.client.moonrope.messages.message(:id => id.to_i, :_expansions => scope.expansions)
  if api.success?
    Message.new(scope.client, api.data)
  elsif api.status == 'error' && api.data['code'] == 'MessageNotFound'
    raise MessageNotFound.new(id)
  else
    raise Error, "Couldn't load message from API (#{api.data})"
  end
end

.method_missing(name, *args, &block) ⇒ Object

If methods are called directly on the Message class, we likely want to see if we can run them through the global client message scope.



26
27
28
29
30
31
32
# File 'lib/postal/message.rb', line 26

def self.method_missing(name, *args, &block)
  if MessageScope.instance_methods(false).include?(name)
    Postal::Client.instance.messages.send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

#attachmentsObject

Return an array of attachment objects



117
118
119
120
121
# File 'lib/postal/message.rb', line 117

def attachments
  @attachments ||= from_expansion(:attachments).map do |a|
    Attachment.new(a)
  end
end

#headersObject

Return a set of headers which can be queried like a hash however looking up values using [] will be case-insensitive.



110
111
112
# File 'lib/postal/message.rb', line 110

def headers
  @headers ||= HeaderSet.new(from_expansion(:headers))
end

#idObject

Return the message ID



45
46
47
# File 'lib/postal/message.rb', line 45

def id
  @attributes['id']
end

#raw_messageObject

Return the full raw message



126
127
128
# File 'lib/postal/message.rb', line 126

def raw_message
  @raw_message ||= Base64.decode64(from_expansion(:raw_message))
end

#tokenObject

Return the message token



52
53
54
# File 'lib/postal/message.rb', line 52

def token
  @attributes['token']
end