Class: Generator::Operation
- Inherits:
-
Object
- Object
- Generator::Operation
show all
- Includes:
- Formatter
- Defined in:
- lib/generator/operation.rb
Constant Summary
collapse
- DEFAULT_PAYLOAD_KEY =
"payload"
Constants included
from Formatter
Formatter::MAX_LINE_LENGTH
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Formatter
#convert_doc_links_to_full_url, #convert_html_links_to_yard, #format_method_definition, #split_long_comment_line
Constructor Details
#initialize(path, verb, operation, api_name_with_version = nil, specification = nil) ⇒ Operation
Returns a new instance of Operation.
16
17
18
19
20
21
22
|
# File 'lib/generator/operation.rb', line 16
def initialize(path, verb, operation, api_name_with_version = nil, specification = nil)
@path = path
@verb = verb
@operation = operation
@api_name_with_version = api_name_with_version
@specification = specification
end
|
Instance Attribute Details
#api_name_with_version ⇒ Object
Returns the value of attribute api_name_with_version.
12
13
14
|
# File 'lib/generator/operation.rb', line 12
def api_name_with_version
@api_name_with_version
end
|
#operation ⇒ Object
Returns the value of attribute operation.
12
13
14
|
# File 'lib/generator/operation.rb', line 12
def operation
@operation
end
|
#path ⇒ Object
Returns the value of attribute path.
12
13
14
|
# File 'lib/generator/operation.rb', line 12
def path
@path
end
|
#specification ⇒ Object
Returns the value of attribute specification.
12
13
14
|
# File 'lib/generator/operation.rb', line 12
def specification
@specification
end
|
#verb ⇒ Object
Returns the value of attribute verb.
12
13
14
|
# File 'lib/generator/operation.rb', line 12
def verb
@verb
end
|
Instance Method Details
#body_param_name ⇒ Object
106
107
108
109
|
# File 'lib/generator/operation.rb', line 106
def body_param_name
body_param = parameters.find { |p| p["in"] == "body" }
body_param["name"].underscore if body_param
end
|
#description ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/generator/operation.rb', line 32
def description
description = operation["description"]
delimiter = description.include?("\\n") ? "\\n" : "\n"
lines = description.split(delimiter)
usage_plan_index = lines.find_index { |line| line.downcase.include?("usage plan") }
lines = lines[0...usage_plan_index] if usage_plan_index
description = lines.join("\n").strip
description = description.gsub(/(\S)\s*(\*\*(Note|Examples?):\*\*)/, "\\1\n\n\\2")
description = convert_html_links_to_yard(description)
description = convert_doc_links_to_full_url(description)
(description, base_indent: 6)
end
|
#has_typed_response? ⇒ Boolean
135
136
137
|
# File 'lib/generator/operation.rb', line 135
def has_typed_response?
!!operation_id && response_model[:model]
end
|
#method_definition ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/generator/operation.rb', line 81
def method_definition
method_name = operation["operationId"].underscore
required_params = parameters.select { |p| p["required"] }&.map { |p| p["name"].underscore } || []
optional_params = parameters.reject do |p|
p["required"]
end.map do |p|
default_value = p["default"]
formatted_default = default_value.is_a?(String) ? "\"#{default_value}\"" : default_value
"#{p["name"].underscore}: #{formatted_default ? formatted_default : "nil"}"
end
params = required_params + optional_params
format_method_definition(method_name, params, base_indent: 6)
end
|
#operation_id ⇒ Object
24
25
26
|
# File 'lib/generator/operation.rb', line 24
def operation_id
operation["operationId"]
end
|
#query_params ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/generator/operation.rb', line 111
def query_params
parameters.select do |p|
p["in"] == "query"
end.reduce({}) do |hash, p|
param_name = p["name"].underscore
value = param_name
if p["type"] == "array"
value = "stringify_array(#{param_name})"
end
hash.merge(p["name"] => value)
end
end
|
#render ⇒ Object
28
29
30
|
# File 'lib/generator/operation.rb', line 28
def render
ERB.new(template, trim_mode: "-").result(binding)
end
|
#request_args ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/generator/operation.rb', line 127
def request_args
args = ["path"]
args << "body:" if body_param_name
args << "params:" if query_params.any?
args
end
|
#response_model ⇒ Object
139
140
141
|
# File 'lib/generator/operation.rb', line 139
def response_model
@response_model ||= build_response_model
end
|
#sandbox_rule ⇒ Object
98
99
100
101
102
103
104
|
# File 'lib/generator/operation.rb', line 98
def sandbox_rule
if !static_sandbox? && !dynamic_sandbox?
"cannot_sandbox!\n\n"
elsif path.sandbox_only?
"must_sandbox!\n\n"
end
end
|
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/generator/operation.rb', line 53
def tags
output = parameters.map do |param|
param_type = param["type"] ? param["type"].capitalize : "Object"
param_type = "Hash" if param["schema"]
if param_type == "Array"
items_type = param.dig("items", "type")
param_type = items_type ? "Array<#{items_type.capitalize}>" : "Array"
end
param_name = param["name"].underscore
param_description = param["description"]&.gsub(/\s+/, " ")
"@param #{param_name} [#{param_type}] #{param_description}"
end
if static_sandbox?
output.unshift("@note This operation can make a static sandbox call.")
elsif dynamic_sandbox?
output.unshift("@note This operation can make a dynamic sandbox call.")
end
output << "@return [Peddler::Response] The API response"
output.map do |line|
line = convert_html_links_to_yard(line)
line = convert_doc_links_to_full_url(line)
(line, base_indent: 6, wrap_indent: 2)
end
end
|