module Fakeit::Openapi::Example

Constants

DEFAULT_BITS
MAX_NUM
MIN_NUM
RANDOM_FORMAT_HANDLERS
STATIC_FORMAT_HANDLERS

Public Instance Methods

array_example(options) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 4
def array_example(options)
  example_options = add_depth(options)
  if example_options[:use_static][type: 'array', property: example_options[:property]]
    generate_array_example(example_options, -> { non_empty_size })
  else
    generate_array_example(example_options, -> { random_array_size(example_options) })
  end
end

Private Instance Methods

add_depth(example_options) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 36
  def add_depth(example_options) = { **example_options, depth: example_options[:depth] + 1 }

  def need_retry?(item, result, retries) = uniqueItems && result.include?(item) && retries.positive?

  def non_empty_size = [min_array, 1].max

  def min_array = minItems || 1

  def max_array(depth) = maxItems || min_array + (depth > 1 ? 2 : 9)
end
boolean_example(example_options) click to toggle source
# File lib/fakeit/openapi/example/boolean_example.rb, line 4
def boolean_example(example_options)
  example_options[:use_static][type: 'boolean', property: example_options[:property]] || Faker::Boolean.boolean
end
fixed_faker(&block) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 52
def fixed_faker(&block)
  Faker::Config.random = Random.new(1)
  result = block.call
  Faker::Config.random = nil
  result
end
generate_array_example(example_options, get_size) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 15
def generate_array_example(example_options, get_size)
  size = retries = get_size[]
  [].tap { generate_items(size, retries, example_options, _1) }
end
generate_items(size, retries, example_options, result) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 24
def generate_items(size, retries, example_options, result)
  while result.size < size
    item = items.to_example(example_options)

    if need_retry?(item, result, retries)
      retries -= 1
    else
      result << item
    end
  end
end
int_bits() click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 56
def int_bits
  return DEFAULT_BITS unless format =~ /int\d+/

  format[/\d+/].to_i
end
int_multiple(= multipleOf || 1) click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 38
  def int_multiple = multipleOf || 1

  def min_int
    if minimum
      exclusiveMinimum ? minimum + 1 : minimum
    else
      -2**(int_bits - 1)
    end
  end

  def max_int
    if maximum
      exclusiveMaximum ? maximum - 1 : maximum
    else
      2**(int_bits - 1) - 1
    end
  end

  def int_bits
    return DEFAULT_BITS unless format =~ /int\d+/

    format[/\d+/].to_i
  end
end
int_rand_begin(= min_int / int_multiple + int_rand_begin_adjust) click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 32
    def int_rand_begin = min_int / int_multiple + int_rand_begin_adjust

    def int_rand_end = max_int / int_multiple

    def int_rand_begin_adjust = (min_int % int_multiple).zero? ? 0 : 1

    def int_multiple = multipleOf || 1

    def min_int
      if minimum
        exclusiveMinimum ? minimum + 1 : minimum
      else
        -2**(int_bits - 1)
      end
    end

    def max_int
      if maximum
        exclusiveMaximum ? maximum - 1 : maximum
      else
        2**(int_bits - 1) - 1
      end
    end

    def int_bits
      return DEFAULT_BITS unless format =~ /int\d+/

      format[/\d+/].to_i
    end
  end
end
int_rand_begin_adjust(= (min_int % int_multiple).zero? ? 0 : 1) click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 36
    def int_rand_begin_adjust = (min_int % int_multiple).zero? ? 0 : 1

    def int_multiple = multipleOf || 1

    def min_int
      if minimum
        exclusiveMinimum ? minimum + 1 : minimum
      else
        -2**(int_bits - 1)
      end
    end

    def max_int
      if maximum
        exclusiveMaximum ? maximum - 1 : maximum
      else
        2**(int_bits - 1) - 1
      end
    end

    def int_bits
      return DEFAULT_BITS unless format =~ /int\d+/

      format[/\d+/].to_i
    end
  end
end
int_rand_end(= max_int / int_multiple) click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 34
      def int_rand_end = max_int / int_multiple

      def int_rand_begin_adjust = (min_int % int_multiple).zero? ? 0 : 1

      def int_multiple = multipleOf || 1

      def min_int
        if minimum
          exclusiveMinimum ? minimum + 1 : minimum
        else
          -2**(int_bits - 1)
        end
      end

      def max_int
        if maximum
          exclusiveMaximum ? maximum - 1 : maximum
        else
          2**(int_bits - 1) - 1
        end
      end

      def int_bits
        return DEFAULT_BITS unless format =~ /int\d+/

        format[/\d+/].to_i
      end
    end
  end
end
integer_example(example_options) click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 6
def integer_example(example_options)
  if example_options[:use_static][type: 'integer', property: example_options[:property]]
    static_integer_example
  else
    random_integer_example
  end
end
length_constraint(= minLength || maxLength) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 78
def length_constraint = minLength || maxLength

def string_with_length = Faker::Internet.username(specifier: min_string_length..max_string_length)

def min_string_length = minLength || 0

def max_string_length = maxLength || min_string_length + 10

def random_string_format = (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]

def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)

def unknown_format
  Logger.info("Unknown string format: #{format}")
  'Unknown string format'
end
    
max_array(depth) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 44
  def max_array(depth) = maxItems || min_array + (depth > 1 ? 2 : 9)
end
max_int() click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 48
def max_int
  if maximum
    exclusiveMaximum ? maximum - 1 : maximum
  else
    2**(int_bits - 1) - 1
  end
end
max_num(= (maximum || MAX_NUM).to_f.floor(2)) click to toggle source
# File lib/fakeit/openapi/example/number_example.rb, line 32
  def max_num = (maximum || MAX_NUM).to_f.floor(2)
end
max_string_length(= maxLength || min_string_length + 10) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 84
      def max_string_length = maxLength || min_string_length + 10

      def random_string_format = (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]

      def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)

      def unknown_format
        Logger.info("Unknown string format: #{format}")
        'Unknown string format'
      end
    end
  end
end
min_array(= minItems || 1) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 42
    def min_array = minItems || 1

    def max_array(depth) = maxItems || min_array + (depth > 1 ? 2 : 9)
  end
end
min_int() click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 40
def min_int
  if minimum
    exclusiveMinimum ? minimum + 1 : minimum
  else
    -2**(int_bits - 1)
  end
end
min_num(= (minimum || MIN_NUM).to_f.ceil(2)) click to toggle source
# File lib/fakeit/openapi/example/number_example.rb, line 30
    def min_num = (minimum || MIN_NUM).to_f.ceil(2)

    def max_num = (maximum || MAX_NUM).to_f.floor(2)
  end
end
min_string_length(= minLength || 0) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 82
    def min_string_length = minLength || 0

    def max_string_length = maxLength || min_string_length + 10

    def random_string_format = (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]

    def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)

    def unknown_format
      Logger.info("Unknown string format: #{format}")
      'Unknown string format'
    end
  end
end
need_retry?(item, result, retries) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 38
    def need_retry?(item, result, retries) = uniqueItems && result.include?(item) && retries.positive?

    def non_empty_size = [min_array, 1].max

    def min_array = minItems || 1

    def max_array(depth) = maxItems || min_array + (depth > 1 ? 2 : 9)
  end
end
non_empty_size(= [min_array, 1].max) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 40
      def non_empty_size = [min_array, 1].max

      def min_array = minItems || 1

      def max_array(depth) = maxItems || min_array + (depth > 1 ? 2 : 9)
    end
  end
end
num_multiple(= multipleOf || 1) click to toggle source
# File lib/fakeit/openapi/example/number_example.rb, line 28
      def num_multiple = multipleOf || 1

      def min_num = (minimum || MIN_NUM).to_f.ceil(2)

      def max_num = (maximum || MAX_NUM).to_f.floor(2)
    end
  end
end
num_rand_begin(= multipleOf ? (min_num / multipleOf).ceil : min_num) click to toggle source
# File lib/fakeit/openapi/example/number_example.rb, line 24
  def num_rand_begin = multipleOf ? (min_num / multipleOf).ceil : min_num

  def num_rand_end = multipleOf ? (max_num / multipleOf).floor : max_num

  def num_multiple = multipleOf || 1

  def min_num = (minimum || MIN_NUM).to_f.ceil(2)

  def max_num = (maximum || MAX_NUM).to_f.floor(2)
end
num_rand_end(= multipleOf ? (max_num / multipleOf).floor : max_num) click to toggle source
# File lib/fakeit/openapi/example/number_example.rb, line 26
    def num_rand_end = multipleOf ? (max_num / multipleOf).floor : max_num

    def num_multiple = multipleOf || 1

    def min_num = (minimum || MIN_NUM).to_f.ceil(2)

    def max_num = (maximum || MAX_NUM).to_f.floor(2)
  end
end
number_example(example_options) click to toggle source
# File lib/fakeit/openapi/example/number_example.rb, line 7
def number_example(example_options)
  if example_options[:use_static][type: 'number', property: example_options[:property]]
    static_number_example
  else
    random_number_example
  end
end
object_example(example_options) click to toggle source
# File lib/fakeit/openapi/example/object_example.rb, line 4
def object_example(example_options)
  properties.each_with_object({}) do |(name, schema), obj|
    obj[name] = schema.to_example(**example_options, property: name)
  end
end
random_array_size(example_options) click to toggle source
# File lib/fakeit/openapi/example/array_example.rb, line 20
def random_array_size(example_options)
  uniqueItems ? non_empty_size : Faker::Number.between(from: min_array, to: max_array(example_options[:depth]))
end
random_integer_example() click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 24
def random_integer_example
  if enum
    enum.to_a.sample
  else
    Faker::Number.between(from: int_rand_begin, to: int_rand_end) * int_multiple
  end
end
random_number_example() click to toggle source
# File lib/fakeit/openapi/example/number_example.rb, line 19
def random_number_example
  (Faker::Number.between(from: num_rand_begin, to: num_rand_end) * num_multiple)
    .then { multipleOf ? _1 : _1.round(2) }
end
random_string_example() click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 59
def random_string_example
  if enum then enum.to_a.sample
  elsif pattern then random_string_pattern
  elsif format then random_string_format
  elsif length_constraint then string_with_length
  else Faker::Book.title
  end
end
random_string_format(= (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 86
    def random_string_format = (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]

    def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)

    def unknown_format
      Logger.info("Unknown string format: #{format}")
      'Unknown string format'
    end
  end
end
random_string_pattern(= @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 88
  def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)

  def unknown_format
    Logger.info("Unknown string format: #{format}")
    'Unknown string format'
  end
end
static_integer_example() click to toggle source
# File lib/fakeit/openapi/example/integer_example.rb, line 16
def static_integer_example
  if enum
    enum.to_a.first
  else
    int_rand_end * int_multiple
  end
end
static_number_example(= (num_rand_end * num_multiple).then { multipleOf ? _1 : _1.round(2) }) click to toggle source
# File lib/fakeit/openapi/example/number_example.rb, line 17
def static_number_example = (num_rand_end * num_multiple).then { multipleOf ? _1 : _1.round(2) }

def random_number_example
  (Faker::Number.between(from: num_rand_begin, to: num_rand_end) * num_multiple)
    .then { multipleOf ? _1 : _1.round(2) }
end

def num_rand_begin = multipleOf ? (min_num / multipleOf).ceil : min_num

def num_rand_end = multipleOf ? (max_num / multipleOf).floor : max_num

def num_multiple = multipleOf || 1

def min_num = (minimum || MIN_NUM).to_f.ceil(2)

def max_num = (maximum || MAX_NUM).to_f.floor(2)
    
static_string_example() click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 41
def static_string_example
  fixed_faker do
    if enum then enum.to_a.first
    elsif pattern then static_string_pattern
    elsif format then static_string_format
    elsif length_constraint then static_string_with_length
    else 'string'
    end
  end
end
static_string_format(= (STATIC_FORMAT_HANDLERS[format] || method(:unknown_format))[]) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 70
def static_string_format = (STATIC_FORMAT_HANDLERS[format] || method(:unknown_format))[]

def static_string_pattern
  @static_string_pattern ||= @string_pattern.examples(
    max_repeater_variance: 1, max_group_results: 1, max_results_limit: 1
  ).first
end

def length_constraint = minLength || maxLength

def string_with_length = Faker::Internet.username(specifier: min_string_length..max_string_length)

def min_string_length = minLength || 0

def max_string_length = maxLength || min_string_length + 10

def random_string_format = (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]

def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)

def unknown_format
  Logger.info("Unknown string format: #{format}")
  'Unknown string format'
end
static_string_pattern() click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 72
def static_string_pattern
  @static_string_pattern ||= @string_pattern.examples(
    max_repeater_variance: 1, max_group_results: 1, max_results_limit: 1
  ).first
end
static_string_with_length(= '1' * max_string_length) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 68
def static_string_with_length = '1' * max_string_length

def static_string_format = (STATIC_FORMAT_HANDLERS[format] || method(:unknown_format))[]

def static_string_pattern
  @static_string_pattern ||= @string_pattern.examples(
    max_repeater_variance: 1, max_group_results: 1, max_results_limit: 1
  ).first
end

def length_constraint = minLength || maxLength

def string_with_length = Faker::Internet.username(specifier: min_string_length..max_string_length)

def min_string_length = minLength || 0

def max_string_length = maxLength || min_string_length + 10

def random_string_format = (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]

def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)

def unknown_format
  Logger.info("Unknown string format: #{format}")
  'Unknown string format'
string_example(example_options) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 29
def string_example(example_options)
  @string_pattern ||= Regexp.new(pattern) if pattern

  if example_options[:use_static][type: 'string', property: example_options[:property]]
    static_string_example
  else
    random_string_example
  end
end
string_with_length(= Faker::Internet.username(specifier: min_string_length..max_string_length)) click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 80
  def string_with_length = Faker::Internet.username(specifier: min_string_length..max_string_length)

  def min_string_length = minLength || 0

  def max_string_length = maxLength || min_string_length + 10

  def random_string_format = (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]

  def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)

  def unknown_format
    Logger.info("Unknown string format: #{format}")
    'Unknown string format'
  end
end
unknown_format() click to toggle source
# File lib/fakeit/openapi/example/string_example.rb, line 90
def unknown_format
  Logger.info("Unknown string format: #{format}")
  'Unknown string format'
end