Sha256: fee0fd170e187135626f57240cba45a9f4304e244560f61583df40a02ab548cd

Contents?: true

Size: 829 Bytes

Versions: 1

Compression:

Stored size: 829 Bytes

Contents

# frozen_string_literals: true

# +Jobshop::Mailman+
#
# A Mailman is responsible for handling incoming email and dispatching it to its
# approrpriate handler. If no handler is found, the email is rejected.
module Jobshop
  class Mailman < ApplicationRecord
    self.primary_keys = %i[ organization_id address ]

    belongs_to :organization, inverse_of: :mailmen

    validates :address, presence: true

    class << self
      def handler_for(message)
        to = Mail::Address.new(message.to.first)

        mailbox = to.local.split("+").first
        address = "#{mailbox}@#{to.domain}"
        mailman = find_by(address: address)

        klass = if mailman
          mailman.handler_type.constantize
        else
          Mailroom::NullHandler
        end

        klass.new(message, mailman)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jobshop-0.0.167 app/models/jobshop/mailman.rb