From 2ca944bdc1ae4a5a265fb1dc33143ba5be9949b8 Mon Sep 17 00:00:00 2001 From: dblock Date: Thu, 7 Apr 2016 12:07:18 -0400 Subject: [PATCH] Fix: avoid evaluating the same route twice. --- lib/grape/router.rb | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/grape/router.rb b/lib/grape/router.rb index bb6f830d3c..a8c59a28a2 100644 --- a/lib/grape/router.rb +++ b/lib/grape/router.rb @@ -41,7 +41,7 @@ def associate_routes(pattern, options = {}) def call(env) with_optimization do - identity(env) || rotation(env) { |route| route.exec(env) } + rotation(env) end end @@ -53,27 +53,31 @@ def recognize_path(input) private - def identity(env) - transaction(env) do |input, method, routing_args| - route = match?(input, method) - if route - env[Grape::Env::GRAPE_ROUTING_ARGS] = make_routing_args(routing_args, route, input) - route.exec(env) + def rotation(env) + response = nil + exact_route = nil + + response = transaction(env) do |input, method, routing_args| + exact_route = match?(input, method) + if exact_route + env[Grape::Env::GRAPE_ROUTING_ARGS] = make_routing_args(routing_args, exact_route, input) + exact_route.exec(env) end end - end - def rotation(env) transaction(env) do |input, method, routing_args| - response = nil routes_for(method).each do |route| + next if route == exact_route next unless route.match?(input) env[Grape::Env::GRAPE_ROUTING_ARGS] = make_routing_args(routing_args, route, input) - response = yield(route) + response = route.exec(env) break unless cascade?(response) end + response - end + end unless response + + response end def transaction(env)