Sha256: d3b885b4e89f2e0bece4d8d208f3e3ebf2af1788d410362b87da835ec1520e5c

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

require 'fakeit/openapi/example/array_example'
require 'fakeit/openapi/example/boolean_example'
require 'fakeit/openapi/example/integer_example'
require 'fakeit/openapi/example/number_example'
require 'fakeit/openapi/example/object_example'
require 'fakeit/openapi/example/string_example'

module Fakeit
  module Openapi
    module Schema
      include Fakeit::Openapi::Example

      def to_example(**example_options)
        return example if example_options[:use_example] && example

        return one_of_example(example_options) if one_of
        return all_of_example(example_options) if all_of
        return any_of_example(example_options) if any_of

        type_based_example(example_options)
      end

      private

      def one_of_example(example_options)
        if example_options[:use_static][property: example_options[:property]]
          one_of.first.to_example(example_options)
        else
          one_of.sample.to_example(example_options)
        end
      end

      def all_of_example(example_options)
        all_of
          .select { |option| option.type == 'object' }
          .map { |option| option.to_example(example_options) }
          .reduce(&:merge)
      end

      def any_of_example(example_options)
        any_of_options(example_options)
          .map { |option| option.to_example(example_options) }
          .reduce(&:merge)
      end

      def any_of_options(example_options)
        any_of
          .select { |option| option.type == 'object' }
          .then do |options|
            if example_options[:use_static][property: example_options[:property]]
              options
            else
              options.sample(Faker::Number.between(from: 1, to: any_of.size))
            end
          end
      end

      def type_based_example(example_options)
        send("#{type}_example", example_options) if %w[string integer number boolean array object].include?(type)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fakeit-0.4.2 lib/fakeit/openapi/schema.rb
fakeit-0.4.1 lib/fakeit/openapi/schema.rb
fakeit-0.4.0 lib/fakeit/openapi/schema.rb
fakeit-0.3.1 lib/fakeit/openapi/schema.rb