Class: DefraRubyEmail::LastEmailCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/defra_ruby_email/last_email_cache.rb

Constant Summary collapse

EMAIL_ATTRIBUTES =
%i[date from to bcc cc reply_to subject].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_emailObject

Returns the value of attribute last_email.



9
10
11
# File 'lib/defra_ruby_email/last_email_cache.rb', line 9

def last_email
  @last_email
end

Instance Method Details

#email_bodyObject

If you’ve set multipart emails then you’ll have both a text and a html version (determined by adding the relevant erb views). If you do so then ‘my_mail.parts.length` will at least equal 2. If you only have one of them e.g. just a text version then parts doesn’t get populated.

However any attachments will cause ActionMailer to use parts. So for example if we have a text only email with an attached image, then parts will be of length 2; one being the content and the other being the attachment.

To cater for all possibilities we have this method to grab the body content guides.rubyonrails.org/action_mailer_basics.html#sending-multipart-emails stackoverflow.com/a/15818886



41
42
43
44
45
46
# File 'lib/defra_ruby_email/last_email_cache.rb', line 41

def email_body
  part_to_use = last_email.text_part || last_email.html_part || last_email

  # return the message body without the header information
  part_to_use.body.decoded
end

#last_email_jsonObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/defra_ruby_email/last_email_cache.rb', line 16

def last_email_json
  return JSON.generate(error: "No emails sent.") unless last_email.present?

  message_hash = {}
  EMAIL_ATTRIBUTES.each do |attribute|
    message_hash[attribute] = last_email.public_send(attribute)
  end
  message_hash[:body] = email_body
  message_hash[:attachments] = last_email.attachments.map(&:filename)

  JSON.generate(last_email: message_hash)
end

#resetObject

This is necessary to properly test the service functionality



12
13
14
# File 'lib/defra_ruby_email/last_email_cache.rb', line 12

def reset
  @last_email = nil
end