Class: Eazypi::Operation

Inherits:
Object
  • Object
show all
Includes:
SpecObject
Defined in:
lib/eazypi/operation.rb

Overview

OpenAPI spec OperationObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SpecObject

included, #load

Constructor Details

#initialize(controller_klass, controller_method, path, method, &block) ⇒ Operation

Returns a new instance of Operation.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/eazypi/operation.rb', line 15

def initialize(controller_klass, controller_method, path, method, &block)
  @controller_klass = controller_klass
  @controller_method = controller_method
  @path = path
  @method = method
  @parameters = []
  @security = nil
  @tags = nil

  super(&block)
end

Instance Attribute Details

#controller_klassObject (readonly)

Returns the value of attribute controller_klass.



13
14
15
# File 'lib/eazypi/operation.rb', line 13

def controller_klass
  @controller_klass
end

#controller_methodObject (readonly)

Returns the value of attribute controller_method.



13
14
15
# File 'lib/eazypi/operation.rb', line 13

def controller_method
  @controller_method
end

#methodObject (readonly)

Returns the value of attribute method.



13
14
15
# File 'lib/eazypi/operation.rb', line 13

def method
  @method
end

#parametersObject (readonly)

Returns the value of attribute parameters.



13
14
15
# File 'lib/eazypi/operation.rb', line 13

def parameters
  @parameters
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/eazypi/operation.rb', line 13

def path
  @path
end

Class Method Details

.normalized_path(path) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/eazypi/operation.rb', line 93

def self.normalized_path(path)
  normalized_path = path.dup

  path.scan(PATH_REGEX).each do |match|
    normalized_path.gsub!(match[1..], "{#{match[2..]}}")
  end

  normalized_path
end

Instance Method Details

#call(controller) ⇒ Object



69
70
71
72
# File 'lib/eazypi/operation.rb', line 69

def call(controller)
  controller.instance_variable_set(:@current_operation, self)
  controller.instance_exec(&@renderer)
end

#collect_components(**kwargs) ⇒ Object



103
104
105
106
# File 'lib/eazypi/operation.rb', line 103

def collect_components(**kwargs)
  @request_body&.collect_components(**kwargs)
  @responses&.collect_components(**kwargs)
end

#openapi_templated_pathObject



27
28
29
# File 'lib/eazypi/operation.rb', line 27

def openapi_templated_path
  self.class.normalized_path(@path)
end

#parameter(name, location: nil, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/eazypi/operation.rb', line 31

def parameter(name, location: nil, &block)
  if location.nil?
    location = path_parameters.include?(name.to_s) ? "path" : "query"
  end

  @parameters << Parameter.new(name: name, location: location, &block)
end

#render(&block) ⇒ Object



55
56
57
# File 'lib/eazypi/operation.rb', line 55

def render(&block)
  @renderer = block
end

#request_body(&block) ⇒ Object



39
40
41
# File 'lib/eazypi/operation.rb', line 39

def request_body(&block)
  @request_body ||= RequestBody.new(&block)
end

#response(status_code, &block) ⇒ Object



43
44
45
46
47
# File 'lib/eazypi/operation.rb', line 43

def response(status_code, &block)
  @responses ||= Responses.new

  @responses.add_response(status_code, &block)
end

#response_for_response_code(response_code) ⇒ Object



49
50
51
52
53
# File 'lib/eazypi/operation.rb', line 49

def response_for_response_code(response_code)
  response_code = Rack::Utils::SYMBOL_TO_STATUS_CODE[response_code] if response_code.is_a?(Symbol)

  @responses.response_for_response_code(response_code)
end

#security(name, scopes = []) ⇒ Object



63
64
65
66
67
# File 'lib/eazypi/operation.rb', line 63

def security(name, scopes = [])
  @security ||= []

  @security.append(SecurityRequirement.new(name, scopes))
end

#security!Object



59
60
61
# File 'lib/eazypi/operation.rb', line 59

def security!
  @security = []
end

#tag(tag_name) ⇒ Object



74
75
76
77
# File 'lib/eazypi/operation.rb', line 74

def tag(tag_name)
  @tags ||= []
  @tags.append(tag_name)
end

#to_openapi_specObject

rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/eazypi/operation.rb', line 79

def to_openapi_spec # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
  {
    "summary" => summary,
    "description" => description,
    "tags" => @tags,
    "operationId" => operation_id,
    "depcrecated" => depcrecated,
    "parameters" => @parameters.empty? ? nil : @parameters&.map(&:to_openapi_spec),
    "requestBody" => @request_body&.to_openapi_spec,
    "responses" => @responses&.to_openapi_spec,
    "security" => @security&.map(&:to_openapi_spec)
  }.compact
end