Class: Wrappix::Templates::Documentation

Inherits:
Object
  • Object
show all
Defined in:
lib/wrappix/templates/documentation.rb

Class Method Summary collapse

Class Method Details

.param_description(param) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/wrappix/templates/documentation.rb', line 163

def self.param_description(param)
  case param
  when "id"
    "The unique identifier of the resource."
  when "customer_id"
    "The identifier of the customer."
  when /^(\w+)_id$/
    "The identifier of the #{::Regexp.last_match(1)}."
  else
    "Description not available."
  end
end

.render(_api_name, module_name, config) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wrappix/templates/documentation.rb', line 6

def self.render(_api_name, module_name, config)
  base_url = config["base_url"] || "https://api.example.com"
  resources = config["resources"] || {}

  # Cabecera y descripción general
  doc = "    # \#{module_name} API Documentation\n\n    This document provides detailed information about the endpoints available in the \#{module_name} API client.\n\n    **API Base URL:** `\#{base_url}`\n\n    ## Table of Contents\n\n    - [Authentication](#authentication)\n    \#{resources.keys.map { |r| \"- [\#{r.capitalize}](#\#{r})\" }.join(\"\\n  \")}\n\n    ## Authentication\n\n    \#{render_authentication_docs(config)}\n\n    ## Resources\n\n  MARKDOWN\n\n  # A\u00F1adir documentaci\u00F3n para cada recurso\n  resources.each do |resource_name, resource_config|\n    doc += render_resource_docs(resource_name, resource_config, module_name, base_url)\n  end\n\n  doc\nend\n"

.render_authentication_docs(config) ⇒ Object



39
40
41
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
69
70
71
72
73
74
75
76
77
# File 'lib/wrappix/templates/documentation.rb', line 39

def self.render_authentication_docs(config)
  case config["auth_type"]
  when "oauth"
    "      This API uses OAuth 2.0 authentication. You need to obtain an access token from the authorization server.\n\n      ```ruby\n      \#{config[\"api_name\"].gsub(\"-\", \"_\").capitalize}.configure do |config|\n        config.client_id = \"YOUR_CLIENT_ID\"\n        config.client_secret = \"YOUR_CLIENT_SECRET\"\n      end\n      ```\n    MARKDOWN\n  when \"basic\"\n    <<~MARKDOWN\n      This API uses HTTP Basic Authentication.\n\n      ```ruby\n      \#{config[\"api_name\"].gsub(\"-\", \"_\").capitalize}.configure do |config|\n        config.username = \"YOUR_USERNAME\"\n        config.password = \"YOUR_PASSWORD\"\n      end\n      ```\n    MARKDOWN\n  when \"api_key\"\n    header = config[\"api_key_header\"] || \"X-Api-Key\"\n    <<~MARKDOWN\n      This API uses API Key authentication. The key should be provided in the `\#{header}` header.\n\n      ```ruby\n      \#{config[\"api_name\"].gsub(\"-\", \"_\").capitalize}.configure do |config|\n        config.api_key = \"YOUR_API_KEY\"\n      end\n      ```\n    MARKDOWN\n  else\n    \"This API does not require authentication.\"\n  end\nend\n"

.render_endpoint_docs(endpoint, resource_name, singular_name, _module_name, base_url) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/wrappix/templates/documentation.rb', line 97

def self.render_endpoint_docs(endpoint, resource_name, singular_name, _module_name, base_url)
  name = endpoint["name"]
  method = endpoint["method"]&.upcase || "GET"
  path = endpoint["path"] || name

  path_params = path.scan(/\{([^}]+)\}/).flatten

  full_url = "#{base_url.chomp("/")}/#{path}"

  method_params = []
  method_params.concat(path_params)
  method_params << "params" if endpoint["params"]
  method_params << "body" if %w[POST PUT PATCH].include?(method)

  client_call = "client.#{resource_name}.#{name}(#{method_params.join(", ")})"

  doc = "\n    ### \#{name}\n\n    **\#{method}** `\#{full_url}`\n\n    \#{endpoint[\"description\"] || \"No description provided.\"}\n\n    #### Parameters\n  MARKDOWN\n\n  # A\u00F1adir documentaci\u00F3n de par\u00E1metros\n  if path_params.empty? && !endpoint[\"params\"] && !%w[POST PUT PATCH].include?(method)\n    doc += \"\\nThis endpoint does not require any parameters.\\n\"\n  else\n    if path_params.any?\n      doc += \"\\n**Path Parameters:**\\n\\n\"\n      path_params.each do |param|\n        doc += \"- `\#{param}`: Required. \#{param_description(param)}\\n\"\n      end\n    end\n\n    if endpoint[\"params\"]\n      doc += \"\\n**Query Parameters:**\\n\\n\"\n      doc += \"- This endpoint accepts additional query parameters.\\n\"\n    end\n\n    if %w[POST PUT PATCH].include?(method)\n      doc += \"\\n**Request Body:**\\n\\n\"\n      doc += \"- This endpoint accepts a request body with the resource attributes.\\n\"\n    end\n  end\n\n  # A\u00F1adir ejemplos de uso\n  doc += <<~MARKDOWN\n\n    #### Example Usage\n\n    ```ruby\n    \#{client_call}\n    ```\n\n    #### Response\n\n    \#{response_example(name, singular_name, endpoint[\"collection\"])}\n  MARKDOWN\n\n  doc\nend\n"

.render_resource_docs(resource_name, resource_config, module_name, base_url) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wrappix/templates/documentation.rb', line 79

def self.render_resource_docs(resource_name, resource_config, module_name, base_url)
  endpoints = resource_config["endpoints"] || []
  singular_name = resource_name.end_with?("s") ? resource_name.chop : resource_name

  doc = "\n    <a name=\"\#{resource_name}\"></a>\n    ## \#{resource_name.capitalize}\n\n  MARKDOWN\n\n  endpoints.each do |endpoint|\n    doc += render_endpoint_docs(endpoint, resource_name, singular_name, module_name, base_url)\n  end\n\n  doc\nend\n"

.response_example(name, resource_name, _is_collection) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/wrappix/templates/documentation.rb', line 176

def self.response_example(name, resource_name, _is_collection)
  case name
  when "list", "all", "index", "search"
    "      ```ruby\n      # Returns a Collection object\n      collection.data.each do |\#{resource_name}|\n        puts \#{resource_name}.id\n        puts \#{resource_name}.name\n        # Other attributes...\n      end\n\n      # Pagination information\n      puts collection.next_href  # URL for the next page, if available\n      ```\n    MARKDOWN\n  when \"get\", \"find\", \"show\"\n    <<~MARKDOWN\n      ```ruby\n      # Returns a single Object\n      puts \#{resource_name}.id\n      puts \#{resource_name}.name\n      # Other attributes...\n      ```\n    MARKDOWN\n  when \"create\"\n    <<~MARKDOWN\n      ```ruby\n      # Returns the created object\n      puts \#{resource_name}.id\n      puts \#{resource_name}.created_at\n      # Other attributes...\n      ```\n    MARKDOWN\n  when \"update\"\n    <<~MARKDOWN\n      ```ruby\n      # Returns the updated object\n      puts \#{resource_name}.id\n      puts \#{resource_name}.updated_at\n      # Other attributes...\n      ```\n    MARKDOWN\n  when \"delete\", \"destroy\", \"remove\"\n    <<~MARKDOWN\n      ```ruby\n      # Returns a success indicator or the deleted object\n      puts \"Resource deleted successfully\"\n      ```\n    MARKDOWN\n  else\n    <<~MARKDOWN\n      ```ruby\n      # Returns a response specific to this endpoint\n      puts response  # Examine the response structure\n      ```\n    MARKDOWN\n  end\nend\n"