Class: Jsapi::Meta::RequestBody::Model

Inherits:
Base
  • Object
show all
Defined in:
lib/jsapi/meta/request_body/model.rb

Instance Method Summary collapse

Methods inherited from Base

#inspect, #reference?, #resolve

Methods included from Attributes::ClassMethods

#attribute, #attribute_names

Constructor Details

#initialize(keywords = {}) ⇒ Model

Returns a new instance of Model.



24
25
26
27
28
29
30
31
# File 'lib/jsapi/meta/request_body/model.rb', line 24

def initialize(keywords = {})
  keywords = keywords.dup
  super(keywords.extract!(:description, :examples))

  add_example(value: keywords.delete(:example)) if keywords.key?(:example)

  @schema = Schema.new(keywords)
end

Instance Method Details

#descriptionObject

:attr: description The optional description of the request body.



10
# File 'lib/jsapi/meta/request_body/model.rb', line 10

attribute :description, String

#examplesObject

:attr_reader: examples The optional examples.



15
# File 'lib/jsapi/meta/request_body/model.rb', line 15

attribute :examples, { String => Example }, default_key: 'default'

#required?Boolean

Returns true if the level of existence is greater than or equal to ALLOW_NIL, false otherwise.

Returns:

  • (Boolean)


35
36
37
# File 'lib/jsapi/meta/request_body/model.rb', line 35

def required?
  schema.existence >= Existence::ALLOW_NIL
end

#schemaObject

:attr_reader: schema The Schema of the parameter.



20
# File 'lib/jsapi/meta/request_body/model.rb', line 20

attribute :schema, writer: false

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI 3.x request body object.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jsapi/meta/request_body/model.rb', line 50

def to_openapi(version)
  {
    description: description,
    content: {
      'application/json' => {
        schema: schema.to_openapi(version),
        examples: examples&.transform_values(&:to_openapi)
      }.compact
    },
    required: required?
  }.compact
end

#to_openapi_parameterObject

Returns a hash representing the OpenAPI 2.0 parameter object.



40
41
42
43
44
45
46
47
# File 'lib/jsapi/meta/request_body/model.rb', line 40

def to_openapi_parameter
  {
    name: 'body',
    in: 'body',
    description: description,
    required: required?
  }.merge(schema.to_openapi('2.0')).compact
end