<commit_before>require "pry"

module JsonTestData
  class JsonSchema
    attr_accessor :schema

    def initialize(schema)
      @schema = JSON.parse(schema, symbolize_names: true)
    end

    def generate_example
      @schema.fetch(:type) == "object" ? generate_object : generate_array
    end

    private
      def object_of_type(type)
        case type
        when "number" || "integer"
          1
        when "boolean"
          true
        when "string"
          "string"
        end
      end

      def generate_object
        obj = {}

        schema.fetch(:properties).each {|k, v| obj[k] = object_of_type(v.fetch(:type)) }

        obj.to_json
      end

      def generate_array
        arr = []

        schema.fetch(:items).each do |item|
          val = item.is_a?(Array) ? object_of_type(item.last) : item
          arr << val
        end

        arr.to_json
      end
  end
end<commit_msg>Try to create nested objects recursively<commit_after>module JsonTestData
  class JsonSchema
    attr_accessor :schema

    def initialize(schema)
      @schema = JSON.parse(schema, symbolize_names: true)
    end

    def generate_example
      @schema.fetch(:type) == "object" ? generate_object(schema) : generate_array(schema)
    end

    private
      def object_of_type(type)
        case type
        when "number" || "integer"
          1
        when "boolean"
          true
        when "string"
          "string"
        end
      end

      def generate_object(object)
        obj = {}

        object.fetch(:properties).each do |k, v|
          obj[k] = v.fetch(:type) == "object" ? generate_object(v) : object_of_type(v.fetch(:type))
        end

        obj.to_json
      end

      def generate_array(object)
        arr = []

        object.fetch(:items).each do |item|
          val = item.is_a?(Array) ? object_of_type(item.last) : item
          arr << val
        end

        arr.to_json
      end
  end
end
{'fraction_non_alphanumeric': 0.09670781893004116, 'fraction_numerical': 0.00102880658436214, 'mean_word_length': 1.8814814814814815, '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': '4917871', 'n_tokens_mistral': 598, 'n_tokens_neox': 581, 'n_words': 139}