From 6c168aaffb37c62dee6c895a22e240e5552be669 Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Wed, 26 Dec 2018 16:18:42 -0500 Subject: Nest Action Mailbox classes in the API docs --- .../app/models/action_mailbox/inbound_email.rb | 70 +++++++++++----------- .../inbound_email/incineratable/incineration.rb | 38 ++++++------ 2 files changed, 56 insertions(+), 52 deletions(-) (limited to 'actionmailbox/app/models/action_mailbox') diff --git a/actionmailbox/app/models/action_mailbox/inbound_email.rb b/actionmailbox/app/models/action_mailbox/inbound_email.rb index 88df7a929a..fbf75a6080 100644 --- a/actionmailbox/app/models/action_mailbox/inbound_email.rb +++ b/actionmailbox/app/models/action_mailbox/inbound_email.rb @@ -2,44 +2,46 @@ require "mail" -# The `InboundEmail` is an Active Record that keeps a reference to the raw email stored in Active Storage -# and tracks the status of processing. By default, incoming emails will go through the following lifecycle: -# -# * Pending: Just received by one of the ingress controllers and scheduled for routing. -# * Processing: During active processing, while a specific mailbox is running its #process method. -# * Delivered: Successfully processed by the specific mailbox. -# * Failed: An exception was raised during the specific mailbox's execution of the `#process` method. -# * Bounced: Rejected processing by the specific mailbox and bounced to sender. -# -# Once the `InboundEmail` has reached the status of being either `delivered`, `failed`, or `bounced`, -# it'll count as having been `#processed?`. Once processed, the `InboundEmail` will be scheduled for -# automatic incineration at a later point. -# -# When working with an `InboundEmail`, you'll usually interact with the parsed version of the source, -# which is available as a `Mail` object from `#mail`. But you can also access the raw source directly -# using the `#source` method. -# -# Examples: -# -# inbound_email.mail.from # => 'david@loudthinking.com' -# inbound_email.source # Returns the full rfc822 source of the email as text -class ActionMailbox::InboundEmail < ActiveRecord::Base - self.table_name = "action_mailbox_inbound_emails" +module ActionMailbox + # The `InboundEmail` is an Active Record that keeps a reference to the raw email stored in Active Storage + # and tracks the status of processing. By default, incoming emails will go through the following lifecycle: + # + # * Pending: Just received by one of the ingress controllers and scheduled for routing. + # * Processing: During active processing, while a specific mailbox is running its #process method. + # * Delivered: Successfully processed by the specific mailbox. + # * Failed: An exception was raised during the specific mailbox's execution of the `#process` method. + # * Bounced: Rejected processing by the specific mailbox and bounced to sender. + # + # Once the `InboundEmail` has reached the status of being either `delivered`, `failed`, or `bounced`, + # it'll count as having been `#processed?`. Once processed, the `InboundEmail` will be scheduled for + # automatic incineration at a later point. + # + # When working with an `InboundEmail`, you'll usually interact with the parsed version of the source, + # which is available as a `Mail` object from `#mail`. But you can also access the raw source directly + # using the `#source` method. + # + # Examples: + # + # inbound_email.mail.from # => 'david@loudthinking.com' + # inbound_email.source # Returns the full rfc822 source of the email as text + class InboundEmail < ActiveRecord::Base + self.table_name = "action_mailbox_inbound_emails" - include Incineratable, MessageId, Routable + include Incineratable, MessageId, Routable - has_one_attached :raw_email - enum status: %i[ pending processing delivered failed bounced ] + has_one_attached :raw_email + enum status: %i[ pending processing delivered failed bounced ] - def mail - @mail ||= Mail.from_source(source) - end + def mail + @mail ||= Mail.from_source(source) + end - def source - @source ||= raw_email.download - end + def source + @source ||= raw_email.download + end - def processed? - delivered? || failed? || bounced? + def processed? + delivered? || failed? || bounced? + end end end diff --git a/actionmailbox/app/models/action_mailbox/inbound_email/incineratable/incineration.rb b/actionmailbox/app/models/action_mailbox/inbound_email/incineratable/incineration.rb index 4656f359bf..685f7fceb6 100644 --- a/actionmailbox/app/models/action_mailbox/inbound_email/incineratable/incineration.rb +++ b/actionmailbox/app/models/action_mailbox/inbound_email/incineratable/incineration.rb @@ -1,24 +1,26 @@ # frozen_string_literal: true -# Command class for carrying out the actual incineration of the `InboundMail` that's been scheduled -# for removal. Before the incineration – which really is just a call to `#destroy!` – is run, we verify -# that it's both eligible (by virtue of having already been processed) and time to do so (that is, -# the `InboundEmail` was processed after the `incinerate_after` time). -class ActionMailbox::InboundEmail::Incineratable::Incineration - def initialize(inbound_email) - @inbound_email = inbound_email - end - - def run - @inbound_email.destroy! if due? && processed? - end - - private - def due? - @inbound_email.updated_at < ActionMailbox.incinerate_after.ago.end_of_day +module ActionMailbox + # Command class for carrying out the actual incineration of the `InboundMail` that's been scheduled + # for removal. Before the incineration – which really is just a call to `#destroy!` – is run, we verify + # that it's both eligible (by virtue of having already been processed) and time to do so (that is, + # the `InboundEmail` was processed after the `incinerate_after` time). + class InboundEmail::Incineratable::Incineration + def initialize(inbound_email) + @inbound_email = inbound_email end - def processed? - @inbound_email.processed? + def run + @inbound_email.destroy! if due? && processed? end + + private + def due? + @inbound_email.updated_at < ActionMailbox.incinerate_after.ago.end_of_day + end + + def processed? + @inbound_email.processed? + end + end end -- cgit v1.2.3