Sha256: 85310c060a90d98bb70a1e953124852c8c7c130ae52c05aec4362ddd4cd0c8dc

Contents?: true

Size: 1.25 KB

Versions: 90

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module ActionMailbox
  # Encapsulates a route, which can then be matched against an inbound_email and provide a lookup of the matching
  # mailbox class. See examples for the different route addresses and how to use them in the +ActionMailbox::Base+
  # documentation.
  class Router::Route
    attr_reader :address, :mailbox_name

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

      ensure_valid_address
    end

    def match?(inbound_email)
      case address
      when :all
        true
      when String
        inbound_email.mail.recipients.any? { |recipient| address.casecmp?(recipient) }
      when Regexp
        inbound_email.mail.recipients.any? { |recipient| address.match?(recipient) }
      when Proc
        address.call(inbound_email)
      else
        address.match?(inbound_email)
      end
    end

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

    private
      def ensure_valid_address
        unless [ Symbol, String, Regexp, Proc ].any? { |klass| address.is_a?(klass) } || address.respond_to?(:match?)
          raise ArgumentError, "Expected a Symbol, String, Regexp, Proc, or matchable, got #{address.inspect}"
        end
      end
  end
end

Version data entries

90 entries across 88 versions & 7 rubygems

Version Path
actionmailbox-6.1.7.10 lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.9 lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.8 lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.7 lib/action_mailbox/router/route.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionmailbox-6.1.6.1/lib/action_mailbox/router/route.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionmailbox-7.0.2.3/lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.6 lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.5 lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.4 lib/action_mailbox/router/route.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionmailbox-6.1.6.1/lib/action_mailbox/router/route.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionmailbox-7.0.2.3/lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.3 lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.2 lib/action_mailbox/router/route.rb
actionmailbox-6.1.7.1 lib/action_mailbox/router/route.rb
actionmailbox-6.0.6.1 lib/action_mailbox/router/route.rb
actionmailbox-6.1.7 lib/action_mailbox/router/route.rb
actionmailbox-6.0.6 lib/action_mailbox/router/route.rb
actionmailbox-6.1.6.1 lib/action_mailbox/router/route.rb
actionmailbox-6.0.5.1 lib/action_mailbox/router/route.rb
actionmailbox-6.0.5 lib/action_mailbox/router/route.rb