class Postal::Message
Constants
- ATTRIBUTES
Set a has of all the attributes from the API that should be exposed through the
Message
class.
Public Class Methods
find_with_scope(scope, id)
click to toggle source
Find a specific messsage with the given scope
# 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)
click to toggle source
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.
Calls superclass method
# 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
new(client, attributes)
click to toggle source
Initialize a new message object with the client and a set of initial attributes.
# File lib/postal/message.rb, line 37 def initialize(client, attributes) @client = client @attributes = attributes end
Public Instance Methods
attachments()
click to toggle source
Return an array of attachment objects
# File lib/postal/message.rb, line 117 def attachments @attachments ||= from_expansion(:attachments).map do |a| Attachment.new(a) end end
headers()
click to toggle source
Return a set of headers which can be queried like a hash however looking up values using [] will be case-insensitive.
# File lib/postal/message.rb, line 110 def headers @headers ||= HeaderSet.new(from_expansion(:headers)) end
id()
click to toggle source
Return the message ID
# File lib/postal/message.rb, line 45 def id @attributes['id'] end
method_missing(name, *args, &block)
click to toggle source
Catch calls to any of the default attributes for a message and return the data however we’d like it
Calls superclass method
# 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
raw_message()
click to toggle source
Return the full raw message
# File lib/postal/message.rb, line 126 def raw_message @raw_message ||= Base64.decode64(from_expansion(:raw_message)) end
token()
click to toggle source
Return the message token
# File lib/postal/message.rb, line 52 def token @attributes['token'] end
Private Instance Methods
from_expansion(expansion, attribute = nil, loaded = false)
click to toggle source
# File lib/postal/message.rb, line 132 def from_expansion(expansion, attribute = nil, loaded = false) if @attributes.has_key?(expansion.to_s) || loaded attribute ? @attributes[expansion.to_s][attribute.to_s] : @attributes[expansion.to_s] else load_expansions(expansion) from_expansion(expansion, attribute, true) end end
load_expansions(*names)
click to toggle source
# File lib/postal/message.rb, line 141 def load_expansions(*names) puts "\e[31mLoading expansion #{names}\e[0m" api = @client.moonrope.messages.message(:id => self.id, :_expansions => names) if api.success? names.each do |expansion_name| if api.data.has_key?(expansion_name.to_s) @attributes[expansion_name.to_s] = api.data[expansion_name.to_s] end end else raise Postal::Error, "Couldn't load expansion data (#{names.join(', ')}) for message ID '#{self.id}'" end end