Class: Jsapi::Meta::Operation

Inherits:
Base
  • Object
show all
Defined in:
lib/jsapi/meta/operation.rb

Instance Method Summary collapse

Methods inherited from Base

#inspect, #reference?, #resolve

Methods included from Attributes::ClassMethods

#attribute, #attribute_names

Constructor Details

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

Returns a new instance of Operation.



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

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

Instance Method Details

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

:nodoc:



138
139
140
# File 'lib/jsapi/meta/operation.rb', line 138

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

#callbacksObject

:attr: callbacks The optional callbacks. Applies to OpenAPI 3.x.



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

attribute :callbacks, { String => OpenAPI::Callback }

#consumed_mime_typesObject Also known as: consumes

:attr: consumed_mime_types The MIME types consumed by the operation. Applies to OpenAPI 2.0 only.



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

attribute :consumed_mime_types, [String]

#deprecatedObject

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



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

attribute :deprecated, values: [true, false]

#descriptionObject

:attr: description The optional description of the operation.



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

attribute :description, String

#external_docsObject

:attr: external_docs The optional OpenAPI::ExternalDocumentation object.



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

attribute :external_docs, OpenAPI::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".



49
50
51
# File 'lib/jsapi/meta/operation.rb', line 49

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. The default model class is Model::Base.



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

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

#nameObject

:attr_reader: name The name of the operation.



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

attribute :name, writer: false

#parametersObject

:attr: parameters The parameters of the operation.



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

attribute :parameters, { String => Parameter },
default: {},
writer: false

#pathObject

:attr: path The relative path of the operation.



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

attribute :path, String

#produced_mime_typesObject Also known as: produces

:attr: consumed_mime_types The MIME types produced by the operation. Applies to OpenAPI 2.0 only.



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

attribute :produced_mime_types, [String]

#request_bodyObject

:attr: request_body The optional request body of the operation.



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

attribute :request_body, RequestBody

#responsesObject

:attr: responses The responses of the operation.



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

attribute :responses, { String => Response },
default: {},
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.



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

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

#security_requirementsObject

:attr: security_requirements The OpenAPI::SecurityRequirement objects.



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

attribute :security_requirements, [OpenAPI::SecurityRequirement]

#serversObject

:attr: servers The OpenAPI::Server objects. Applies to OpenAPI 3.x.



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

attribute :servers, [OpenAPI::Server]

#summaryObject

:attr: summary The optional summary of the operation.



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

attribute :summary, String

#tagsObject

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



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

attribute :tags, [String]

#to_openapi(version, definitions) ⇒ Object

Returns a hash representing the OpenAPI operation object.



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
# File 'lib/jsapi/meta/operation.rb', line 143

def to_openapi(version, definitions)
  version = OpenAPI::Version.from(version)
  {
    operationId: name,
    tags: tags,
    summary: summary,
    description: description,
    externalDocs: external_docs&.to_openapi,
    deprecated: deprecated?.presence,
    security: security_requirements&.map(&:to_openapi)
  }.tap do |hash|
    if version.major == 2
      hash[:consumes] = consumed_mime_types if consumed_mime_types
      hash[:produces] = produced_mime_types if produced_mime_types
      hash[:schemes] = schemes if schemes
    elsif servers
      hash[:servers] = servers.map(&:to_openapi)
    end
    # Parameters (and request body)
    hash[:parameters] = parameters.values.flat_map do |parameter|
      parameter.to_openapi(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 && version.major > 2
      hash[:callbacks] = callbacks.transform_values do |callback|
        callback.to_openapi(version, definitions)
      end
    end
  end.compact
end