Class: ActionMailbox::Router

Inherits:
Object
  • Object
show all
Defined in:
actionmailbox/lib/action_mailbox/router.rb

Overview

Action Mailbox Router

Encapsulates the routes that live on the ApplicationMailbox and performs the actual routing when an inbound_email is received.

Defined Under Namespace

Classes: Route, RoutingError

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



11
12
13
# File 'actionmailbox/lib/action_mailbox/router.rb', line 11

def initialize
  @routes = []
end

Instance Method Details

#add_route(address, to:) ⇒ Object



21
22
23
# File 'actionmailbox/lib/action_mailbox/router.rb', line 21

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

#add_routes(routes) ⇒ Object



15
16
17
18
19
# File 'actionmailbox/lib/action_mailbox/router.rb', line 15

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

#mailbox_for(inbound_email) ⇒ Object



35
36
37
# File 'actionmailbox/lib/action_mailbox/router.rb', line 35

def mailbox_for(inbound_email)
  routes.detect { |route| route.match?(inbound_email) }&.mailbox_class
end

#route(inbound_email) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'actionmailbox/lib/action_mailbox/router.rb', line 25

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

    raise RoutingError
  end
end