<commit_before>require_relative 'swagger-invalid-exception'
require_relative 'swagger-type-property'

module Sinatra

  module SwaggerExposer

    # A type
    class SwaggerType

      def initialize(type_name, type_content)
        @properties = process_properties(type_name, type_content)
        @required = process_required(type_name, type_content, @properties.keys)
        @example = process_example(type_name, type_content, @properties.keys)
      end

      def process_properties(type_name, type_content)
        if !type_content.key?(:properties)
          {}
        elsif !type_content[:properties].is_a? Hash
          raise SwaggerInvalidException.new("Swagger: attribute [properties] of #{type_name} is not an hash: #{type_content[:properties]}")
        else
          result = {}
          type_content[:properties].each_pair do |property_name, property_properties|
            result[property_name.to_s] = SwaggerTypeProperty.new(type_name, property_name, property_properties)
          end
          result
        end
      end

      def process_required(type_name, type_content, properties_names)
        if !type_content.key?(:required)
          []
        elsif !type_content[:required].is_a? Array
          raise SwaggerInvalidException.new("Swagger: attribute [required] of #{type_name} is not an array: #{type_content[:required]}")
        else
          type_content[:required].each do |property_name|
            property_name = property_name.to_s
            unless properties_names.include? property_name
              raise SwaggerInvalidException.new("Swagger: required property [#{property_name}] of [#{type_name}] is unknown, known properties: #{properties_names.join(', ')}")
            end
          end
          type_content[:required]
        end
      end

      def process_example(type_name, type_content, properties_names)
        if !type_content.key?(:example)
          []
        elsif !type_content[:example].is_a? Hash
          raise SwaggerInvalidException.new("Swagger: attribute [example] of #{type_name} is not an hash: #{type_content[:example]}")
        else
          type_content[:example].each_pair do |property_name, property_value|
            property_name = property_name.to_s
            unless properties_names.include? property_name
              raise SwaggerInvalidException.new("Swagger: example property [#{property_name}] with value [#{property_value}] of [#{type_name}] is unknown, known properties: #{properties_names.join(', ')}")
            end
          end
          type_content[:example]
        end
      end

      def to_swagger
        result = {}

        unless @properties.empty?
          swagger_properties = {}
          @properties.collect do |key, value|
            swagger_properties[key] = value.to_swagger
          end
          result[:properties] = swagger_properties
        end

        unless @required.empty?
          result[:required] = @required
        end

        unless @example.empty?
          result[:example] = @example
        end

        result
      end

    end<commit_msg>code reuse<commit_after>require_relative 'swagger-invalid-exception'
require_relative 'swagger-type-property'

module Sinatra

  module SwaggerExposer

    # A type
    class SwaggerType

      def initialize(type_name, type_content)
        @properties = process_properties(type_name, type_content)
        @required = process_required(type_name, type_content, @properties.keys)
        @example = process_example(type_name, type_content, @properties.keys)
      end

      def process_properties(type_name, type_content)
        possible_value = check_attribute_empty_or_bad(type_name, type_content, :properties, Hash)
        if possible_value
          possible_value
        else
          result = {}
          type_content[:properties].each_pair do |property_name, property_properties|
            result[property_name.to_s] = SwaggerTypeProperty.new(type_name, property_name, property_properties)
          end
          result
        end
      end

      def process_required(type_name, type_content, properties_names)
        possible_value = check_attribute_empty_or_bad(type_name, type_content, :required, Array)
        if possible_value
          possible_value
        else
          type_content[:required].each do |property_name|
            property_name = property_name.to_s
            unless properties_names.include? property_name
              raise SwaggerInvalidException.new("Swagger: required property [#{property_name}] of [#{type_name}] is unknown, known properties: #{properties_names.join(', ')}")
            end
          end
          type_content[:required]
        end
      end

      def process_example(type_name, type_content, properties_names)
        possible_value = check_attribute_empty_or_bad(type_name, type_content, :example, Hash)
        if possible_value
          possible_value
        else
          type_content[:example].each_pair do |property_name, property_value|
            property_name = property_name.to_s
            unless properties_names.include? property_name
              raise SwaggerInvalidException.new("Swagger: example property [#{property_name}] with value [#{property_value}] of [#{type_name}] is unknown, known properties: #{properties_names.join(', ')}")
            end
          end
          type_content[:example]
        end
      end

      def check_attribute_empty_or_bad(type_name, type_content, attribute_name, attribute_class)
        if !type_content.key?(attribute_name)
          attribute_class.new
        elsif !type_content[attribute_name].is_a? attribute_class
          raise SwaggerInvalidException.new("Swagger: attribute [#{attribute_name}] of #{type_name} is not an hash: #{type_content[attribute_name]}")
        end
      end

      def to_swagger
        result = {}

        unless @properties.empty?
          swagger_properties = {}
          @properties.collect do |key, value|
            swagger_properties[key] = value.to_swagger
          end
          result[:properties] = swagger_properties
        end

        unless @required.empty?
          result[:required] = @required
        end

        unless @example.empty?
          result[:example] = @example
        end

        result
      end

    end
{'fraction_non_alphanumeric': 0.08436213991769548, 'fraction_numerical': 0.0, 'mean_word_length': 2.6483833718244805, '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': '6191364', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1661, 'n_words': 370}