<commit_before>require 'rack'
require 'grape'
require 'hashie'

module Grape
  # An Endpoint is the proxy scope in which all routing
  # blocks are executed. In other words, any methods
  # on the instance level of this class may be called
  # from inside a `get`, `post`, etc. block.
  class Endpoint
    attr_accessor :block, :options, :settings
    attr_reader :env, :request

    def initialize(settings, options = {}, &block)
      @settings = settings
      @block = block
      @options = options

      raise ArgumentError, "Must specify :path option." unless options.key?(:path)
      options[:path] = Array(options[:path])
      options[:path] = ['/'] if options[:path].empty?

      raise ArgumentError, "Must specify :method option." unless options.key?(:method)
      options[:method] = Array(options[:method])

      options[:route_options] ||= {}
    end

    def routes
      @routes ||= prepare_routes
    end

    def mount_in(route_set)
      if options[:app] && options[:app].respond_to?(:endpoints)
        options[:app].endpoints.each{|e| e.mount_in(route_set)}
      else
        routes.each do |route|
          route_set.add_route(self, {
            :path_info => route.route_compiled,
            :request_method => route.route_method,
          }, { :route_info => route })
        end
      end
    end

    def prepare_routes
      routes = []
      options[:method].each do |method|
        options[:path].each do |path|
          prepared_path = prepare_path(path)

          anchor = options[:route_options][:anchor]
          anchor = anchor.nil? ? true : anchor

          requirements = options[:route_options][:requirements] || {}

          path = compile_path(prepared_path, anchor && !options[:app], requirements)
          regex = Rack::Mount::RegexpWithNamedGroups.new(path)
          path_params = {}
          # named parameters in the api path
          named_params = regex.named_captures.map { |nc| nc[0] } - [ 'version', 'format' ]
          named_params.each { |named_param| path_params[named_param] = "" }
          # route parameters declared via desc or appended to the api declaration
          route_params = (options[:route_options][:params] || {})
          path_params.merge!(route_params)
          request_method = (method.to_s.upcase unless method == :any)
          route = Route.new(options[:route_options].clone.merge({
            :prefix => settings[:root_prefix],
            :version => settings[:version] ? settings[:version].join('|') : nil,
            :namespace => namespace,
            :method => request_method,
            :path => prepared_path,
            :params => path_params,
            :compiled => path,
            })
          )
          routes << route
        end
      end
      routes
    end

    def prepare_path(path)
      parts = []
      parts << settings[:root_prefix] if settings[:root_prefix]
      parts << ':version' if settings[:version] && settings[:version_options][:using] == :path
      parts << namespace.to_s if namespace
      parts << path.to_s if path && '/' != path
      Rack::Mount::Utils.normalize_path(parts.join('/') + '(.:format)')
    end
<commit_msg>Undid a change, don't define a local route.<commit_after>require 'rack'
require 'grape'
require 'hashie'

module Grape
  # An Endpoint is the proxy scope in which all routing
  # blocks are executed. In other words, any methods
  # on the instance level of this class may be called
  # from inside a `get`, `post`, etc. block.
  class Endpoint
    attr_accessor :block, :options, :settings
    attr_reader :env, :request

    def initialize(settings, options = {}, &block)
      @settings = settings
      @block = block
      @options = options

      raise ArgumentError, "Must specify :path option." unless options.key?(:path)
      options[:path] = Array(options[:path])
      options[:path] = ['/'] if options[:path].empty?

      raise ArgumentError, "Must specify :method option." unless options.key?(:method)
      options[:method] = Array(options[:method])

      options[:route_options] ||= {}
    end

    def routes
      @routes ||= prepare_routes
    end

    def mount_in(route_set)
      if options[:app] && options[:app].respond_to?(:endpoints)
        options[:app].endpoints.each{|e| e.mount_in(route_set)}
      else
        routes.each do |route|
          route_set.add_route(self, {
            :path_info => route.route_compiled,
            :request_method => route.route_method,
          }, { :route_info => route })
        end
      end
    end

    def prepare_routes
      routes = []
      options[:method].each do |method|
        options[:path].each do |path|
          prepared_path = prepare_path(path)

          anchor = options[:route_options][:anchor]
          anchor = anchor.nil? ? true : anchor

          requirements = options[:route_options][:requirements] || {}

          path = compile_path(prepared_path, anchor && !options[:app], requirements)
          regex = Rack::Mount::RegexpWithNamedGroups.new(path)
          path_params = {}
          # named parameters in the api path
          named_params = regex.named_captures.map { |nc| nc[0] } - [ 'version', 'format' ]
          named_params.each { |named_param| path_params[named_param] = "" }
          # route parameters declared via desc or appended to the api declaration
          route_params = (options[:route_options][:params] || {})
          path_params.merge!(route_params)
          request_method = (method.to_s.upcase unless method == :any)
          routes << Route.new(options[:route_options].clone.merge({
            :prefix => settings[:root_prefix],
            :version => settings[:version] ? settings[:version].join('|') : nil,
            :namespace => namespace,
            :method => request_method,
            :path => prepared_path,
            :params => path_params,
            :compiled => path,
            })
          )
        end
      end
      routes
    end

    def prepare_path(path)
      parts = []
      parts << settings[:root_prefix] if settings[:root_prefix]
      parts << ':version' if settings[:version] && settings[:version_options][:using] == :path
      parts << namespace.to_s if namespace
      parts << path.to_s if path && '/' != path
      Rack::Mount::Utils.normalize_path(parts.join('/') + '(.:format)')
    end

{'fraction_non_alphanumeric': 0.12720403022670027, 'fraction_numerical': 0.00031486146095717883, 'mean_word_length': 2.5333704115684093, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '5861702', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1775, 'n_words': 451}