From f7c8a758a6780fe2832018777ca45a4ff7f3c9b3 Mon Sep 17 00:00:00 2001 From: fatimamohamed Date: Wed, 29 May 2019 10:38:40 -0500 Subject: [PATCH] small fixes --- lib/helpers/helpers.ex | 11 ++++++++ lib/phoenix_swagger.ex | 58 +++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/lib/helpers/helpers.ex b/lib/helpers/helpers.ex index 5c9358b..2665472 100644 --- a/lib/helpers/helpers.ex +++ b/lib/helpers/helpers.ex @@ -9,6 +9,17 @@ defmodule PhoenixSwagger.Helpers do %{swagger_map | paths: paths} end + def swagger_map(swagger_map) do + Map.update(swagger_map, :definitions, %{}, &(&1)) + |> Map.update(:paths, %{}, &(&1)) + end + + def extract_args(action) do + [ + %{verb: action |> String.to_atom, path: ""} + ] + end + defp merge_conflicts(_key, value1, value2) do Map.merge(value1, value2) end diff --git a/lib/phoenix_swagger.ex b/lib/phoenix_swagger.ex index ce2095a..523c2e4 100644 --- a/lib/phoenix_swagger.ex +++ b/lib/phoenix_swagger.ex @@ -218,62 +218,50 @@ defmodule PhoenixSwagger do |> add_module(SimpleWeb.UserSocket) """ - def add_module(struct, module) do + def add_module(swagger_map, module) do functions = module.__info__(:functions) - paths = get_paths(functions, struct, module) + swagger_map + |> get_paths(module, functions) + |> get_schemas(module, functions) + end + defp get_schemas(swagger_map, module, functions) do Enum.map(functions, fn {action, _arg} -> build_schemas(action, module) end) |> Enum.filter(&!is_nil(&1)) - |> Enum.reduce(swagger_map(paths), &Helpers.merge_definitions/2) + |> Enum.reduce(Helpers.swagger_map(swagger_map), &Helpers.merge_definitions/2) end - defp build_schemas(function, module) do if is_schema?(function), do: apply(module, function, []) end - defp is_schema?(function) do function |> Atom.to_string |> String.contains?("swagger_definitions") end - defp get_paths(functions, struct, module) do + defp get_paths(swagger_map, module, functions) do Enum.map(functions, fn {action, _arg} -> build_path(action, module) end) |> Enum.filter(&!is_nil(&1)) - |> Enum.reduce(swagger_map(struct), &Helpers.merge_paths/2) + |> Enum.reduce(Helpers.swagger_map(swagger_map), &Helpers.merge_paths/2) end - - defp swagger_map(swagger_map) do - Map.update(swagger_map, :definitions, %{}, &(&1)) - |> Map.update(:paths, %{}, &(&1)) - end - - defp build_path(function, module) do #room for improvement here for sure, comments? - case is_path?(function) do - {true, action} -> - apply(module, function, extract_args(action)) - {false, _action} -> - nil + defp build_path(function, module) do + if is_path?(function) do + action = + function + |> get_action + apply(module, function, Helpers.extract_args(action)) end end - - defp is_path?(function) do #room for improvement here for sure, comments? - funct_string = - function - |> Atom.to_string - - contains = - funct_string - |> String.contains?("swagger_path") - - {contains, String.replace(funct_string, "swagger_path_", "")} + defp is_path?(function) do + function + |> Atom.to_string + |> String.contains?("swagger_path") end - - defp extract_args(action) do - [ - %{verb: action |> String.to_atom, path: ""} - ] + defp get_action(function) do + function + |> Atom.to_string + |> String.replace("swagger_path_", "") end @doc false