1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/action_mailbox/router.rb

37 lines
719 B
Ruby
Raw Normal View History

class ActionMailbox::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)
2018-09-17 20:49:47 -04:00
end
def route(inbound_email)
2018-09-20 20:27:41 -04:00
if mailbox = match_to_mailbox(inbound_email)
mailbox.receive(inbound_email)
else
inbound_email.bounced!
raise RoutingError
end
2018-09-17 20:49:47 -04:00
end
private
attr_reader :routes
2018-09-20 20:27:41 -04:00
def match_to_mailbox(inbound_email)
routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class)
2018-09-17 20:49:47 -04:00
end
end
require "action_mailbox/router/route"