<commit_before>module Hobby
  class Router
    def initialize
      @routes = Routes.new
    end

    def add_route verb, path = nil, &route
      @routes["#{verb}#{path}"] = route
    end

    def route_for env
      route, params = @routes["#{env['REQUEST_METHOD']}#{env['PATH_INFO']}"]
      env[:path_params] = params if params
      route
    end

    class Routes < Hash
      def initialize
        @patterns = {}
        super { |hash, key| hash[key] = find key }
      end

      def []= key, route
        if key.include? ?:
          string = key.gsub(/(:\w+)/) { "(?<#{$1[1..-1]}>[^/?#]+)" }
          @patterns[/^#{string}$/] = route
        else
          super and super "#{key}/", route
        end
      end

      private

      def find key
        _, route = @patterns.find { |pattern, _| pattern.match key }
        [route, $~.names.map(&:to_sym).zip($~.captures).to_h] if route
      end
    end
  end
end
<commit_msg>Get values from env with Hash#values_at<commit_after>module Hobby
  class Router
    def initialize
      @routes = Routes.new
    end

    def add_route verb, path = nil, &route
      @routes["#{verb}#{path}"] = route
    end

    def route_for env
      route, params = @routes[env.values_at('REQUEST_METHOD', 'PATH_INFO').join]
      env[:path_params] = params if params
      route
    end

    class Routes < Hash
      def initialize
        @patterns = {}
        super { |hash, key| hash[key] = find key }
      end

      def []= key, route
        if key.include? ?:
          string = key.gsub(/(:\w+)/) { "(?<#{$1[1..-1]}>[^/?#]+)" }
          @patterns[/^#{string}$/] = route
        else
          super and super "#{key}/", route
        end
      end

      private

      def find key
        _, route = @patterns.find { |pattern, _| pattern.match key }
        [route, $~.names.map(&:to_sym).zip($~.captures).to_h] if route
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.15392670157068064, 'fraction_numerical': 0.0031413612565445027, 'mean_word_length': 1.9445300462249615, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1957868', 'n_tokens_mistral': 648, 'n_tokens_neox': 635, 'n_words': 162}