Module: Scorpio::OpenAPI::Operation
- Includes:
- Configurables
- Included in:
- V2::Operation, V3::Operation
- Defined in:
- lib/scorpio/openapi/operation.rb
Overview
An OpenAPI operation
Scorpio::OpenAPI::Operation is a module common to V2 and V3 operations.
Defined Under Namespace
Modules: Configurables
Instance Attribute Summary
Attributes included from Configurables
#base_url, #faraday_adapter, #faraday_builder, #logger, #request_headers, #user_agent
Instance Method Summary collapse
- #build_request(*a, &b) ⇒ Scorpio::Request
-
#http_method ⇒ Object
The HTTP method of this operation as indicated by the attribute name for this operation from the parent PathItem.
-
#human_id ⇒ String
A short identifier for this operation appropriate for an error message.
-
#inferred_parameters ⇒ #to_ary<#to_h>
this method is not intended to be API-stable at the moment.
- #oa_response(status:) ⇒ Scorpio::OpenAPI::V3::Response, Scorpio::OpenAPI::V2::Response
-
#openapi_document ⇒ Scorpio::OpenAPI::Document
The document whence this operation came.
-
#path_template ⇒ Addressable::Template
The path as an Addressable::Template.
- #path_template_str ⇒ Object
-
#request_accessor_module ⇒ Module
A module with accessor methods for unambiguously named parameters of this operation.
-
#run(*a, &b) ⇒ Object
Response body object.
-
#run_ur(*a, &b) ⇒ Scorpio::Ur
Response ur.
-
#uri_template(base_url: self.base_url) ⇒ Addressable::Template
The URI template, consisting of the base_url concatenated with the path template.
-
#v2? ⇒ Boolean
V2?.
-
#v3? ⇒ Boolean
V3?.
Instance Method Details
#build_request(*a, &b) ⇒ Scorpio::Request
154 155 156 |
# File 'lib/scorpio/openapi/operation.rb', line 154 def build_request(*a, &b) Scorpio::Request.new(self, *a, &b) end |
#http_method ⇒ Object
Returns the HTTP method of this operation as indicated by the attribute name for this operation from the parent PathItem.
88 89 90 91 92 |
# File 'lib/scorpio/openapi/operation.rb', line 88 def http_method return @http_method if instance_variable_defined?(:@http_method) raise(Bug) unless parent_jsi.is_a?(Scorpio::OpenAPI::V2::PathItem) || parent_jsi.is_a?(Scorpio::OpenAPI::V3::PathItem) @http_method = jsi_ptr.reference_tokens.last end |
#human_id ⇒ String
Returns a short identifier for this operation appropriate for an error message.
95 96 97 |
# File 'lib/scorpio/openapi/operation.rb', line 95 def human_id operationId || "path: #{path_template_str}, method: #{http_method}" end |
#inferred_parameters ⇒ #to_ary<#to_h>
this method is not intended to be API-stable at the moment.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/scorpio/openapi/operation.rb', line 113 def inferred_parameters parameters = self.parameters ? self.parameters.to_a.dup : [] path_template.variables.each do |var| unless parameters.any? { |p| p['in'] == 'path' && p['name'] == var } # we could instantiate this as a V2::Parameter or a V3::Parameter # or a ParameterWithContentInPath or whatever. but I can't be bothered. parameters << { 'name' => var, 'in' => 'path', 'required' => true, 'type' => 'string', } end end parameters end |
#oa_response(status:) ⇒ Scorpio::OpenAPI::V3::Response, Scorpio::OpenAPI::V2::Response
100 101 102 103 104 105 106 107 |
# File 'lib/scorpio/openapi/operation.rb', line 100 def oa_response(status: ) status = status.to_s if status.is_a?(Numeric) if self.responses _, oa_response = self.responses.detect { |k, v| k.to_s == status } oa_response ||= self.responses['default'] end oa_response end |
#openapi_document ⇒ Scorpio::OpenAPI::Document
Returns the document whence this operation came.
57 58 59 |
# File 'lib/scorpio/openapi/operation.rb', line 57 def openapi_document parent_jsis.detect { |p| p.is_a?(Scorpio::OpenAPI::Document) } end |
#path_template ⇒ Addressable::Template
Returns the path as an Addressable::Template.
69 70 71 72 |
# File 'lib/scorpio/openapi/operation.rb', line 69 def path_template return @path_template if instance_variable_defined?(:@path_template) @path_template = Addressable::Template.new(path_template_str) end |
#path_template_str ⇒ Object
61 62 63 64 65 66 |
# File 'lib/scorpio/openapi/operation.rb', line 61 def path_template_str return @path_template_str if instance_variable_defined?(:@path_template_str) raise(Bug) unless parent_jsi.is_a?(Scorpio::OpenAPI::V2::PathItem) || parent_jsi.is_a?(Scorpio::OpenAPI::V3::PathItem) raise(Bug) unless parent_jsi.parent_jsi.is_a?(Scorpio::OpenAPI::V2::Paths) || parent_jsi.parent_jsi.is_a?(Scorpio::OpenAPI::V3::Paths) @path_template_str = parent_jsi.jsi_ptr.reference_tokens.last end |
#request_accessor_module ⇒ Module
Returns a module with accessor methods for unambiguously named parameters of this operation.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/scorpio/openapi/operation.rb', line 131 def request_accessor_module return @request_accessor_module if instance_variable_defined?(:@request_accessor_module) @request_accessor_module = begin params_by_name = inferred_parameters.group_by { |p| p['name'] } Module.new do instance_method_modules = [Request, Request::Configurables] instance_method_names = instance_method_modules.map do |mod| (mod.instance_methods + mod.private_instance_methods).map(&:to_s) end.inject(Set.new, &:|) params_by_name.each do |name, params| next if instance_method_names.include?(name) if params.size == 1 param = params.first define_method("#{name}=") { |value| set_param_from(param['in'], param['name'], value) } define_method(name) { get_param_from(param['in'], param['name']) } end end end end end |
#run(*a, &b) ⇒ Object
Returns response body object.
166 167 168 |
# File 'lib/scorpio/openapi/operation.rb', line 166 def run(*a, &b) build_request(*a, &b).run end |
#run_ur(*a, &b) ⇒ Scorpio::Ur
Returns response ur.
160 161 162 |
# File 'lib/scorpio/openapi/operation.rb', line 160 def run_ur(*a, &b) build_request(*a, &b).run_ur end |
#uri_template(base_url: self.base_url) ⇒ Addressable::Template
Returns the URI template, consisting of the base_url concatenated with the path template.
77 78 79 80 81 82 83 84 |
# File 'lib/scorpio/openapi/operation.rb', line 77 def uri_template(base_url: self.base_url) unless base_url raise(ArgumentError, "no base_url has been specified for operation #{self}") end # we do not use Addressable::URI#join as the paths should just be concatenated, not resolved. # we use File.join just to deal with consecutive slashes. Addressable::Template.new(File.join(base_url, path_template_str)) end |