<commit_before>require 'sinatra/base'
require 'json'

require_relative 'swagger-endpoint'
require_relative 'swagger-endpoint-parameter'
require_relative 'swagger-endpoint-response'
require_relative 'swagger-info'
require_relative 'swagger-invalid-exception'
require_relative 'swagger-type'

require_relative 'swagger-content-creator'

module Sinatra

  # Expose swagger API from your Sinatra app
  module SwaggerExposer

    def self.registered(app)
      app.set :swagger_endpoints, []
      app.set :swagger_current_endpoint_info, {}
      app.set :swagger_current_endpoint_parameters, {}
      app.set :swagger_current_endpoint_responses, {}
      app.set :swagger_types, {}
      declare_swagger_endpoints(app)
    end

    def self.declare_swagger_endpoints(app)
      app.endpoint_summary 'The swagger endpoint'
      app.endpoint_tags 'swagger'
      app.get '/swagger_doc.json' do
        swagger_content = ::Sinatra::SwaggerExposer::SwaggerContentCreator.new(
            settings.respond_to?(:swagger_info) ? settings.swagger_info : nil,
            settings.swagger_types,
            settings.swagger_endpoints
        ).to_swagger
        content_type :json
        swagger_content.to_json
      end

      app.endpoint_summary 'Option method for the swagger endpoint, useful for some CORS stuff'
      app.endpoint_tags 'swagger'
      app.options '/swagger_doc.json' do
        200
      end
    end

    # Provide a summary for the endpoint
    def endpoint_summary(summary)
      set_if_type_and_not_exist(summary, :summary, String)
    end

    # Provide a path
    def endpoint_path(path)
      set_if_type_and_not_exist(path, :path, String)
    end

    # Provide a description for the endpoint
    def endpoint_description(description)
      set_if_type_and_not_exist(description, :description, String)
    end

    # Provide tags for the endpoint
    def endpoint_tags(*tags)
      set_if_type_and_not_exist(tags, :tags, nil)
    end

    # Define parameter for the endpoint
    def endpoint_parameter(name, description, how_to_pass, required, type, params = {})
      parameters = settings.swagger_current_endpoint_parameters
      check_if_not_duplicate(name, parameters, 'Parameter')
      parameters[name] = SwaggerEndpointParameter.new(
          name,
          description,
          how_to_pass,
          required,
          type,
          params,
          settings.swagger_types.keys)
    end

    # Define fluent endpoint dispatcher
    def endpoint(params)
      if params[:summary]
        endpoint_summary params[:summary]
      end
      if params[:description]
        endpoint_description params[:description]
      end
      if params[:response]
        endpoint_response *params[:response]
      end
      if params[:tags]
        endpoint_tags *params[:tags]
      end
      if params[:parameters]
        params[:parameters].each do |param, args|
          endpoint_parameter param, *args
        end
      end

    end


    # General information
    def general_info(params)
      set :swagger_info, SwaggerInfo.new(params)
    end

    # Declare a type
    def type(name, params)
      types = settings.swagger_types
      if types.key? name
        raise SwaggerInvalidException.new("Type [#{name}] already exist with value #{types[name]}")
      end
      types[name] = SwaggerType.new(name, params, settings.swagger_types.keys)
    end

    # Declare a response<commit_msg>refactore endpoint with more concise code<commit_after>require 'sinatra/base'
require 'json'

require_relative 'swagger-endpoint'
require_relative 'swagger-endpoint-parameter'
require_relative 'swagger-endpoint-response'
require_relative 'swagger-info'
require_relative 'swagger-invalid-exception'
require_relative 'swagger-type'

require_relative 'swagger-content-creator'

module Sinatra

  # Expose swagger API from your Sinatra app
  module SwaggerExposer

    def self.registered(app)
      app.set :swagger_endpoints, []
      app.set :swagger_current_endpoint_info, {}
      app.set :swagger_current_endpoint_parameters, {}
      app.set :swagger_current_endpoint_responses, {}
      app.set :swagger_types, {}
      declare_swagger_endpoints(app)
    end

    def self.declare_swagger_endpoints(app)
      app.endpoint_summary 'The swagger endpoint'
      app.endpoint_tags 'swagger'
      app.get '/swagger_doc.json' do
        swagger_content = ::Sinatra::SwaggerExposer::SwaggerContentCreator.new(
            settings.respond_to?(:swagger_info) ? settings.swagger_info : nil,
            settings.swagger_types,
            settings.swagger_endpoints
        ).to_swagger
        content_type :json
        swagger_content.to_json
      end

      app.endpoint_summary 'Option method for the swagger endpoint, useful for some CORS stuff'
      app.endpoint_tags 'swagger'
      app.options '/swagger_doc.json' do
        200
      end
    end

    # Provide a summary for the endpoint
    def endpoint_summary(summary)
      set_if_type_and_not_exist(summary, :summary, String)
    end

    # Provide a path
    def endpoint_path(path)
      set_if_type_and_not_exist(path, :path, String)
    end

    # Provide a description for the endpoint
    def endpoint_description(description)
      set_if_type_and_not_exist(description, :description, String)
    end

    # Provide tags for the endpoint
    def endpoint_tags(*tags)
      set_if_type_and_not_exist(tags, :tags, nil)
    end

    # Define parameter for the endpoint
    def endpoint_parameter(name, description, how_to_pass, required, type, params = {})
      parameters = settings.swagger_current_endpoint_parameters
      check_if_not_duplicate(name, parameters, 'Parameter')
      parameters[name] = SwaggerEndpointParameter.new(
          name,
          description,
          how_to_pass,
          required,
          type,
          params,
          settings.swagger_types.keys)
    end

    # Define fluent endpoint dispatcher
    def endpoint(params)

      endpoint_summary params[:summary] if params[:summary]
      endpoint_description params[:description] if params[:description]
      endpoint_response *params[:response] if params[:response]
      endpoint_tags *params[:tags] if params[:tags]

      params[:parameters].each do |param, args|
        endpoint_parameter param, *args
      end if params[:parameters]
    end

    # General information
    def general_info(params)
      set :swagger_info, SwaggerInfo.new(params)
    end

    # Declare a type
    def type(name, params)
      types = settings.swagger_types
      if types.key? name
        raise SwaggerInvalidException.new("Type [#{name}] already exist with value #{types[name]}")
      end
      types[name] = SwaggerType.new(name, params, settings.swagger_types.keys)
    end

    # Declare a response
{'fraction_non_alphanumeric': 0.07785314066647006, 'fraction_numerical': 0.0008846947803007962, 'mean_word_length': 3.0787733012627783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5003610', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1942, 'n_words': 461}