Class: Generator::Operation

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

Constant Summary

Constants included from Formatter

Formatter::MAX_LINE_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#pascalcase, #snakecase

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) ⇒ Operation

Returns a new instance of Operation.



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

def initialize(path, verb, operation)
  @path = path
  @verb = verb
  @operation = operation
end

Instance Attribute Details

#operationObject (readonly)

Returns the value of attribute operation.



14
15
16
# File 'lib/generator/operation.rb', line 14

def operation
  @operation
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/generator/operation.rb', line 14

def path
  @path
end

#verbObject (readonly)

Returns the value of attribute verb.



14
15
16
# File 'lib/generator/operation.rb', line 14

def verb
  @verb
end

Instance Method Details

#body_param_nameObject



100
101
102
103
# File 'lib/generator/operation.rb', line 100

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

#descriptionObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generator/operation.rb', line 26

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
  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

#method_definitionObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/generator/operation.rb', line 75

def method_definition
  method_name = snakecase(operation.delete("operationId"))

  required_params = parameters.select { |p| p["required"] }&.map { |p| snakecase(p["name"]) } || []
  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

    "#{snakecase(p["name"])}: #{formatted_default ? formatted_default : "nil"}"
  end
  params = required_params + optional_params

  format_method_definition(method_name, params, base_indent: 6)
end

#query_paramsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/generator/operation.rb', line 105

def query_params
  parameters.select do |p|
    p["in"] == "query"
  end.reduce({}) do |hash, p|
    param_name = snakecase(p["name"])
    value = param_name

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

    hash.merge(p["name"] => value)
  end
end

#renderObject



22
23
24
# File 'lib/generator/operation.rb', line 22

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

#request_argsObject



121
122
123
124
125
126
127
# File 'lib/generator/operation.rb', line 121

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

  args
end

#sandbox_ruleObject



92
93
94
95
96
97
98
# File 'lib/generator/operation.rb', line 92

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

#tagsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/generator/operation.rb', line 47

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 = snakecase(param["name"])
    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)
    split_long_comment_line(line, base_indent: 6, wrap_indent: 2)
  end
end