Parse matching record fields in Records.Record
This commit is contained in:
@@ -599,22 +599,6 @@ defmodule MusicLibrary.ListeningStats do
|
||||
defp parse_matching_records(json) when is_binary(json) do
|
||||
json
|
||||
|> JSON.decode!()
|
||||
|> Enum.map(fn record ->
|
||||
%{
|
||||
id: record["id"],
|
||||
title: record["title"],
|
||||
format: record["format"],
|
||||
type: record["type"],
|
||||
purchased_at: parse_purchased_at(record["purchased_at"]),
|
||||
cover_hash: record["cover_hash"]
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp parse_purchased_at(nil), do: nil
|
||||
|
||||
defp parse_purchased_at(dt_string) do
|
||||
{:ok, dt, _offset} = DateTime.from_iso8601(dt_string)
|
||||
dt
|
||||
|> Enum.map(&Record.parse_matching_record/1)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -321,4 +321,30 @@ defmodule MusicLibrary.Records.Record do
|
||||
def format_as_date(dt) do
|
||||
Calendar.strftime(dt, "%d/%m/%Y")
|
||||
end
|
||||
|
||||
@spec parse_matching_record(map()) :: map()
|
||||
def parse_matching_record(%{
|
||||
"id" => id,
|
||||
"title" => title,
|
||||
"format" => format,
|
||||
"type" => type,
|
||||
"purchased_at" => purchased_at,
|
||||
"cover_hash" => cover_hash
|
||||
}) do
|
||||
%{
|
||||
id: id,
|
||||
title: title,
|
||||
format: String.to_existing_atom(format),
|
||||
type: String.to_existing_atom(type),
|
||||
purchased_at: parse_datetime(purchased_at),
|
||||
cover_hash: cover_hash
|
||||
}
|
||||
end
|
||||
|
||||
defp parse_datetime(nil), do: nil
|
||||
|
||||
defp parse_datetime(dt_string) do
|
||||
{:ok, dt, _offset} = DateTime.from_iso8601(dt_string)
|
||||
dt
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user