Class: Mailflow::Message

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

Constant Summary collapse

ATTRIBUTES =
{
  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, :threat, :boolean],
  threat_details: [:inspection, :threat_details],
  plain_body: [:plain_body],
  html_body: [:html_body]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes) ⇒ Message

Returns a new instance of Message.



28
29
30
31
32
33
34
35
36
# File 'lib/mailflow/message.rb', line 28

def initialize(client, attributes)
  @client = client
  @attributes = attributes
  @id = @attributes['id']
  @token = @attributes['token']
  @headers = HeaderSet.new(from_expansion(:headers))
  @attachments = from_expansion(:attachments).map { |a| Attachment.new(a) }
  @raw_message = Base64.decode64(from_expansion(:raw_message))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mailflow/message.rb', line 63

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

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



7
8
9
# File 'lib/mailflow/message.rb', line 7

def attachments
  @attachments
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/mailflow/message.rb', line 7

def headers
  @headers
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/mailflow/message.rb', line 7

def id
  @id
end

#raw_messageObject (readonly)

Returns the value of attribute raw_message.



7
8
9
# File 'lib/mailflow/message.rb', line 7

def raw_message
  @raw_message
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/mailflow/message.rb', line 7

def token
  @token
end

Class Method Details

.find_with_scope(scope, id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/mailflow/message.rb', line 9

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



20
21
22
23
24
25
26
# File 'lib/mailflow/message.rb', line 20

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