Class: Apicraft::Mocker::AnyOf
- Defined in:
- lib/apicraft/mocker/any_of.rb
Overview
Generate mock based on the AnyOf schema
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
Constructor Details
This class inherits a constructor from Apicraft::Mocker::Base
Instance Method Details
#mock ⇒ Object
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 |