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.
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
-
#content_type ⇒ Object
:attr: content_type The content type.
-
#description ⇒ Object
:attr: description The description of the response.
-
#examples ⇒ Object
:attr: examples The Example objects.
-
#headers ⇒ Object
:attr: headers The Header objects.
-
#initialize(keywords = {}) ⇒ Base
constructor
A new instance of Base.
-
#json_type? ⇒ Boolean
Returns true if content type is a JSON MIME type as specified by mimesniff.spec.whatwg.org/#json-mime-type.
-
#links ⇒ Object
:attr: links The Link objects.
-
#locale ⇒ Object
:attr: locale The locale used when rendering a response.
-
#schema ⇒ Object
:attr_reader: schema The Schema 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
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_type ⇒ Object
: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' |
#description ⇒ Object
:attr: description The description of the response.
22 |
# File 'lib/jsapi/meta/response/base.rb', line 22 attribute :description, String |
#examples ⇒ Object
:attr: examples The Example objects.
27 |
# File 'lib/jsapi/meta/response/base.rb', line 27 attribute :examples, { String => Example }, default_key: 'default' |
#headers ⇒ Object
: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.
65 66 67 |
# File 'lib/jsapi/meta/response/base.rb', line 65 def json_type? content_type.match?(JSON_TYPE) end |
#links ⇒ Object
:attr: links The Link objects.
37 |
# File 'lib/jsapi/meta/response/base.rb', line 37 attribute :links, { String => Link } |
#locale ⇒ Object
:attr: locale The locale used when rendering a response.
42 |
# File 'lib/jsapi/meta/response/base.rb', line 42 attribute :locale, Symbol |
#schema ⇒ Object
: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 |