diff --git a/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex b/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex index 2c8aa3e..07ec8bd 100644 --- a/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex +++ b/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex @@ -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 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 index a515708..701511e 100644 --- a/test/decentralised_book_index/sync/data_transformers/author_transformer_test.exs +++ b/test/decentralised_book_index/sync/data_transformers/author_transformer_test.exs @@ -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