123456789_123456789_123456789_123456789_123456789_

Class: ActionMailbox::Router

Relationships & Source Files
Namespace Children
Classes:
Exceptions:
Inherits: Object
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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newRouter

[ GitHub ]

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

def initialize
  @routes = []
end

Instance Attribute Details

#routes (readonly, private)

[ GitHub ]

  
# File 'actionmailbox/lib/action_mailbox/router.rb', line 40

attr_reader :routes

Instance Method Details

#add_route(address, to:)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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