Module: Fakeit::Openapi::Example

Included in:
Schema
Defined in:
lib/fakeit/openapi/example/array_example.rb,
lib/fakeit/openapi/example/number_example.rb,
lib/fakeit/openapi/example/object_example.rb,
lib/fakeit/openapi/example/string_example.rb,
lib/fakeit/openapi/example/boolean_example.rb,
lib/fakeit/openapi/example/integer_example.rb

Constant Summary collapse

MIN_NUM =
-2**31
MAX_NUM =
(2**31) - 1
STATIC_FORMAT_HANDLERS =
{
  "uri" => -> { "https://some.uri" },
  "uuid" => -> { "11111111-1111-1111-1111-111111111111" },
  "guid" => -> { "11111111-1111-1111-1111-111111111111" },
  "email" => -> { "[email protected]" },
  "date" => -> { Date.today.iso8601 },
  "date-time" => lambda do
    now = Time.now
    Time.new(now.year, now.month, now.day, 0, 0, 0, now.utc_offset).iso8601
  end,
  "binary" => -> { "binary" },
  "byte" => -> { "Ynl0ZQ==" }
}.freeze
RANDOM_FORMAT_HANDLERS =
{
  "uri" => -> { Faker::Internet.url },
  "uuid" => -> { SecureRandom.uuid },
  "guid" => -> { SecureRandom.uuid },
  "email" => -> { Faker::Internet.email },
  "date" => -> { Faker::Date.backward(days: 100).iso8601 },
  "date-time" => -> { Faker::Time.backward(days: 100).iso8601 },
  "binary" => -> { Faker::String.random(length: 1..30) },
  "byte" => -> { Base64.strict_encode64(Faker::String.random(length: 1..30)) }
}.freeze
DEFAULT_BITS =
32

Instance Method Summary collapse

Instance Method Details

#array_example(options) ⇒ Object



4
5
6
7
8
9
10
11
# 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

#boolean_example(example_options) ⇒ Object



4
5
6
# 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

#integer_example(example_options) ⇒ Object



6
7
8
9
10
11
12
# 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

#number_example(example_options) ⇒ Object



7
8
9
10
11
12
13
# 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) ⇒ Object



4
5
6
7
8
# 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

#string_example(example_options) ⇒ Object



29
30
31
32
33
34
35
36
37
# 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