Class Skates::StanzaRouter
In: lib/skates/router.rb
Parent: Object

Routers are in charge of sending the right stanzas to the right controllers based on user defined Routes. Each application can have only one!

Methods

Attributes

connection  [R] 
routes  [R] 

Public Class methods

[Source]

    # File lib/skates/router.rb, line 11
11:     def initialize
12:       @routes = []
13:     end

Public Instance methods

Connected is called by the XmppConnection to indicate that the XMPP connection has been established

[Source]

    # File lib/skates/router.rb, line 17
17:     def connected(connection)
18:       @connection = connection
19:     end

Run the router DSL.

[Source]

    # File lib/skates/router.rb, line 58
58:     def draw(&block) 
59:       dsl = Router::DSL.new 
60:       dsl.instance_eval(&block) 
61:       dsl.routes.each do |route| 
62:         raise("Route lacks destination: #{route.inspect}") unless route.is_a?(Route) 
63:       end 
64:       @routes += dsl.routes 
65:       sort
66:     end

Executes the route for the given xml_stanza, by instantiating the controller_name, calling action_name and sending the result to the connection

[Source]

    # File lib/skates/router.rb, line 33
33:     def execute_route(controller_name, action_name, stanza = nil)
34:       begin
35:         stanza = Kernel.const_get(action_name.capitalize).new(stanza) if stanza
36:         Skates.logger.info {
37:           "ROUTING TO #{controller_name}::#{action_name} with #{stanza.class}"
38:         }
39:       rescue NameError
40:         Skates.logger.info {
41:           "ROUTING TO #{controller_name}::#{action_name} with #{stanza.class}"
42:         }
43:       end
44:       controller = controller_name.new(stanza) 
45:       controller.perform(action_name) 
46:       connection.send_xml(controller.evaluate)
47:     end

Throw away all added routes from this router. Helpful for testing.

[Source]

    # File lib/skates/router.rb, line 52
52:     def purge_routes! 
53:       @routes = [] 
54:     end

Look for the first matching route and calls the corresponding action for the corresponding controller. Sends the response on the XMPP stream/

[Source]

    # File lib/skates/router.rb, line 24
24:     def route(xml_stanza) 
25:       route = routes.select{ |r| r.accepts?(xml_stanza) }.first 
26:       return false unless route 
27:       execute_route(route.controller, route.action, xml_stanza)
28:     end

[Validate]