class CottonTail::Middleware::Router
Attributes
handlers[R]
Public Class Methods
new(app, handlers:)
click to toggle source
# File lib/cotton_tail/middleware/router.rb, line 11 def initialize(app, handlers:) @app = app @handlers = handlers end
Public Instance Methods
call(message)
click to toggle source
# File lib/cotton_tail/middleware/router.rb, line 16 def call(message) env, req, = message @app.call [env, req, response(*message)] end
Private Instance Methods
add_route_params(req, route)
click to toggle source
# File lib/cotton_tail/middleware/router.rb, line 49 def add_route_params(req, route) delivery_info, properties, payload = req.to_a Request.new( delivery_info, properties.merge( route_params: route.extract_params(req.routing_key) ), payload ) end
lookup_handler(routing_key)
click to toggle source
# File lib/cotton_tail/middleware/router.rb, line 36 def lookup_handler(routing_key) handlers[lookup_route(routing_key)] end
lookup_route(routing_key)
click to toggle source
# File lib/cotton_tail/middleware/router.rb, line 40 def lookup_route(routing_key) route, *conflicts = routes(routing_key) raise UndefinedRouteError if route.nil? raise RouteConflictError unless conflicts.empty? route end
response(env, req, res)
click to toggle source
# File lib/cotton_tail/middleware/router.rb, line 23 def response(env, req, res) routing_key = req.routing_key handler = lookup_handler(routing_key) route = lookup_route(routing_key) req = add_route_params(req, route) if route_params?(route, routing_key) CottonTail::Response.new handler.call([env, req, res]) end
route_params?(route, routing_key)
click to toggle source
# File lib/cotton_tail/middleware/router.rb, line 60 def route_params?(route, routing_key) route.extract_params(routing_key) == {} || true end
routes(routing_key)
click to toggle source
# File lib/cotton_tail/middleware/router.rb, line 32 def routes(routing_key) handlers.keys.select { |route| route.match? routing_key } end