Class: Babylon::StanzaRouter
- Inherits:
-
Object
- Object
- Babylon::StanzaRouter
- Defined in:
- lib/babylon/router.rb
Overview
Routers are in charge of sending the right stanzas to the right controllers based on user defined Routes. Each application can have only one!
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#connected(connection) ⇒ Object
Connected is called by the XmppConnection to indicate that the XMPP connection has been established.
-
#draw(&block) ⇒ Object
Run the router DSL.
-
#execute_route(controller_name, action_name, stanza = nil) ⇒ Object
Executes the route for the given xml_stanza, by instantiating the controller_name, calling action_name and sending the result to the connection.
-
#initialize ⇒ StanzaRouter
constructor
A new instance of StanzaRouter.
-
#purge_routes! ⇒ Object
Throw away all added routes from this router.
-
#route(xml_stanza) ⇒ Object
Look for the first matching route and calls the corresponding action for the corresponding controller.
Constructor Details
#initialize ⇒ StanzaRouter
Returns a new instance of StanzaRouter.
11 12 13 |
# File 'lib/babylon/router.rb', line 11 def initialize @routes = [] end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/babylon/router.rb', line 9 def connection @connection end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
9 10 11 |
# File 'lib/babylon/router.rb', line 9 def routes @routes end |
Instance Method Details
#connected(connection) ⇒ Object
Connected is called by the XmppConnection to indicate that the XMPP connection has been established
17 18 19 |
# File 'lib/babylon/router.rb', line 17 def connected(connection) @connection = connection end |
#draw(&block) ⇒ Object
Run the router DSL.
60 61 62 63 64 65 66 67 68 |
# File 'lib/babylon/router.rb', line 60 def draw(&block) dsl = Router::DSL.new dsl.instance_eval(&block) dsl.routes.each do |route| raise("Route lacks destination: #{route.inspect}") unless route.is_a?(Route) end @routes += dsl.routes sort end |
#execute_route(controller_name, action_name, stanza = nil) ⇒ Object
Executes the route for the given xml_stanza, by instantiating the controller_name, calling action_name and sending the result to the connection
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/babylon/router.rb', line 33 def execute_route(controller_name, action_name, stanza = nil) begin stanza_class_name = Extlib::Inflection.camelize(action_name) Babylon.logger.debug("looking for stanza #{stanza_class_name} for #{action_name}") stanza = Kernel.const_get(stanza_class_name).new(stanza) if stanza Babylon.logger.info { "ROUTING TO #{controller_name}::#{action_name} with #{stanza.class}" } rescue NameError Babylon.logger.info { "ROUTING TO #{controller_name}::#{action_name} with #{stanza.class}" } end controller = controller_name.new(stanza) controller.perform(action_name) connection.send_xml(controller.evaluate) end |
#purge_routes! ⇒ Object
Throw away all added routes from this router. Helpful for testing.
54 55 56 |
# File 'lib/babylon/router.rb', line 54 def purge_routes! @routes = [] end |
#route(xml_stanza) ⇒ Object
Look for the first matching route and calls the corresponding action for the corresponding controller. Sends the response on the XMPP stream/
24 25 26 27 28 |
# File 'lib/babylon/router.rb', line 24 def route(xml_stanza) route = routes.select{ |r| r.accepts?(xml_stanza) }.first return false unless route execute_route(route.controller, route.action, xml_stanza) end |