aboutsummaryrefslogblamecommitdiffstats
path: root/lib/action_mailbox/router/route.rb
blob: e123a93bb16b72d8336dd3e8720288cb5a538359 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
                                  










                                                
                                                                                          
               
                                                                                        







                                                                       
                                                      
     




                                                       
   
class ActionMailbox::Router::Route
  class InvalidAddressError < StandardError; end

  attr_reader :address, :mailbox_name

  def initialize(address, to:)
    @address, @mailbox_name = address, to
  end

  def match?(inbound_email)
    case address
    when String
      recipients_from(inbound_email.mail).any? { |recipient| address.casecmp?(recipient) }
    when Regexp
      recipients_from(inbound_email.mail).any? { |recipient| address.match?(recipient) }
    when Proc
      address.call(inbound_email)
    else
      address.try(:match?, inbound_email) || raise(InvalidAddressError)
    end
  end

  def mailbox_class
    "#{mailbox_name.to_s.camelize}Mailbox".constantize
  end

  private
    def recipients_from(mail)
      Array(mail.to) + Array(mail.cc) + Array(mail.bcc)
    end
end