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

Instance Method Details

#build_request(*a, &b) ⇒ Scorpio::Request

Parameters:

  • a,

    b are passed to Scorpio::Request#initialize

Returns:



162
163
164
# File 'lib/scorpio/openapi/operation.rb', line 162

def build_request(*a, &b)
  Scorpio::Request.new(self, *a, &b)
end

#http_methodObject

Returns the HTTP method of this operation as indicated by the attribute name for this operation from the parent PathItem.

Returns:

  • the HTTP method of this operation as indicated by the attribute name for this operation from the parent PathItem



92
93
94
95
96
97
98
99
100
# File 'lib/scorpio/openapi/operation.rb', line 92

def http_method
  return @http_method if instance_variable_defined?(:@http_method)
  @http_method = begin
    parent_is_pathitem = parent.is_a?(Scorpio::OpenAPI::V2::PathItem) || parent.is_a?(Scorpio::OpenAPI::V3::PathItem)
    if parent_is_pathitem
      instance.path.last
    end
  end
end

#human_idString

Returns a short identifier for this operation appropriate for an error message.

Returns:

  • (String)

    a short identifier for this operation appropriate for an error message



103
104
105
# File 'lib/scorpio/openapi/operation.rb', line 103

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.

Returns:

  • (#to_ary<#to_h>)

    the parameters specified for this operation, plus any others scorpio considers to be parameters



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/scorpio/openapi/operation.rb', line 121

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



108
109
110
111
112
113
114
115
# File 'lib/scorpio/openapi/operation.rb', line 108

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_documentScorpio::OpenAPI::Document

Returns the document whence this operation came.

Returns:



57
58
59
# File 'lib/scorpio/openapi/operation.rb', line 57

def openapi_document
  parents.detect { |p| p.is_a?(Scorpio::OpenAPI::Document) }
end

#path_templateAddressable::Template

Returns the path as an Addressable::Template.

Returns:

  • (Addressable::Template)

    the path as an Addressable::Template



73
74
75
76
# File 'lib/scorpio/openapi/operation.rb', line 73

def path_template
  return @path_template if instance_variable_defined?(:@path_template)
  @path_template = Addressable::Template.new(path_template_str)
end

#path_template_strObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/scorpio/openapi/operation.rb', line 61

def path_template_str
  return @path_template_str if instance_variable_defined?(:@path_template_str)
  @path_template_str = begin
    parent_is_pathitem = parent.is_a?(Scorpio::OpenAPI::V2::PathItem) || parent.is_a?(Scorpio::OpenAPI::V3::PathItem)
    parent_parent_is_paths = parent.parent.is_a?(Scorpio::OpenAPI::V2::Paths) || parent.parent.is_a?(Scorpio::OpenAPI::V3::Paths)
    if parent_is_pathitem && parent_parent_is_paths
      parent.instance.path.last
    end
  end
end

#request_accessor_moduleModule

Returns a module with accessor methods for unambiguously named parameters of this operation.

Returns:

  • (Module)

    a module with accessor methods for unambiguously named parameters of this operation.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/scorpio/openapi/operation.rb', line 139

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.

Parameters:

  • a,

    b are passed to Scorpio::Request#initialize

Returns:

  • response body object



174
175
176
# File 'lib/scorpio/openapi/operation.rb', line 174

def run(*a, &b)
  build_request(*a, &b).run
end

#run_ur(*a, &b) ⇒ Scorpio::Ur

Returns response ur.

Parameters:

  • a,

    b are passed to Scorpio::Request#initialize

Returns:



168
169
170
# File 'lib/scorpio/openapi/operation.rb', line 168

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.

Parameters:

  • base_url (#to_str) (defaults to: self.base_url)

    the base URL to which the path template is appended

Returns:

  • (Addressable::Template)

    the URI template, consisting of the base_url concatenated with the path template



81
82
83
84
85
86
87
88
# File 'lib/scorpio/openapi/operation.rb', line 81

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

#v2?Boolean

Returns v2?.

Returns:

  • (Boolean)

    v2?



52
53
54
# File 'lib/scorpio/openapi/operation.rb', line 52

def v2?
  is_a?(V2::Operation)
end

#v3?Boolean

Returns v3?.

Returns:

  • (Boolean)

    v3?



47
48
49
# File 'lib/scorpio/openapi/operation.rb', line 47

def v3?
  is_a?(V3::Operation)
end