Class: DefraRubyEmail::LastEmailCache
- Inherits:
-
Object
- Object
- DefraRubyEmail::LastEmailCache
- 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
-
#last_email ⇒ Object
Returns the value of attribute last_email.
Instance Method Summary collapse
-
#email_body ⇒ Object
If you’ve set multipart emails then you’ll have both a text and a html version (determined by adding the relevant erb views).
- #last_email_json ⇒ Object
-
#reset ⇒ Object
This is necessary to properly test the service functionality.
Instance Attribute Details
#last_email ⇒ Object
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_body ⇒ Object
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_json ⇒ Object
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? = {} EMAIL_ATTRIBUTES.each do |attribute| [attribute] = last_email.public_send(attribute) end [:body] = email_body [:attachments] = last_email..map(&:filename) JSON.generate(last_email: ) end |
#reset ⇒ Object
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 |