diff --git a/lib/last_fm.ex b/lib/last_fm.ex index a21adfe5..0533bd59 100644 --- a/lib/last_fm.ex +++ b/lib/last_fm.ex @@ -1,5 +1,5 @@ defmodule LastFm do - alias LastFm.{API, Feed, Refresh} + alias LastFm.{API, Feed, Refresh, Scrobble} def get_scrobbled_tracks, do: Feed.all_tracks() @@ -47,10 +47,12 @@ defmodule LastFm do API.get_session(token, last_fm_config) end - def scrobble(tracks, session_key) do + def scrobble(scrobbles, session_key) do last_fm_config = last_fm_config() - API.scrobble(tracks, session_key, last_fm_config) + scrobbles + |> Enum.map(&Scrobble.encode/1) + |> API.scrobble(session_key, last_fm_config) end def auth_url do diff --git a/lib/last_fm/scrobble.ex b/lib/last_fm/scrobble.ex new file mode 100644 index 00000000..ff2b16ff --- /dev/null +++ b/lib/last_fm/scrobble.ex @@ -0,0 +1,9 @@ +defmodule LastFm.Scrobble do + defstruct [:track, :artist, :timestamp, :album, :album_artist, :mbid] + + def encode(scrobble) do + scrobble + |> Map.from_struct() + |> Map.reject(fn {_key, value} -> is_nil(value) end) + end +end diff --git a/test/last_fm_test.exs b/test/last_fm_test.exs index 901d18bd..ca4509d1 100644 --- a/test/last_fm_test.exs +++ b/test/last_fm_test.exs @@ -1,7 +1,7 @@ defmodule LastFmTest do use ExUnit.Case, async: true - alias LastFm.{Artist, Fixtures} + alias LastFm.{Artist, Fixtures, Scrobble} describe "get_artist_info/1" do test "it returns the artist info" do @@ -23,8 +23,8 @@ defmodule LastFmTest do describe "scrobble/2" do test "it returns the scrobbled track" do - tracks = [ - %{ + scrobbles = [ + %Scrobble{ track: "Wonderland", artist: "IQ", album: "Dominion", @@ -58,7 +58,7 @@ defmodule LastFmTest do Req.Test.json(conn, response_body) end) - assert {:ok, response_body} == LastFm.scrobble(tracks, session_key) + assert {:ok, response_body} == LastFm.scrobble(scrobbles, session_key) end end end