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



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

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
# File 'lib/generator/operation.rb', line 26

def description
  description = operation["description"]

  # Remove usage plan details
  lines = description.split("\n")
  usage_plan_index = lines.find_index { |line| line.downcase.include?("usage plan") }
  lines = lines[0...usage_plan_index] if usage_plan_index
  lines.reject! { |line| line.strip == "" }
  description = lines.join("\n").strip

  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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generator/operation.rb', line 70

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



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

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

#renderObject



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

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

#request_argsObject



106
107
108
109
110
111
112
# File 'lib/generator/operation.rb', line 106

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

  args
end

#sandbox_ruleObject



87
88
89
90
91
92
93
# File 'lib/generator/operation.rb', line 87

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

#tagsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generator/operation.rb', line 42

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 [Hash] 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