Sha256: 7aec7dafd25beca775737890f0dc4ef27840c4f242c663bdf94d4194b9630d9c

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

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).to_json : generate_array(schema).to_json
    end

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

      def generate_object(object)
        obj = {}

        object.fetch(:properties).each do |k, v|
          obj[k]  = nil unless v.has_key?(:type)

          obj[k]  = if v.fetch(:type) == "object"
                      generate_object(v)
                    elsif v.fetch(:type) == "array"
                      generate_array(v)
                    else
                      object_of_type(v.fetch(:type))
                    end
        end

        obj
      end

      def generate_array(object)
        return [] unless object.fetch(:items).has_key?(:type)

        val = if object.fetch(:items).fetch(:type) == "object"
                generate_object(object.fetch(:items))
              elsif object.fetch(:items).fetch(:type) == "array"
                generate_array(object.fetch(:items))
              else
                object_of_type(object.fetch(:items).fetch(:type))
              end

        [val].compact
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json_test_data-0.0.1 lib/json_test_data/json_schema.rb
json_test_data-0.0.1.beta lib/json_test_data/json_schema.rb