Class: Jsapi::Meta::Response::Model

Inherits:
Base
  • Object
show all
Defined in:
lib/jsapi/meta/response/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.



34
35
36
37
38
39
40
41
# File 'lib/jsapi/meta/response/model.rb', line 34

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

  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 response.



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

attribute :description, String

#examplesObject

:attr: examples The optional examples.



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

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

:attr: links The optional OpenAPI::Link objects.



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

attribute :links, { String => OpenAPI::Link }

#localeObject

:attr: locale The locale used when rendering a response.



25
# File 'lib/jsapi/meta/response/model.rb', line 25

attribute :locale, Symbol

#schemaObject

:attr_reader: schema The Schema of the parameter.



30
# File 'lib/jsapi/meta/response/model.rb', line 30

attribute :schema, writer: false

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI response object.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jsapi/meta/response/model.rb', line 44

def to_openapi(version, definitions)
  version = OpenAPI::Version.from(version)
  if version.major == 2
    {
      description: description,
      schema: schema.to_openapi(version),
      examples: (
        if (example = examples&.values&.first).present?
          { 'application/json' => example.resolve(definitions).value }
        end
      )
    }
  else
    {
      description: description,
      content: {
        'application/json' => {
          schema: schema.to_openapi(version),
          examples: examples&.transform_values(&:to_openapi)
        }.compact
      },
      links: links&.transform_values(&:to_openapi)
    }
  end.compact
end