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.

Instance Method Summary collapse

Methods included from OpenAPI::Extensions

included

Methods inherited from Model::Base

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

Methods included from Model::Attributes

#attributes_frozen?, #freeze_attributes, included

Constructor Details

#initialize(keywords = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(keywords = {})
  keywords = keywords.dup
   = keywords.slice!(*self.class.attribute_names)

  # Move content-related keywords to :contents so that the first
  # key-value pair in @contents is created from them.
  if .present?
    content_type = .delete(:content_type)

    (keywords[:contents] ||= {}).reverse_merge!(
      { content_type =>  }
    )
  end
  super(keywords)
end

Instance Method Details

#add_content(media_type = nil, keywords = {}) ⇒ Object

:nodoc:



74
75
76
77
78
79
80
81
# File 'lib/jsapi/meta/response/base.rb', line 74

def add_content(media_type = nil, keywords = {}) # :nodoc:
  try_modify_attribute!(:contents) do
    media_type, keywords = nil, media_type if media_type.is_a?(Hash)
    media_type = Media::Type.from(media_type || Media::Type::APPLICATION_JSON)

    (@contents ||= {})[media_type] = Content.new(keywords)
  end
end

#attribute_changed(name) ⇒ Object

:nodoc:



69
70
71
72
# File 'lib/jsapi/meta/response/base.rb', line 69

def attribute_changed(name) # :nodoc:
  @default_media_type = nil if name == :contents
  super
end

#contentsObject

:attr_reader: contents The alternative contents of the response. Maps instances of Media::Range to Content objects.



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

attribute :contents, { Media::Type => Content }, accessors: %i[reader writer]

#default_media_typeObject



83
84
85
# File 'lib/jsapi/meta/response/base.rb', line 83

def default_media_type
  @default_media_type ||= contents.keys.first
end

#descriptionObject

:attr: description The description of the response.



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

attribute :description, String

#headersObject

:attr: headers The headers of the response. Maps header names to Header objects or references.



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

attribute :headers, { String => Header }

:attr: links The linked operations. Maps link names to Link objects.



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

attribute :links, { String => Link }

#localeObject

:attr: locale The locale to be used instead of the default locale when rendering a response.



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

attribute :locale, Symbol

#media_type_and_content_for(*media_ranges) ⇒ Object

Returns the most appropriate media type and content for the given media ranges.



89
90
91
92
93
94
95
96
97
98
# File 'lib/jsapi/meta/response/base.rb', line 89

def media_type_and_content_for(*media_ranges)
  media_ranges
    .filter_map { |media_range| Media::Range.try_from(media_range) }
    .sort # e.g. "text/plain" before "text/*" before "*/*"
    .lazy.map do |media_range|
      contents.find do |media_type_and_content|
        media_range =~ media_type_and_content.first
      end
    end.first || contents.first
end

#nodocObject

:attr: nodoc Prevents the response to be described in generated OpenAPI documents.



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

attribute :nodoc, values: [true, false], default: false

#summaryObject

:attr: summary The short description of the response.

Applies to OpenAPI 3.2 and higher.



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

attribute :summary, String

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI response object.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jsapi/meta/response/base.rb', line 101

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

  with_openapi_extensions(
    if version == OpenAPI::V2_0
      media_type, content = contents.first
      example = content&.examples&.values&.first
      {
        description: description,
        schema: content&.schema&.to_openapi(version),
        headers:
          headers.transform_values do |header|
            header.to_openapi(version) unless header.reference?
          end.compact.presence,
        examples:
          if media_type.present? && example.present?
            { media_type => 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:
          contents.to_h do |nth_media_type, nth_content|
            [nth_media_type, nth_content.to_openapi(version, nth_media_type)]
          end.presence,
        links:
          links.transform_values do |link|
            link.to_openapi(version)
          end.presence
      }
    end
  )
end