<commit_before>module Setup
  class ApiSpec
    include SharedEditable
    include RailsAdmin::Models::Setup::ApiSpecAdmin

    build_in_data_type.referenced_by(:name)

    shared_allow :swagger

    field :title, type: String
    field :url, type: String
    field :specification, type: String

    before_save :validate_specification

    def validate_specification
      json =
        begin
          JSON.parse(specification)
          self.specification = json.to_yaml
        rescue Exception
          begin
            Psych.load(specification)
          rescue Exception
            nil
          end
        end
      if json.is_a?(Hash)
        if (swagger_version = json['swagger'])
          if swagger_version == '2.0'
            if title.blank? && (info = json['info']) && (title = info['title'].to_s.strip).present?
              if (version = info['version'].to_s.strip).present?
                title = "#{title} API #{version}"
              end
              self.title = title
            end
          else
            errors.add(:specification, "swagger version #{swagger_version} is not supported")
          end
        else
          errors.add(:specification, 'does not contains swagger version entry')
        end
      else
        errors.add(:specification, 'is not YAML nor JSON format')
      end
      errors.blank?
    end

    def cenit_collection_hash(options = {})
      self.class.swagger_to_cenit(specification, options)
    end

    def pull_asynchronous
      true
    end

    class << self
      def swagger_to_cenit(spec, options = {})
        spec = Psych.load(spec)<commit_msg>Fixing API Spec version validation<commit_after>module Setup
  class ApiSpec
    include SharedEditable
    include RailsAdmin::Models::Setup::ApiSpecAdmin

    build_in_data_type.referenced_by(:name)

    shared_allow :swagger

    field :title, type: String
    field :url, type: String
    field :specification, type: String

    before_save :validate_specification

    def validate_specification
      json =
        begin
          JSON.parse(specification)
          self.specification = json.to_yaml
        rescue Exception
          begin
            Psych.load(specification)
          rescue Exception
            nil
          end
        end
      if json.is_a?(Hash)
        if (swagger_version = json['swagger'])
          if swagger_version.to_s.to_f == '2.0'
            if title.blank? && (info = json['info']) && (title = info['title'].to_s.strip).present?
              if (version = info['version'].to_s.strip).present?
                title = "#{title} API #{version}"
              end
              self.title = title
            end
          else
            errors.add(:specification, "swagger version #{swagger_version} is not supported")
          end
        else
          errors.add(:specification, 'does not contains swagger version entry')
        end
      else
        errors.add(:specification, 'is not YAML nor JSON format')
      end
      errors.blank?
    end

    def cenit_collection_hash(options = {})
      self.class.swagger_to_cenit(specification, options)
    end

    def pull_asynchronous
      true
    end

    class << self
      def swagger_to_cenit(spec, options = {})
        spec = Psych.load(spec)
{'fraction_non_alphanumeric': 0.08838768668088998, 'fraction_numerical': 0.0012191405059433099, 'mean_word_length': 2.024884792626728, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3488495', 'n_tokens_mistral': 918, 'n_tokens_neox': 904, 'n_words': 225}