Skip to content

Commit

Permalink
Merge pull request #46 from scott-wyatt/master
Browse files Browse the repository at this point in the history
[feat] update deps, add formatter, start units
  • Loading branch information
OpakAlex authored Jan 4, 2019
2 parents 2c77082 + 0892e6f commit 3ae604d
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 93 deletions.
6 changes: 6 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
# files to format
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"],

line_length: 140
]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
erl_crash.dump
*.ez
/doc
.idea
bureaucrat.iml
examples/swagger_demo/swagger_demo.iml
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ First, add Bureaucrat to your `mix.exs` dependencies:

```elixir
defp deps do
[{:bureaucrat, "~> 0.2.4"}]
[{:bureaucrat, "~> 0.2.5"}]
end
```

Expand All @@ -21,7 +21,7 @@ Bureaucrat needs a json library and defaults to Poison. It must be added as a de
```elixir
defp deps do
[
{:bureaucrat, "~> 0.2.4"},
{:bureaucrat, "~> 0.2.5"},
{:poison, "~> 3.0"}
]
end
Expand Down
2 changes: 1 addition & 1 deletion examples/swagger_demo/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule SwaggerDemo.Mixfile do
def project do
[app: :swagger_demo,
version: "0.0.1",
elixir: "~> 1.2",
elixir: "~> 1.6 or ~> 1.7",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
Expand Down
1 change: 1 addition & 0 deletions examples/swagger_demo/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"decimal": {:hex, :decimal, "1.3.1", "157b3cedb2bfcb5359372a7766dd7a41091ad34578296e951f58a946fcab49c6", [:mix], []},
"ecto": {:hex, :ecto, "2.1.3", "ffb24e150b519a4c0e4c84f9eabc8587199389bc499195d5d1a93cd3b2d9a045", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
"gettext": {:hex, :gettext, "0.14.0", "1a019a2e51d5ad3d126efe166dcdf6563768e5d06c32a99ad2281a1fa94b4c72", [:mix], [], "hexpm"},
"inflex": {:hex, :inflex, "1.10.0", "8366a7696e70e1813aca102e61274addf85d99f4a072b2f9c7984054ea1b9d29", [], [], "hexpm"},
"mime": {:hex, :mime, "1.2.0", "78adaa84832b3680de06f88f0997e3ead3b451a440d183d688085be2d709b534", [:mix], [], "hexpm"},
"phoenix": {:hex, :phoenix, "1.3.0", "1c01124caa1b4a7af46f2050ff11b267baa3edb441b45dbf243e979cd4c5891b", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_ecto": {:hex, :phoenix_ecto, "3.3.0", "702f6e164512853d29f9d20763493f2b3bcfcb44f118af2bc37bb95d0801b480", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
6 changes: 3 additions & 3 deletions lib/bureaucrat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Bureaucrat do
import Supervisor.Spec

children = [
worker(Bureaucrat.Recorder, []),
worker(Bureaucrat.Recorder, [])
]

opts = [strategy: :one_for_one, name: Bureaucrat.Supervisor]
Expand All @@ -19,8 +19,8 @@ defmodule Bureaucrat do
end

defp configure(options) do
Enum.each options, fn {k, v} ->
Enum.each(options, fn {k, v} ->
Application.put_env(:bureaucrat, k, v)
end
end)
end
end
7 changes: 5 additions & 2 deletions lib/bureaucrat/api_blueprint_writer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule Bureaucrat.ApiBlueprintWriter do
file
|> puts("\n\n+ Request #{record.assigns.bureaucrat_desc}")
|> puts("**#{record.method}**  `#{path}`\n")

write_headers(record.req_headers, file)
write_request_body(record.body_params, file)
end
Expand All @@ -89,7 +90,9 @@ defmodule Bureaucrat.ApiBlueprintWriter do

defp write_request_body(params, file) do
case params == %{} do
true -> nil
true ->
nil

false ->
file
|> puts(indent_lines(4, "+ Body\n"))
Expand Down Expand Up @@ -176,6 +179,6 @@ defmodule Bureaucrat.ApiBlueprintWriter do
|> Enum.map(fn {controller_name, records} ->
{controller_name, Enum.group_by(records, & &1.private.phoenix_action)}
end)
|> Enum.sort
|> Enum.sort()
end
end
1 change: 1 addition & 0 deletions lib/bureaucrat/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule Bureaucrat.Formatter do

defp path_for({_, _}, _, default_path), do: default_path
defp path_for(_record, [], default_path), do: default_path

defp path_for(record, [{prefix, path} | paths], default_path) do
module = record.private.phoenix_controller

Expand Down
15 changes: 9 additions & 6 deletions lib/bureaucrat/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Bureaucrat.Helpers do
ref
end
end

defmacro doc_push(socket, event, payload) do
quote bind_quoted: [socket: socket, event: event, payload: payload] do
ref = make_ref()
Expand All @@ -41,7 +42,7 @@ defmodule Bureaucrat.Helpers do
%{pubsub_server: pubsub_server, topic: topic, transport_pid: transport_pid} = socket
broadcast = %Broadcast{topic: topic, event: event, payload: message}
doc(broadcast, [])
Phoenix.Channel.Server.broadcast_from pubsub_server, transport_pid, topic, event, message
Phoenix.Channel.Server.broadcast_from(pubsub_server, transport_pid, topic, event, message)
end
end

Expand All @@ -50,7 +51,7 @@ defmodule Bureaucrat.Helpers do
%{pubsub_server: pubsub_server, topic: topic, transport_pid: transport_pid} = socket
broadcast = %Broadcast{topic: topic, event: event, payload: message}
doc(broadcast, [])
Phoenix.Channel.Server.broadcast_from! pubsub_server, transport_pid, topic, event, message
Phoenix.Channel.Server.broadcast_from!(pubsub_server, transport_pid, topic, event, message)
end
end

Expand All @@ -69,7 +70,7 @@ defmodule Bureaucrat.Helpers do
|> get("/api/v1/products")
|> doc(description: "List all products", operation_id: "list_products")
"""
defmacro doc(conn, desc) when is_binary(desc) do
defmacro doc(conn, desc) when is_binary(desc) do
quote bind_quoted: [conn: conn, desc: desc] do
doc(conn, description: desc)
end
Expand All @@ -78,9 +79,10 @@ defmodule Bureaucrat.Helpers do
defmacro doc(conn, opts) when is_list(opts) do
# __CALLER__returns a `Macro.Env` struct
# -> https://hexdocs.pm/elixir/Macro.Env.html
mod = __CALLER__.module
fun = __CALLER__.function |> elem(0) |> to_string
file = __CALLER__.file # full path as binary
mod = __CALLER__.module
fun = __CALLER__.function |> elem(0) |> to_string
# full path as binary
file = __CALLER__.file
line = __CALLER__.line

titles = Application.get_env(:bureaucrat, :titles)
Expand All @@ -102,6 +104,7 @@ defmodule Bureaucrat.Helpers do
def format_test_name("test " <> name), do: name

def group_title_for(_mod, []), do: nil

def group_title_for(mod, [{other, path} | paths]) do
if String.replace_suffix(to_string(mod), "Test", "") == to_string(other) do
path
Expand Down
44 changes: 28 additions & 16 deletions lib/bureaucrat/markdown_writer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ defmodule Bureaucrat.MarkdownWriter do
alias Bureaucrat.JSON

def write(records, path) do
{:ok, file} = File.open path, [:write, :utf8]
{:ok, file} = File.open(path, [:write, :utf8])
records = group_records(records)
write_intro(path, file)
write_table_of_contents(records, file)

Enum.each(records, fn {controller, records} ->
write_controller(controller, records, file)
end)
end

defp write_intro(path, file) do
intro_file_path = [
String.replace(path, ~r/\.md$/i, "_INTRO\\0"), # /path/to/API.md -> /path/to/API_INTRO.md
String.replace(path, ~r/\.md$/i, "_intro\\0"), # /path/to/api.md -> /path/to/api_intro.md
"#{path}_INTRO", # /path/to/API -> /path/to/API_INTRO
"#{path}_intro", # /path/to/api -> /path/to/api_intro
] |> Enum.find(nil, &File.exists?/1) # which one exists?
intro_file_path =
[
# /path/to/API.md -> /path/to/API_INTRO.md
String.replace(path, ~r/\.md$/i, "_INTRO\\0"),
# /path/to/api.md -> /path/to/api_intro.md
String.replace(path, ~r/\.md$/i, "_intro\\0"),
# /path/to/API -> /path/to/API_INTRO
"#{path}_INTRO",
# /path/to/api -> /path/to/api_intro
"#{path}_intro"
]
# which one exists?
|> Enum.find(nil, &File.exists?/1)

if intro_file_path do
file
Expand All @@ -32,16 +40,19 @@ defmodule Bureaucrat.MarkdownWriter do
Enum.each(records, fn {controller, actions} ->
anchor = to_anchor(controller)
puts(file, " * [#{controller}](##{anchor})")

Enum.each(actions, fn {action, _} ->
anchor = to_anchor(controller, action)
puts(file, " * [#{action}](##{anchor})")
end)
end)

puts(file, "")
end

defp write_controller(controller, records, file) do
puts(file, "## #{controller}")

Enum.each(records, fn {action, records} ->
write_action(action, controller, records, file)
end)
Expand All @@ -50,7 +61,7 @@ defmodule Bureaucrat.MarkdownWriter do
defp write_action(action, controller, records, file) do
anchor = to_anchor(controller, action)
puts(file, "### <a id=#{anchor}></a>#{action}")
Enum.each(records, &(write_example(&1, file)))
Enum.each(records, &write_example(&1, file))
end

defp write_example({%Phoenix.Socket.Broadcast{topic: topic, payload: payload, event: event}, _}, file) do
Expand Down Expand Up @@ -130,9 +141,9 @@ defmodule Bureaucrat.MarkdownWriter do
|> puts("* __Request headers:__")
|> puts("```")

Enum.each record.req_headers, fn({header, value}) ->
puts file, "#{header}: #{value}"
end
Enum.each(record.req_headers, fn {header, value} ->
puts(file, "#{header}: #{value}")
end)

file
|> puts("```")
Expand All @@ -151,15 +162,14 @@ defmodule Bureaucrat.MarkdownWriter do
|> puts("##### Response")
|> puts("* __Status__: #{record.status}")


unless record.resp_headers == [] do
file
|> puts("* __Response headers:__")
|> puts("```")

Enum.each record.resp_headers, fn({header, value}) ->
puts file, "#{header}: #{value}"
end
Enum.each(record.resp_headers, fn {header, value} ->
puts(file, "#{header}: #{value}")
end)

file
|> puts("```")
Expand Down Expand Up @@ -201,16 +211,18 @@ defmodule Bureaucrat.MarkdownWriter do
end

defp to_anchor(controller, action), do: to_anchor("#{controller}.#{action}")

defp to_anchor(name) do
name
|> String.downcase
|> String.downcase()
|> String.replace(~r/\W+/, "-")
|> String.replace_leading("-", "")
|> String.replace_trailing("-", "")
end

defp group_records(records) do
by_controller = Bureaucrat.Util.stable_group_by(records, &get_controller/1)

Enum.map(by_controller, fn {c, recs} ->
{c, Bureaucrat.Util.stable_group_by(recs, &get_action/1)}
end)
Expand Down
Loading

0 comments on commit 3ae604d

Please sign in to comment.