Skip to content

Commit

Permalink
Adding PolyPost.list_resources/0 (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
alakra authored Dec 24, 2024
1 parent 8576b96 commit 3516d0d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
25 changes: 24 additions & 1 deletion lib/poly_post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ defmodule PolyPost do
alias PolyPost.{
Builder,
Depots,
Depot
Depot,
Resource,
Util
}

# API
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions lib/poly_post/depots.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
defmodule PolyPost.Depots do
use Supervisor

alias PolyPost.Depot
alias PolyPost.{
Depot,
Util
}

@doc """
Starts the Depot supervisor
Expand All @@ -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} ->
Expand Down
3 changes: 3 additions & 0 deletions lib/poly_post/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()}

Expand Down
8 changes: 8 additions & 0 deletions lib/poly_post/util.ex
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/poly_post_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule PolyPostTest do
use ExUnit.Case, async: false
use Mneme

alias PolyPost.Depot

Expand Down Expand Up @@ -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

0 comments on commit 3516d0d

Please sign in to comment.