Update AuthorTransformer to check if the "data" attibute is available.

dev
KKlochko 3 months ago
parent d398cc1594
commit da0e5246c7

@ -3,11 +3,20 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do
alias DecentralisedBookIndex.Metadata.Author
def from_json(json_body) do
author = %{
id: get_in(json_body, ["data", "id"]),
name: get_in(json_body, ["data", "attributes", "name"]),
description: get_in(json_body, ["data", "attributes", "description"]),
}
author =
if Map.has_key?(json_body, "data") do
%{
id: get_in(json_body, ["data", "id"]),
name: get_in(json_body, ["data", "attributes", "name"]),
description: get_in(json_body, ["data", "attributes", "description"]),
}
else
%{
id: get_in(json_body, ["id"]),
name: get_in(json_body, ["attributes", "name"]),
description: get_in(json_body, ["attributes", "description"]),
}
end
{:ok, author}
end

@ -29,5 +29,23 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
description: "Something"
} = author
end
test "a json doesn't contains author information \"data\" attribute" do
json_body = %{
"attributes" => %{"description" => "Something", "name" => "Author"},
"id" => "889a323e-d104-4b5d-b276-dad5a9b1da9d",
"links" => %{},
"meta" => %{},
"relationships" => %{},
"type" => "author"
}
assert {:ok, author} = AuthorTransformer.from_json(json_body)
assert %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author",
description: "Something"
} = author
end
end
end

Loading…
Cancel
Save