1
0
mirror of https://github.com/avitex/elixir-vultr synced 2024-11-21 10:59:57 +00:00

Clean up documentation helpers

This commit is contained in:
avitex 2017-10-13 21:46:24 +11:00
parent a1f08f09f0
commit 3fb6a65ddb

View File

@ -151,53 +151,39 @@ defmodule Vultr.Request do
# Documentation helpers # Documentation helpers
defp gen_doc(method, path, desc, params, required_access, api_key) do defp gen_doc(method, path, desc, params, required_access, api_key) do
summary_table = doc_table([
["Method", "Path", "API Key", "Required Access"],
["------", "----", "-------", "---------------"],
[doc_method(method), path, doc_api_key(api_key), doc_required_access(required_access)],
])
params_rows = Enum.map(params, &[
"`#{String.downcase(&1.name)}`",
&1.type_string,
doc_optional_default(&1.optional, &1.default),
String.replace(&1.desc, "\n", "<br>"),
])
params_table = doc_table([
["Name", "Type", "Optional", "Description"],
["----", "----", "--------", "-----------"],
] ++ params_rows)
""" """
#{desc} #{desc}
#{doc_params(params)} #{summary_table}
### Backend #### Params
- Method: `#{doc_method(method)}` #{params_table}
- Path: `#{path}`
- API Key: `#{doc_api_key(api_key)}`
- Required Access: `#{doc_required_access(required_access)}`
""" """
end end
defp doc_params([]), do: ""
defp doc_params(params) do
param_rows =
params
|> Enum.map(&doc_param/1)
|> Enum.join("")
"""
### Params
| Name | Type | Optional | Description |
| ---- | ---- | -------- | ----------- |
#{param_rows}
"""
end
defp doc_param(param) do
columns = [
"`#{String.downcase(param.name)}`",
param.type_string,
doc_optional_default(param.optional, param.default),
String.replace(param.desc, "\n", "<br>"),
]
"| #{Enum.join(columns, " | ")} |"
end
defp doc_optional_default(optional, default) do defp doc_optional_default(optional, default) do
if optional do cond do
optional = "Yes" optional && is_nil(default) ->
"Yes"
if default == nil do optional ->
optional "<br>(Default `#{inspect default}`)"
else true ->
optional <> "<br>(Default `#{inspect default}`)"
end
else
"No" "No"
end end
end end
@ -210,5 +196,9 @@ defmodule Vultr.Request do
defp doc_required_access(nil), do: "None" defp doc_required_access(nil), do: "None"
defp doc_required_access(atm), do: Atom.to_string(atm) defp doc_required_access(atm), do: Atom.to_string(atm)
defp doc_table(rows), do: Enum.map(rows, &doc_table_columns/1) |> Enum.join
defp doc_table_columns(columns), do: "| #{Enum.join(columns, " | ")} |\n"
defp atom_to_word(atm), do: atm |> Atom.to_string |> String.capitalize defp atom_to_word(atm), do: atm |> Atom.to_string |> String.capitalize
end end