Class: Generator::Operation

Inherits:
Object
  • Object
show all
Includes:
Formatter
Defined in:
lib/generator/builders/operation.rb

Constant Summary collapse

DEFAULT_TEMPLATE =
File.read(Config.template_path("operation"))

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, template: DEFAULT_TEMPLATE) ⇒ Operation

Returns a new instance of Operation.



20
21
22
23
24
25
26
# File 'lib/generator/builders/operation.rb', line 20

def initialize(path, verb, operation, api_name_with_version, template: DEFAULT_TEMPLATE)
  @path = path
  @verb = verb
  @operation = operation
  @api_name_with_version = api_name_with_version
  @template = template
end

Instance Attribute Details

#api_name_with_version ⇒ Object (readonly)

Returns the value of attribute api_name_with_version.



18
19
20
# File 'lib/generator/builders/operation.rb', line 18

def api_name_with_version
  @api_name_with_version
end

#operation ⇒ Object (readonly)

Returns the value of attribute operation.



18
19
20
# File 'lib/generator/builders/operation.rb', line 18

def operation
  @operation
end

#path ⇒ Object (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/generator/builders/operation.rb', line 18

def path
  @path
end

#template ⇒ Object (readonly)

Returns the value of attribute template.



18
19
20
# File 'lib/generator/builders/operation.rb', line 18

def template
  @template
end

#verb ⇒ Object (readonly)

Returns the value of attribute verb.



18
19
20
# File 'lib/generator/builders/operation.rb', line 18

def verb
  @verb
end

Instance Method Details

#body_param_name ⇒ Object



124
125
126
127
# File 'lib/generator/builders/operation.rb', line 124

def body_param_name
  body_param = parameters.find { |p| p["in"] == "body" }
  Naming.parameter_name(body_param["name"]) if body_param
end

#description ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generator/builders/operation.rb', line 36

def description
  description = operation["description"]

  # Remove usage plan details
  # Some API models have literal \n strings instead of actual newlines
  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
  # Join lines back together, preserving blank lines for proper paragraph separation
  description = lines.join("\n").strip

  # Ensure **Note:** and **Examples:** start on new lines with blank lines before them
  # Handle both **Note:** and __Note__: formats (will be normalized to ** later)
  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)

  split_long_comment_line(description, base_indent: 6)
end

#has_typed_response? ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/generator/builders/operation.rb', line 153

def has_typed_response?
  !!operation_id && response_model[:model]
end

#method_definition ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/generator/builders/operation.rb', line 89

def method_definition
  method_name = operation["operationId"].underscore

  required_params = parameters.select { |p| p["required"] }&.map { |p| Naming.parameter_name(p["name"]) } || []
  optional_params = parameters.reject do |p|
    p["required"]
  end.map do |p|
    default_value = p["default"]

    # Handle array parameters with string defaults (OpenAPI spec inconsistency)
    formatted_default = if p["type"] == "array" && default_value.is_a?(String)
      "[\"#{default_value}\"]"
    elsif default_value.is_a?(String)
      "\"#{default_value}\""
    elsif default_value.is_a?(Array)
      default_value.inspect
    else
      default_value || "nil"
    end

    "#{Naming.parameter_name(p["name"])}: #{formatted_default}"
  end
  params = required_params + optional_params

  format_method_definition(method_name, params, base_indent: 6)
end

#operation_id ⇒ Object



28
29
30
# File 'lib/generator/builders/operation.rb', line 28

def operation_id
  operation["operationId"]
end

#parameters ⇒ Object



161
162
163
# File 'lib/generator/builders/operation.rb', line 161

def parameters
  @parameters ||= ParameterBuilder.new(operation, path.parameters, rate_limit).build
end

#query_params ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/generator/builders/operation.rb', line 129

def query_params
  hash = {}
  parameters.select { |p| p["in"] == "query" }.each do |p|
    param_name = Naming.parameter_name(p["name"])
    value = param_name

    # If parameter is array type, add array handling
    if p["type"] == "array"
      value = "stringify_array(#{param_name})"
    end

    hash[p["name"]] = value
  end
  hash
end

#rate_limit ⇒ Object



165
166
167
# File 'lib/generator/builders/operation.rb', line 165

def rate_limit
  @rate_limit ||= RateLimitParser.new(operation).parse
end

#render ⇒ Object



32
33
34
# File 'lib/generator/builders/operation.rb', line 32

def render
  ERB.new(template, trim_mode: "-").result(binding)
end

#request_args ⇒ Object



145
146
147
148
149
150
151
# File 'lib/generator/builders/operation.rb', line 145

def request_args
  args = ["path"]
  args << "body:" if body_param_name
  args << "params:" if query_params.any?

  args
end

#response_model ⇒ Object



157
158
159
# File 'lib/generator/builders/operation.rb', line 157

def response_model
  @response_model ||= ResponseModel.new(operation).build
end

#sandbox_rule ⇒ Object



116
117
118
119
120
121
122
# File 'lib/generator/builders/operation.rb', line 116

def sandbox_rule
  if !static_sandbox? && !dynamic_sandbox?
    "cannot_sandbox!\n\n"
  elsif sandbox_only?
    "must_sandbox!\n\n"
  end
end

#tags ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/generator/builders/operation.rb', line 58

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 = Naming.parameter_name(param["name"])
    param_description = param["description"]&.gsub(/\s+/, " ")
    param_description = convert_html_links_to_yard(param_description) if param_description
    param_description = convert_doc_links_to_full_url(param_description) if param_description
    param_description = KnownValues.append(param_description, KnownValues.enum_values(param))

    "@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)
    split_long_comment_line(line, base_indent: 6, wrap_indent: 2)
  end
end