Class: Jsapi::Meta::Response::Base
- Inherits:
-
Model::Base
- Object
- Model::Base
- Jsapi::Meta::Response::Base
- Includes:
- OpenAPI::Extensions
- Defined in:
- lib/jsapi/meta/response/base.rb
Overview
Specifies a response.
Instance Method Summary collapse
-
#add_content(media_type = nil, keywords = {}) ⇒ Object
:nodoc:.
-
#attribute_changed(name) ⇒ Object
:nodoc:.
-
#contents ⇒ Object
:attr_reader: contents The alternative contents of the response.
- #default_media_type ⇒ Object
-
#description ⇒ Object
:attr: description The description of the response.
-
#headers ⇒ Object
:attr: headers The headers of the response.
-
#initialize(keywords = {}) ⇒ Base
constructor
A new instance of Base.
-
#links ⇒ Object
:attr: links The linked operations.
-
#locale ⇒ Object
:attr: locale The locale to be used instead of the default locale when rendering a response.
-
#media_type_and_content_for(*media_ranges) ⇒ Object
Returns the most appropriate media type and content for the given media ranges.
-
#nodoc ⇒ Object
:attr: nodoc Prevents the response to be described in generated OpenAPI documents.
-
#summary ⇒ Object
:attr: summary The short description of the response.
-
#to_openapi(version, definitions) ⇒ Object
Returns a hash representing the OpenAPI response object.
Methods included from OpenAPI::Extensions
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 content_keywords = 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 content_keywords.present? content_type = content_keywords.delete(:content_type) (keywords[:contents] ||= {}).reverse_merge!( { content_type => content_keywords } ) 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 |
#contents ⇒ Object
: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_type ⇒ Object
83 84 85 |
# File 'lib/jsapi/meta/response/base.rb', line 83 def default_media_type @default_media_type ||= contents.keys.first end |
#description ⇒ Object
:attr: description The description of the response.
22 |
# File 'lib/jsapi/meta/response/base.rb', line 22 attribute :description, String |
#headers ⇒ Object
: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 } |
#links ⇒ Object
: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 } |
#locale ⇒ Object
: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 |
#nodoc ⇒ Object
: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 |
#summary ⇒ Object
: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 |