Class: Apicraft::Mocker::AnyOf

Inherits:
Base
  • Object
show all
Defined in:
lib/apicraft/mocker/any_of.rb

Overview

Generate mock based on the AnyOf schema

Instance Attribute Summary

Attributes inherited from Base

#schema

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Apicraft::Mocker::Base

Instance Method Details

#mockObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/apicraft/mocker/any_of.rb', line 7

def mock
  num = schema.any_of.size.times.to_a.sample

  # Pick a random number of schemas
  # to build the response off of
  res = schema.any_of.sample(num + 1).map do |s|
    Mocker.mock(s)
  end
  type = res[0].class

  # TODO: This is not an accurate implementation
  # especially when it comes to numbers and strings
  # Ideally, we would want to combine rules from
  # all the schemas and then generate a value.
  if type.is_a?(Hash)
    res.reduce(&:merge)
  elsif type.is_a?(Array)
    res.reduce(&:concat)
  else
    res.sample
  end
end