Class: Mailman::Router

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

Overview

The router. Stores routes and uses them to process messages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



21
22
23
24
# File 'lib/mailman/router.rb', line 21

def initialize
  @routes = []
  @params = HashWithIndifferentAccess.new
end

Instance Attribute Details

#bounce_blockProc

Returns the block to run if a message has bounced.

Returns:

  • (Proc)

    the block to run if a message has bounced



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

def bounce_block
  @bounce_block
end

#default_blockProc

Returns the block to run if no routes match.

Returns:

  • (Proc)

    the block to run if no routes match



12
13
14
# File 'lib/mailman/router.rb', line 12

def default_block
  @default_block
end

#messageMail::Message (readonly)

Returns the most recently processed message.

Returns:

  • (Mail::Message)

    the most recently processed message



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

def message
  @message
end

#paramsHash (readonly)

Returns the params of the most recently processed message. Used by route blocks.

Returns:

  • (Hash)

    the params of the most recently processed message. Used by route blocks



16
17
18
# File 'lib/mailman/router.rb', line 16

def params
  @params
end

#routesArray

Returns the list of routes.

Returns:

  • (Array)

    the list of routes



6
7
8
# File 'lib/mailman/router.rb', line 6

def routes
  @routes
end

Instance Method Details

#add_route(route) ⇒ Mailman::Route

Adds a route to the router.

Parameters:

Returns:

  • (Mailman::Route)

    the route object that was added (allows chaining).



30
31
32
# File 'lib/mailman/router.rb', line 30

def add_route(route)
  @routes.push(route)[-1]
end

#route(message) ⇒ Object

Route a message. If the route block accepts arguments, it passes any captured params. Named params are available from the params helper. The message is available from the message helper.

Parameters:

  • the (Mail::Message)

    message to route.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mailman/router.rb', line 38

def route(message)
  @params.clear
  @message = message
  result = nil

  if @bounce_block and message.respond_to?(:bounced?) and message.bounced?
    return instance_exec(&@bounce_block)
  end

  routes.each do |route|
    break if result = route.match!(message)
  end

  if result
    @params.merge!(result[:params])
    if !result[:klass].nil?
      if result[:klass].is_a?(Class) # no instance method specified
        result[:klass].new.send(:receive, @message, @params)
      elsif result[:klass].kind_of?(String) # instance method specified
        klass, method = result[:klass].split('#')
        klass.camelize.constantize.new.send(method.to_sym, @message, @params)
      end
    elsif result[:block].arity > 0
      instance_exec(*result[:args], &result[:block])
    else
      instance_exec(&result[:block])
    end
  elsif @default_block
    instance_exec(&@default_block)
  end
end