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 = <<~MARKDOWN
    # #{module_name} API Documentation

    This document provides detailed information about the endpoints available in the #{module_name} API client.

    **API Base URL:** `#{base_url}`

    ## Table of Contents

    - [Authentication](#authentication)
    #{resources.keys.map { |r| "- [#{r.capitalize}](##{r})" }.join("\n  ")}

    ## Authentication

    #{render_authentication_docs(config)}

    ## Resources

  MARKDOWN

  # Añadir documentación para cada recurso
  resources.each do |resource_name, resource_config|
    doc += render_resource_docs(resource_name, resource_config, module_name, base_url)
  end

  doc
end

.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"
    <<~MARKDOWN
      This API uses OAuth 2.0 authentication. You need to obtain an access token from the authorization server.

      ```ruby
      #{config["api_name"].gsub("-", "_").capitalize}.configure do |config|
        config.client_id = "YOUR_CLIENT_ID"
        config.client_secret = "YOUR_CLIENT_SECRET"
      end
      ```
    MARKDOWN
  when "basic"
    <<~MARKDOWN
      This API uses HTTP Basic Authentication.

      ```ruby
      #{config["api_name"].gsub("-", "_").capitalize}.configure do |config|
        config.username = "YOUR_USERNAME"
        config.password = "YOUR_PASSWORD"
      end
      ```
    MARKDOWN
  when "api_key"
    header = config["api_key_header"] || "X-Api-Key"
    <<~MARKDOWN
      This API uses API Key authentication. The key should be provided in the `#{header}` header.

      ```ruby
      #{config["api_name"].gsub("-", "_").capitalize}.configure do |config|
        config.api_key = "YOUR_API_KEY"
      end
      ```
    MARKDOWN
  else
    "This API does not require authentication."
  end
end

.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 = <<~MARKDOWN

    ### #{name}

    **#{method}** `#{full_url}`

    #{endpoint["description"] || "No description provided."}

    #### Parameters
  MARKDOWN

  # Añadir documentación de parámetros
  if path_params.empty? && !endpoint["params"] && !%w[POST PUT PATCH].include?(method)
    doc += "\nThis endpoint does not require any parameters.\n"
  else
    if path_params.any?
      doc += "\n**Path Parameters:**\n\n"
      path_params.each do |param|
        doc += "- `#{param}`: Required. #{param_description(param)}\n"
      end
    end

    if endpoint["params"]
      doc += "\n**Query Parameters:**\n\n"
      doc += "- This endpoint accepts additional query parameters.\n"
    end

    if %w[POST PUT PATCH].include?(method)
      doc += "\n**Request Body:**\n\n"
      doc += "- This endpoint accepts a request body with the resource attributes.\n"
    end
  end

  # Añadir ejemplos de uso
  doc += <<~MARKDOWN

    #### Example Usage

    ```ruby
    #{client_call}
    ```

    #### Response

    #{response_example(name, singular_name, endpoint["collection"])}
  MARKDOWN

  doc
end

.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 = <<~MARKDOWN

    <a name="#{resource_name}"></a>
    ## #{resource_name.capitalize}

  MARKDOWN

  endpoints.each do |endpoint|
    doc += render_endpoint_docs(endpoint, resource_name, singular_name, module_name, base_url)
  end

  doc
end

.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"
    <<~MARKDOWN
      ```ruby
      # Returns a Collection object
      collection.data.each do |#{resource_name}|
        puts #{resource_name}.id
        puts #{resource_name}.name
        # Other attributes...
      end

      # Pagination information
      puts collection.next_href  # URL for the next page, if available
      ```
    MARKDOWN
  when "get", "find", "show"
    <<~MARKDOWN
      ```ruby
      # Returns a single Object
      puts #{resource_name}.id
      puts #{resource_name}.name
      # Other attributes...
      ```
    MARKDOWN
  when "create"
    <<~MARKDOWN
      ```ruby
      # Returns the created object
      puts #{resource_name}.id
      puts #{resource_name}.created_at
      # Other attributes...
      ```
    MARKDOWN
  when "update"
    <<~MARKDOWN
      ```ruby
      # Returns the updated object
      puts #{resource_name}.id
      puts #{resource_name}.updated_at
      # Other attributes...
      ```
    MARKDOWN
  when "delete", "destroy", "remove"
    <<~MARKDOWN
      ```ruby
      # Returns a success indicator or the deleted object
      puts "Resource deleted successfully"
      ```
    MARKDOWN
  else
    <<~MARKDOWN
      ```ruby
      # Returns a response specific to this endpoint
      puts response  # Examine the response structure
      ```
    MARKDOWN
  end
end