Class: Jsapi::Meta::Operation

Inherits:
Base::Model show all
Includes:
Jsapi::Meta::OpenAPI::Extensions
Defined in:
lib/jsapi/meta/operation.rb,
lib/jsapi/meta/callback/model.rb

Overview

Defines an API operation.

Constant Summary

Constants included from Base::Attributes

Base::Attributes::DEFAULT_ARRAY, Base::Attributes::DEFAULT_HASH

Instance Method Summary collapse

Methods included from Jsapi::Meta::OpenAPI::Extensions

#add_openapi_extension, #openapi_extensions, #openapi_extensions=

Methods inherited from Base::Model

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

Methods included from Base::Attributes

#attribute, #attribute_names

Constructor Details

#initialize(name = nil, keywords = {}) ⇒ Operation

Returns a new instance of Operation.



108
109
110
111
# File 'lib/jsapi/meta/operation.rb', line 108

def initialize(name = nil, keywords = {})
  @name = name&.to_s
  super(keywords)
end

Instance Method Details

#add_parameter(name, keywords = {}) ⇒ Object

:nodoc:



115
116
117
# File 'lib/jsapi/meta/operation.rb', line 115

def add_parameter(name, keywords = {}) # :nodoc:
  (@parameters ||= {})[name.to_s] = Parameter.new(name, keywords)
end

#callbacksObject

:attr: callbacks The Callback objects. Applies to OpenAPI 3.0 and higher.



12
# File 'lib/jsapi/meta/operation.rb', line 12

attribute :callbacks, { String => Callback }

#consumes(definitions) ⇒ Object

Returns the MIME type consumed by the operation.



120
121
122
# File 'lib/jsapi/meta/operation.rb', line 120

def consumes(definitions)
  request_body&.resolve(definitions)&.content_type
end

#deprecatedObject

:attr: deprecated Specifies whether or not the operation is deprecated.



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

attribute :deprecated, values: [true, false]

#descriptionObject

:attr: description The description of the operation.



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

attribute :description, String

#external_docsObject

:attr: external_docs The ExternalDocumentation object.



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

attribute :external_docs, ExternalDocumentation

#methodObject

:attr: method The HTTP verb of the operation. Possible values are:

  • "delete"

  • "get"

  • "head"

  • "options"

  • "patch"

  • "post"

  • "put"

The default HTTP verb is "get".



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

attribute :method, values: %w[delete get head options patch post put], default: 'get'

#modelObject

:attr: model The model class to access top-level parameters by. Model::Base by default.



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

attribute :model, Class, default: Model::Base

#nameObject

:attr_reader: name The name of the operation.



52
# File 'lib/jsapi/meta/operation.rb', line 52

attribute :name, read_only: true

#parametersObject

:attr: parameters The parameters of the operation.



57
# File 'lib/jsapi/meta/operation.rb', line 57

attribute :parameters, { String => Parameter }

#pathObject

:attr: path The relative path of the operation.



62
# File 'lib/jsapi/meta/operation.rb', line 62

attribute :path, String

#produces(definitions) ⇒ Object

Returns an array containing the MIME types produced by the operation.



125
126
127
128
129
# File 'lib/jsapi/meta/operation.rb', line 125

def produces(definitions)
  responses.values.filter_map do |response|
    response.resolve(definitions).content_type
  end.uniq.sort
end

#request_bodyObject

:attr: request_body The request body of the operation.



67
# File 'lib/jsapi/meta/operation.rb', line 67

attribute :request_body, RequestBody

#responsesObject

:attr: responses The responses of the operation.



72
# File 'lib/jsapi/meta/operation.rb', line 72

attribute :responses, { String => Response }, default_key: 'default'

#schemesObject

:attr: schemes The transfer protocols supported by the operation. Possible values are:

  • "http"

  • "https"

  • "ws"

  • "wss"

Applies to OpenAPI 2.0 only.



84
# File 'lib/jsapi/meta/operation.rb', line 84

attribute :schemes, [String], values: %w[http https ws wss]

#security_requirementsObject

:attr: security_requirements The SecurityRequirement objects.



89
# File 'lib/jsapi/meta/operation.rb', line 89

attribute :security_requirements, [SecurityRequirement]

#serversObject

:attr: servers The Server objects. Applies to OpenAPI 3.0 and higher.



96
# File 'lib/jsapi/meta/operation.rb', line 96

attribute :servers, [Server]

#summaryObject

:attr: summary The short summary of the operation.



101
# File 'lib/jsapi/meta/operation.rb', line 101

attribute :summary, String

#tagsObject

:attr: tags The tags used to group operations in an OpenAPI document.



106
# File 'lib/jsapi/meta/operation.rb', line 106

attribute :tags, [String]

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI operation object.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jsapi/meta/operation.rb', line 132

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

  with_openapi_extensions(
    operationId: name,
    tags: tags.presence,
    summary: summary,
    description: description,
    externalDocs: external_docs&.to_openapi,
    deprecated: deprecated?.presence,
    security: security_requirements.map(&:to_openapi).presence
  ).tap do |hash|
    if version.major == 2
      if (consumes = consumes(definitions)).present?
        hash[:consumes] = [consumes]
      end
      if (produces = produces(definitions)).present?
        hash[:produces] = produces
      end
      hash[:schemes] = schemes if schemes.present?
    elsif servers.present?
      hash[:servers] = servers.map(&:to_openapi)
    end
    # Parameters (and request body)
    hash[:parameters] = parameters.values.flat_map do |parameter|
      parameter.to_openapi_parameters(version, definitions)
    end
    if request_body
      if version.major == 2
        hash[:parameters] << request_body.resolve(definitions).to_openapi_parameter
      else
        hash[:request_body] = request_body.to_openapi(version)
      end
    end
    # Responses
    hash[:responses] = responses.transform_values do |response|
      response.to_openapi(version, definitions)
    end
    # Callbacks
    if callbacks.present? && version.major > 2
      hash[:callbacks] = callbacks.transform_values do |callback|
        callback.to_openapi(version, definitions)
      end
    end
  end
end