<commit_before>require 'kramdown'

module Grape
  class API
    class << self
      attr_reader :combined_routes

      def add_swagger_documentation(options={})
        documentation_class = create_documentation_class

        documentation_class.setup({:target_class => self}.merge(options))
        mount(documentation_class)

        @combined_routes = {}

        routes.each do |route|
          route_match = route.route_path.split(route.route_prefix).last.match('\/([\w|-]*?)[\.\/\(]')
          next if route_match.nil?
          resource = route_match.captures.first
          next if resource.empty?
          resource.downcase!

          @combined_routes[resource] ||= []

          unless @@hide_documentation_path and route.route_path.include?(@@mount_path)
            @combined_routes[resource] << route
          end
        end
      end

      private

      def create_documentation_class

        Class.new(Grape::API) do
          class << self
            def name
              @@class_name
            end
          end

          def self.setup(options)
            defaults = {
              :target_class             => nil,
              :mount_path               => '/swagger_doc',
              :base_path                => nil,
              :api_version              => '0.1',
              :markdown                 => false,
              :hide_documentation_path  => false,
              :hide_format              => false,
              :format                   => nil,
              :models                   => [],
              :info                     => {},
              :authorizations           => nil,
              :root_base_path           => true,
              :include_base_url         => true
            }

            options = defaults.merge(options)

            target_class     = options[:target_class]
            @@mount_path     = options[:mount_path]
            @@class_name     = options[:class_name] || options[:mount_path].gsub('/', '')
            @@markdown       = options[:markdown]
            @@hide_format    = options[:hide_format]
            api_version      = options[:api_version]
            base_path        = options[:base_path]
            authorizations   = options[:authorizations]
            include_base_url = options[:include_base_url]
            root_base_path   = options[:root_base_path]
            extra_info       = options[:info]
            @@models         = options[:models] || []

            @@hide_documentation_path = options[:hide_documentation_path]

            if options[:format]
              [:format, :default_format, :default_error_formatter].each do |method|
                send(method, options[:format])
              end
            end

            desc 'Swagger compatible API description'
            get @@mount_path do
              header['Access-Control-Allow-Origin']   = '*'
              header['Access-Control-Request-Method'] = '*'

              routes = target_class::combined_routes

              if @@hide_documentation_path
                routes.reject!{ |route, value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
              end

              routes_array = routes.keys.map do |local_route|
                next if routes[local_route].all?(&:route_hidden)

                url_base    = parse_path(route.route_path.gsub('(.:format)', ''), route.route_version) if include_base_url
                url_format  = '.{format}' unless @@hide_format
                {
                  :path => "#{url_base}/#{local_route}#{url_format}",
                  #:description => "..."
                }
              end.compact

              output = {
                apiVersion:     api_version,
                swaggerVersion: "1.2",
                produces:       content_types_for(target_class),
                operations:     [],
                apis:           routes_array,
                info:           parse_info(extra_info)
              }

              basePath                = parse_base_path(base_path, request)
              output[:basePath]       = basePath        if basePath && basePath.size > 0 && root_base_path != false
              output[:authorizations] = authorizations  if authorizations

              output
            end

            desc 'Swagger compatible API description for specific API', :params => {
              "name" => {<commit_msg>Remove unneeded (and disallowed according to spec) elements from the root documentation<commit_after>require 'kramdown'

module Grape
  class API
    class << self
      attr_reader :combined_routes

      def add_swagger_documentation(options={})
        documentation_class = create_documentation_class

        documentation_class.setup({:target_class => self}.merge(options))
        mount(documentation_class)

        @combined_routes = {}

        routes.each do |route|
          route_match = route.route_path.split(route.route_prefix).last.match('\/([\w|-]*?)[\.\/\(]')
          next if route_match.nil?
          resource = route_match.captures.first
          next if resource.empty?
          resource.downcase!

          @combined_routes[resource] ||= []

          unless @@hide_documentation_path and route.route_path.include?(@@mount_path)
            @combined_routes[resource] << route
          end
        end
      end

      private

      def create_documentation_class

        Class.new(Grape::API) do
          class << self
            def name
              @@class_name
            end
          end

          def self.setup(options)
            defaults = {
              :target_class             => nil,
              :mount_path               => '/swagger_doc',
              :base_path                => nil,
              :api_version              => '0.1',
              :markdown                 => false,
              :hide_documentation_path  => false,
              :hide_format              => false,
              :format                   => nil,
              :models                   => [],
              :info                     => {},
              :authorizations           => nil,
              :root_base_path           => true,
              :include_base_url         => true
            }

            options = defaults.merge(options)

            target_class     = options[:target_class]
            @@mount_path     = options[:mount_path]
            @@class_name     = options[:class_name] || options[:mount_path].gsub('/', '')
            @@markdown       = options[:markdown]
            @@hide_format    = options[:hide_format]
            api_version      = options[:api_version]
            base_path        = options[:base_path]
            authorizations   = options[:authorizations]
            include_base_url = options[:include_base_url]
            root_base_path   = options[:root_base_path]
            extra_info       = options[:info]
            @@models         = options[:models] || []

            @@hide_documentation_path = options[:hide_documentation_path]

            if options[:format]
              [:format, :default_format, :default_error_formatter].each do |method|
                send(method, options[:format])
              end
            end

            desc 'Swagger compatible API description'
            get @@mount_path do
              header['Access-Control-Allow-Origin']   = '*'
              header['Access-Control-Request-Method'] = '*'

              routes = target_class::combined_routes

              if @@hide_documentation_path
                routes.reject!{ |route, value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
              end

              routes_array = routes.keys.map do |local_route|
                next if routes[local_route].all?(&:route_hidden)

                url_base    = parse_path(route.route_path.gsub('(.:format)', ''), route.route_version) if include_base_url
                url_format  = '.{format}' unless @@hide_format
                {
                  :path => "#{url_base}/#{local_route}#{url_format}",
                  #:description => "..."
                }
              end.compact

              output = {
                apiVersion:     api_version,
                swaggerVersion: "1.2",
                produces:       content_types_for(target_class),
                # operations:     [],
                apis:           routes_array,
                info:           parse_info(extra_info)
              }

              basePath                = parse_base_path(base_path, request)
              # output[:basePath]       = basePath        if basePath && basePath.size > 0 && root_base_path != false
              output[:authorizations] = authorizations  if authorizations

              output
            end

            desc 'Swagger compatible API description for specific API', :params => {
              "name" => {
{'fraction_non_alphanumeric': 0.10306282137268052, 'fraction_numerical': 0.0013413816230717639, 'mean_word_length': 1.4539221064179924, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 41, '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': '4292162', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2195, 'n_words': 413}