Class: ActionMailbox::Router

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

Overview

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.



9
10
11
# File 'actionmailbox/lib/action_mailbox/router.rb', line 9

def initialize
  @routes = []
end

Instance Method Details

#add_route(address, to:) ⇒ Object



19
20
21
# File 'actionmailbox/lib/action_mailbox/router.rb', line 19

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

#add_routes(routes) ⇒ Object



13
14
15
16
17
# File 'actionmailbox/lib/action_mailbox/router.rb', line 13

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

#mailbox_for(inbound_email) ⇒ Object



33
34
35
# File 'actionmailbox/lib/action_mailbox/router.rb', line 33

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

#route(inbound_email) ⇒ Object



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

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

    raise RoutingError
  end
end