Sha256: d6360e355dc856f6668a8b3398ce15c15863246740ef197343377e3103e78059

Contents?: true

Size: 1.85 KB

Versions: 17

Compression:

Stored size: 1.85 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 { _1.type == 'object' }
          .map { _1.to_example(example_options) }
          .reduce(&:merge)
      end

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

      def any_of_options(example_options)
        any_of
          .select { _1.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

17 entries across 17 versions & 1 rubygems

Version Path
fakeit-0.11.0 lib/fakeit/openapi/schema.rb
fakeit-0.10.0 lib/fakeit/openapi/schema.rb
fakeit-0.9.2 lib/fakeit/openapi/schema.rb
fakeit-0.9.1 lib/fakeit/openapi/schema.rb
fakeit-0.9.0 lib/fakeit/openapi/schema.rb
fakeit-0.8.1 lib/fakeit/openapi/schema.rb
fakeit-0.8.0 lib/fakeit/openapi/schema.rb
fakeit-0.7.1 lib/fakeit/openapi/schema.rb
fakeit-0.7.0 lib/fakeit/openapi/schema.rb
fakeit-0.6.3 lib/fakeit/openapi/schema.rb
fakeit-0.6.2 lib/fakeit/openapi/schema.rb
fakeit-0.6.1 lib/fakeit/openapi/schema.rb
fakeit-0.6.0 lib/fakeit/openapi/schema.rb
fakeit-0.5.3 lib/fakeit/openapi/schema.rb
fakeit-0.5.2 lib/fakeit/openapi/schema.rb
fakeit-0.5.1 lib/fakeit/openapi/schema.rb
fakeit-0.5.0 lib/fakeit/openapi/schema.rb