Sha256: 3e29d34d311080f60cc55b4e2dd8dd47bf680aaf76a2bca6302236a0aeb13ad4

Contents?: true

Size: 949 Bytes

Versions: 39

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

module ActionMailbox
  # Encapsulates the routes that live on the ApplicationMailbox and performs the actual routing when
  # an inbound_email is received.
  class Router
    class RoutingError < StandardError; end

    def initialize
      @routes = []
    end

    def add_routes(routes)
      routes.each do |(address, mailbox_name)|
        add_route address, to: mailbox_name
      end
    end

    def add_route(address, to:)
      routes.append Route.new(address, to: to)
    end

    def route(inbound_email)
      if mailbox = match_to_mailbox(inbound_email)
        mailbox.receive(inbound_email)
      else
        inbound_email.bounced!

        raise RoutingError
      end
    end

    private
      attr_reader :routes

      def match_to_mailbox(inbound_email)
        routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class)
      end
  end
end

require "action_mailbox/router/route"

Version data entries

39 entries across 39 versions & 4 rubygems

Version Path
actionmailbox-6.0.6.1 lib/action_mailbox/router.rb
actionmailbox-6.0.6 lib/action_mailbox/router.rb
actionmailbox-6.0.5.1 lib/action_mailbox/router.rb
actionmailbox-6.0.5 lib/action_mailbox/router.rb
actionmailbox-6.0.4.8 lib/action_mailbox/router.rb
actionmailbox-6.0.4.7 lib/action_mailbox/router.rb
actionmailbox-6.0.4.6 lib/action_mailbox/router.rb
actionmailbox-6.0.4.5 lib/action_mailbox/router.rb
actionmailbox-6.0.4.4 lib/action_mailbox/router.rb
actionmailbox-6.0.4.3 lib/action_mailbox/router.rb
actionmailbox-6.0.4.2 lib/action_mailbox/router.rb
actionmailbox-6.0.4.1 lib/action_mailbox/router.rb
actionmailbox-6.0.4 lib/action_mailbox/router.rb
actionmailbox-6.0.3.7 lib/action_mailbox/router.rb
actionmailbox-6.0.3.6 lib/action_mailbox/router.rb
actionmailbox-6.0.3.5 lib/action_mailbox/router.rb
actionmailbox-6.0.3.4 lib/action_mailbox/router.rb
actionmailbox-6.0.3.3 lib/action_mailbox/router.rb
actionmailbox-6.0.3.2 lib/action_mailbox/router.rb
actionmailbox-6.0.3.1 lib/action_mailbox/router.rb