Class: Jsapi::Meta::Operation

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

Overview

Specifies an API operation.

Constant Summary

Constants included from Model::Attributes

Model::Attributes::DEFAULT_ARRAY, Model::Attributes::DEFAULT_HASH

Instance Method Summary collapse

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

included

Methods inherited from Model::Base

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

Methods included from Model::Attributes

#attribute, #attribute_names

Constructor Details

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



103
104
105
106
107
108
109
# File 'lib/jsapi/meta/operation.rb', line 103

def initialize(name, parent_path = nil, keywords = {})
  parent_path, keywords = nil, parent_path if parent_path.is_a?(Hash)

  @name = name&.to_s
  @parent_path = Pathname.from(parent_path)
  super(keywords)
end

Instance Method Details

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

:nodoc:



111
112
113
# File 'lib/jsapi/meta/operation.rb', line 111

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 media type consumed by the operation.



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

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

#full_pathObject

Returns the full path of the operation as a Pathname.



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

def full_path
  parent_path + path
end

#methodObject

:attr: method The HTTP method of the operation, "get" by default.



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

attribute :method, String, default: 'get'

#modelObject

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



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

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

#nameObject

:attr_reader: name The name of the operation.



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

attribute :name, accessors: i[reader]

#parametersObject

:attr: parameters The parameters of the operation.



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

attribute :parameters, { String => Parameter }, accessors: i[reader writer]

#parent_pathObject

:attr_reader: parent_path The parent path as a Pathname.



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

attribute :parent_path, Pathname, accessors: i[reader]

#pathObject

:attr: path The relative path of the operation as a Pathname.



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

attribute :path, Pathname

#produces(definitions) ⇒ Object

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



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

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.



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

attribute :request_body, RequestBody

#resolved_parameters(definitions) ⇒ Object

Merges the parameters of this operation and the common parameters of all parent pathes and resolves them.



134
135
136
137
# File 'lib/jsapi/meta/operation.rb', line 134

def resolved_parameters(definitions)
  (definitions.path_parameters(full_path).presence&.merge(parameters) || parameters)
    .transform_values { |parameter| parameter.resolve(definitions) }
end

#responsesObject

:attr: responses The responses of the operation.



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

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.



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

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

#security_requirementsObject

:attr: security_requirements The SecurityRequirement objects.



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

attribute :security_requirements, [SecurityRequirement]

#serversObject

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



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

attribute :servers, [Server]

#summaryObject

:attr: summary The short description of the operation.



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

attribute :summary, String

#tagsObject

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



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

attribute :tags, [String]

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI operation object.



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
178
179
180
181
182
183
184
185
186
187
# File 'lib/jsapi/meta/operation.rb', line 140

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 |result|
    if version.major == 2
      if (consumes = consumes(definitions)).present?
        result[:consumes] = [consumes]
      end
      if (produces = produces(definitions)).present?
        result[:produces] = produces.map(&:to_s)
      end
      result[:schemes] = schemes if schemes.present?
    elsif servers.present?
      result[:servers] = servers.map do |server|
        server.to_openapi(version)
      end
    end
    # Parameters (and request body)
    result[:parameters] = parameters.values.flat_map do |parameter|
      parameter.to_openapi_parameters(version, definitions)
    end
    if request_body
      if version.major == 2
        result[:parameters] << request_body.resolve(definitions).to_openapi_parameter
      else
        result[:request_body] = request_body.to_openapi(version)
      end
    end
    # Responses
    result[:responses] = responses.transform_values do |response|
      response.to_openapi(version, definitions)
    end
    # Callbacks
    if callbacks.present? && version.major > 2
      result[:callbacks] = callbacks.transform_values do |callback|
        callback.to_openapi(version, definitions)
      end
    end
  end
end