diff --git a/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex b/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex new file mode 100644 index 0000000..0398712 --- /dev/null +++ b/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex @@ -0,0 +1,14 @@ +defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do + + alias DecentralisedBookIndex.Metadata.Author + + def from_json(json_body) do + author = %Author{ + id: get_in(json_body, ["data", "id"]), + name: get_in(json_body, ["data", "attributes", "name"]), + description: get_in(json_body, ["data", "attributes", "description"]), + } + + {:ok, author} + end +end diff --git a/test/decentralised_book_index/sync/data_transformers/author_transformer_test.exs b/test/decentralised_book_index/sync/data_transformers/author_transformer_test.exs new file mode 100644 index 0000000..38398af --- /dev/null +++ b/test/decentralised_book_index/sync/data_transformers/author_transformer_test.exs @@ -0,0 +1,33 @@ +defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do + use ExUnit.Case, async: true + + alias DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer + alias DecentralisedBookIndex.Metadata.Author + + describe "correct transformations" do + test "a json contains correct author information" do + json_body = %{ + "data" => %{ + "attributes" => %{"description" => "Something", "name" => "Author"}, + "id" => "889a323e-d104-4b5d-b276-dad5a9b1da9d", + "links" => %{}, + "meta" => %{}, + "relationships" => %{}, + "type" => "author" + }, + "jsonapi" => %{"version" => "1.0"}, + "links" => %{ + "self" => "http://localhost:4000/api/v1/json/author/889a323e-d104-4b5d-b276-dad5a9b1da9d" + }, + "meta" => %{} + } + + assert {:ok, author} = AuthorTransformer.from_json(json_body) + assert %Author{ + id: "889a323e-d104-4b5d-b276-dad5a9b1da9d", + name: "Author", + description: "Something" + } = author + end + end +end