Sha256: 946526930ed1d80e2a156243c10bb97f7c97a769d4477c364f63cd53f3375e26

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

module JsonTestData
  class String
    class << self
      def create(schema)
        return schema.fetch(:enum).sample if schema.fetch(:enum, nil)

        pattern(schema).random_example
      end

      private

      def pattern(schema)
        if schema.fetch(:format, nil)
          Regexp.new(formats.fetch(schema.fetch(:format)))
        elsif schema.fetch(:pattern, nil)
          Regexp.new(schema.fetch(:pattern))
        else
          len = schema.fetch(:maxLength, nil) || schema.fetch(:minLength, nil) || 1
          /.{#{len}}/
        end
      end

      def formats
        {
          "date-time" => /^20\d{2}\-((0[1-9])|(1[0-2]))\-((0[1-9])|([1-2]\d))T(([0-1]\d)|(2[0-3]))\:[0-5]\d\:[0-5]\d(Z|[\+\-]\d{2}\:(0|3)0)$/,
          "email"     => /^\S+@\S+\.\S{1,5}$/,
          "hostname"  => /^[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9]$/,
          "uri"       => /^https?\:\/\/\S{1,10}\.\S{1,10}\.\S{2,5}$/
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_test_data-1.1.0 lib/json_test_data/data_structures/string.rb