Class: Jsapi::Meta::Response::Base

Inherits:
Model::Base show all
Includes:
OpenAPI::Extensions
Defined in:
lib/jsapi/meta/response/base.rb

Overview

Specifies a response.

Constant Summary

Constants included from Model::Attributes

Model::Attributes::DEFAULT_ARRAY, Model::Attributes::DEFAULT_HASH

Instance Method Summary collapse

Methods included from OpenAPI::Extensions

included

Methods inherited from Model::Base

#inspect, #merge!, #reference?, #resolve

Methods included from Model::Attributes

#attribute, #attribute_names

Constructor Details

#initialize(keywords = {}) ⇒ Base

Returns a new instance of Base.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsapi/meta/response/base.rb', line 52

def initialize(keywords = {})
  keywords = keywords.dup
  super(
    keywords.extract!(
      :content_type, :description, :examples, :headers,
      :links, :locale, :openapi_extensions, :summary
    )
  )
  add_example(value: keywords.delete(:example)) if keywords.key?(:example)
  keywords[:ref] = keywords.delete(:schema) if keywords.key?(:schema)

  @schema = Schema.new(keywords)
end

Instance Method Details

#content_typeObject

:attr: content_type The media type of the response, "application/json" by default.



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

attribute :content_type, Media::Type, default: Media::Type::APPLICATION_JSON

#descriptionObject

:attr: description The description of the response.



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

attribute :description, String

#examplesObject

:attr: examples The Example objects.



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

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

#headersObject

:attr: headers The Header objects.



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

attribute :headers, { String => Header }

:attr: links The Link objects.



35
# File 'lib/jsapi/meta/response/base.rb', line 35

attribute :links, { String => Link }

#localeObject

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



40
# File 'lib/jsapi/meta/response/base.rb', line 40

attribute :locale, Symbol

#schemaObject

:attr_reader: schema The Schema of the response.



45
# File 'lib/jsapi/meta/response/base.rb', line 45

attribute :schema, accessors: i[reader]

#summaryObject

:attr: summary The short description of the response. Applies to OpenAPI 3.2 and higher.



50
# File 'lib/jsapi/meta/response/base.rb', line 50

attribute :summary, String

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI response object.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/jsapi/meta/response/base.rb', line 67

def to_openapi(version, definitions)
  version = OpenAPI::Version.from(version)

  with_openapi_extensions(
    if version == OpenAPI::V2_0
      {
        description: description,
        schema: schema.to_openapi(version),
        headers: headers.transform_values do |header|
          header.to_openapi(version) unless header.reference?
        end.compact.presence,
        examples: (
          if (example = examples.values.first).present?
            { content_type.to_s => example.resolve(definitions).value }
          end
        )
      }
    else
      {
        summary: (summary if version >= OpenAPI::V3_2),
        description: description,
        headers: headers.transform_values do |header|
          header.to_openapi(version)
        end.presence,
        content: {
          content_type.to_s => {
            **if content_type == Media::Type::APPLICATION_JSON_SEQ &&
              schema.array? && version >= OpenAPI::V3_2
                { itemSchema: schema.items.to_openapi(version) }
              else
                { schema: schema.to_openapi(version) }
              end,
            examples: examples.transform_values(&:to_openapi).presence
          }.compact
        },
        links: links.transform_values do |link|
          link.to_openapi(version)
        end.presence
      }
    end
  )
end