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 collapse

JSON_TYPE =

:nodoc:

%r{(^application/|^text/|\+)json$}.freeze

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.



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

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.



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

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

#descriptionObject

:attr: description The description of the response.



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

attribute :description, String

#examplesObject

:attr: examples The Example objects.



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

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

#headersObject

:attr: headers The Header objects.



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

attribute :headers, { String => Header }

#json_type?Boolean

Returns true if content type is a JSON MIME type as specified by mimesniff.spec.whatwg.org/#json-mime-type.

Returns:

  • (Boolean)


65
66
67
# File 'lib/jsapi/meta/response/base.rb', line 65

def json_type?
  content_type.match?(JSON_TYPE)
end

:attr: links The Link objects.



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

attribute :links, { String => Link }

#localeObject

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



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

attribute :locale, Symbol

#schemaObject

:attr_reader: schema The Schema of the response.



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

attribute :schema, accessors: %i[reader]

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI response object.



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
# File 'lib/jsapi/meta/response/base.rb', line 70

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