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

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

Overview

Defines a response.

Constant Summary

Constants included from Base::Attributes

Base::Attributes::DEFAULT_ARRAY, Base::Attributes::DEFAULT_HASH

Instance Method Summary collapse

Methods included from OpenAPI::Extensions

#add_openapi_extension, #openapi_extensions, #openapi_extensions=

Methods inherited from Base::Model

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

Methods included from Base::Attributes

#attribute, #attribute_names

Constructor Details

#initialize(keywords = {}) ⇒ Model

Returns a new instance of Model.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jsapi/meta/response/model.rb', line 47

def initialize(keywords = {})
  keywords = keywords.dup
  super(
    keywords.extract!(
      :content_type, :description, :examples, :headers,
      :links, :locale, :openapi_extensions
    )
  )
  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 content type. "application/json" by default.



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

attribute :content_type, String, default: 'application/json'

#descriptionObject

:attr: description The description of the response.



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

attribute :description, String

#examplesObject

:attr: examples The Example objects.



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

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

#headersObject

:attr: headers The Header objects.



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

attribute :headers, { String => Header }

:attr: links The Link objects.



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

attribute :links, { String => Link }

#localeObject

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



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

attribute :locale, Symbol

#schemaObject

:attr_reader: schema The Schema of the response.



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

attribute :schema, read_only: true

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI response object.



62
63
64
65
66
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
# File 'lib/jsapi/meta/response/model.rb', line 62

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

  with_openapi_extensions(
    if version.major == 2
      {
        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 => example.resolve(definitions).value }
          end
        )
      }
    else
      {
        description: description,
        content: {
          content_type => {
            schema: schema.to_openapi(version),
            examples: examples.transform_values(&:to_openapi).presence
          }.compact
        },
        headers: headers.transform_values do |header|
          header.to_openapi(version)
        end.presence,
        links: links.transform_values(&:to_openapi).presence
      }
    end
  )
end