Class: Jsapi::Meta::RequestBody::Base
- Inherits:
-
Model::Base
- Object
- Model::Base
- Jsapi::Meta::RequestBody::Base
- Includes:
- OpenAPI::Extensions
- Defined in:
- lib/jsapi/meta/request_body/base.rb
Overview
Specifies a request body.
Instance Method Summary collapse
-
#add_content(media_range = nil, keywords = {}) ⇒ Object
:nodoc:.
-
#attribute_changed(name) ⇒ Object
:nodoc:.
-
#content_for(media_type) ⇒ Object
Returns the most appropriate content for the given media type.
-
#contents ⇒ Object
:attr_reader: contents The alternative contents of the request body.
- #default_content ⇒ Object
- #default_media_range ⇒ Object
-
#description ⇒ Object
:attr: description The description of the request body.
-
#freeze_attributes ⇒ Object
:nodoc:.
-
#initialize(keywords = {}) ⇒ Base
constructor
A new instance of Base.
-
#to_openapi(version) ⇒ Object
Returns a hash representing the OpenAPI request body object.
-
#to_openapi_parameter ⇒ Object
Returns a hash representing the OpenAPI parameter 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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jsapi/meta/request_body/base.rb', line 24 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_range = nil, keywords = {}) ⇒ Object
:nodoc:
45 46 47 48 49 50 51 52 |
# File 'lib/jsapi/meta/request_body/base.rb', line 45 def add_content(media_range = nil, keywords = {}) # :nodoc: try_modify_attribute!(:contents) do media_range, keywords = nil, media_range if media_range.is_a?(Hash) media_range = Media::Range.from(media_range || Media::Range::APPLICATION_JSON) (@contents ||= {})[media_range] = Content.new(keywords) end end |
#attribute_changed(name) ⇒ Object
:nodoc:
40 41 42 43 |
# File 'lib/jsapi/meta/request_body/base.rb', line 40 def attribute_changed(name) # :nodoc: @default_media_range = @default_content = @sorted_contents = nil if name == :contents super end |
#content_for(media_type) ⇒ Object
Returns the most appropriate content for the given media type.
55 56 57 58 59 |
# File 'lib/jsapi/meta/request_body/base.rb', line 55 def content_for(media_type) (@sorted_contents ||= contents.sort) .find { |media_range, _content| media_range =~ media_type } &.second || default_content end |
#contents ⇒ Object
:attr_reader: contents The alternative contents of the request body. Maps instances of Media::Range to Content objects.
17 |
# File 'lib/jsapi/meta/request_body/base.rb', line 17 attribute :contents, { Media::Range => Content }, accessors: %i[reader writer] |
#default_content ⇒ Object
61 62 63 |
# File 'lib/jsapi/meta/request_body/base.rb', line 61 def default_content @default_content ||= contents.values.first end |
#default_media_range ⇒ Object
65 66 67 |
# File 'lib/jsapi/meta/request_body/base.rb', line 65 def default_media_range @default_media_range = contents.keys.first end |
#description ⇒ Object
:attr: description The description of the request body.
22 |
# File 'lib/jsapi/meta/request_body/base.rb', line 22 attribute :description, String |
#freeze_attributes ⇒ Object
:nodoc:
69 70 71 72 |
# File 'lib/jsapi/meta/request_body/base.rb', line 69 def freeze_attributes # :nodoc: add_content if contents.blank? super end |
#to_openapi(version) ⇒ Object
Returns a hash representing the OpenAPI request body object.
Applies to OpenAPI 3.0 and higher.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/jsapi/meta/request_body/base.rb', line 94 def to_openapi(version, *) with_openapi_extensions( description: description, content: contents.transform_values do |content| content.to_openapi(version) end, required: contents.values.all? do |content| content.schema.existence >= Existence::ALLOW_NIL end ) end |
#to_openapi_parameter ⇒ Object
Returns a hash representing the OpenAPI parameter object.
Applies to OpenAPI 2.0.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/jsapi/meta/request_body/base.rb', line 77 def to_openapi_parameter schema = default_content.schema with_openapi_extensions( { name: 'body', in: 'body', description: description, required: schema.existence >= Existence::ALLOW_NIL, **schema.to_openapi(OpenAPI::V2_0) } ) end |