lib/grape/router.rb in grape-1.3.0 vs lib/grape/router.rb in grape-1.3.1

- old
+ new

@@ -1,8 +1,9 @@ # frozen_string_literal: true require 'grape/router/route' +require 'grape/util/cache' module Grape class Router attr_reader :map, :compiled @@ -14,16 +15,24 @@ @index = index super(attributes) end end + class NormalizePathCache < Grape::Util::Cache + def initialize + @cache = Hash.new do |h, path| + normalized_path = +"/#{path}" + normalized_path.squeeze!('/') + normalized_path.sub!(%r{/+\Z}, '') + normalized_path = '/' if normalized_path.empty? + h[path] = -normalized_path + end + end + end + def self.normalize_path(path) - path = +"/#{path}" - path.squeeze!('/') - path.sub!(%r{/+\Z}, '') - path = '/' if path == '' - path + NormalizePathCache[path] end def self.supported_methods @supported_methods ||= Grape::Http::Headers::SUPPORTED_METHODS + ['*'] end @@ -104,11 +113,11 @@ # return response by using #call_with_allow_headers. return call_with_allow_headers( env, neighbor.allow_header, neighbor.endpoint - ) if neighbor && method == 'OPTIONS' && !cascade + ) if neighbor && method == Grape::Http::Headers::OPTIONS && !cascade route = match?(input, '*') return neighbor.endpoint.call(env) if neighbor && cascade && route if route @@ -158,10 +167,10 @@ last_match = Regexp.last_match @neutral_map.detect { |route| last_match["_#{route.index}"] } end def call_with_allow_headers(env, methods, endpoint) - env[Grape::Env::GRAPE_ALLOWED_METHODS] = methods + env[Grape::Env::GRAPE_ALLOWED_METHODS] = methods.join(', ').freeze endpoint.call(env) end def cascade?(response) response && response[1][Grape::Http::Headers::X_CASCADE] == 'pass'