diff --git a/lib/poly_post.ex b/lib/poly_post.ex index 4b07136..fc6cb99 100644 --- a/lib/poly_post.ex +++ b/lib/poly_post.ex @@ -4,7 +4,9 @@ defmodule PolyPost do alias PolyPost.{ Builder, Depots, - Depot + Depot, + Resource, + Util } # API @@ -73,8 +75,29 @@ defmodule PolyPost do |> Enum.each(fn {resource, _} -> Depot.clear(resource) end) end + @doc """ + List all resources and their metadata + """ + @doc since: "0.2.0" + @spec list_resources() :: {:ok, keyword(Resource.config())} + | {:error, :resources_not_found} + | {:error, :config_not_found} + def list_resources do + case Util.get_config() do + {:ok, config} -> get_resources(config) + :error -> {:error, :config_not_found} + end + end + # Private + defp get_resources(config) do + case Keyword.fetch(config, :content) do + {:ok, content} -> {:ok, content} + :error -> {:error, :content_key_not_found} + end + end + defp store_content(content, resource) do Enum.each(content, fn %{key: key} = data -> Depot.insert(resource, key, data) diff --git a/lib/poly_post/depots.ex b/lib/poly_post/depots.ex index 8820cc1..17db851 100644 --- a/lib/poly_post/depots.ex +++ b/lib/poly_post/depots.ex @@ -1,7 +1,10 @@ defmodule PolyPost.Depots do use Supervisor - alias PolyPost.Depot + alias PolyPost.{ + Depot, + Util + } @doc """ Starts the Depot supervisor @@ -28,16 +31,12 @@ defmodule PolyPost.Depots do # Private defp depots do - case get_config() do + case Util.get_config() do {:ok, config} -> get_child_specs(config) :error -> [] end end - defp get_config do - Application.fetch_env(:poly_post, :resources) - end - defp get_child_specs(config) do case Keyword.fetch(config, :content) do {:ok, content} -> diff --git a/lib/poly_post/resource.ex b/lib/poly_post/resource.ex index d756027..4980356 100644 --- a/lib/poly_post/resource.ex +++ b/lib/poly_post/resource.ex @@ -9,6 +9,9 @@ defmodule PolyPost.Resource do @typedoc "The unique ID for an item belonging to a resource" @type key :: term() + @typedoc "The config for a resource" + @type config :: keyword() + @typedoc "The content belonging to an item (includes `key`)" @type content :: %{key: key()} diff --git a/lib/poly_post/util.ex b/lib/poly_post/util.ex new file mode 100644 index 0000000..711c34b --- /dev/null +++ b/lib/poly_post/util.ex @@ -0,0 +1,8 @@ +defmodule PolyPost.Util do + @moduledoc false + + @spec get_config() :: {:ok, term()} | :error + def get_config do + Application.fetch_env(:poly_post, :resources) + end +end diff --git a/test/poly_post_test.exs b/test/poly_post_test.exs index cff2118..75a4351 100644 --- a/test/poly_post_test.exs +++ b/test/poly_post_test.exs @@ -1,5 +1,6 @@ defmodule PolyPostTest do use ExUnit.Case, async: false + use Mneme alias PolyPost.Depot @@ -69,4 +70,15 @@ defmodule PolyPostTest do assert 0 = Depot.get_all(@resource) |> length() end end + + describe ".list_resources/0" do + test "it returns the list of configured resources" do + assert {:ok, resources} = PolyPost.list_resources() + assert Keyword.has_key?(resources, :test_articles) + + metadata = Keyword.get(resources, :test_articles) + assert Keyword.has_key?(metadata, :module) + assert Keyword.has_key?(metadata, :path) + end + end end