Add LastFm.Scrobble abstraction

This commit is contained in:
Claudio Ortolina
2025-05-07 08:38:35 +01:00
parent fcdae8f925
commit 8477517e5c
3 changed files with 18 additions and 7 deletions
+5 -3
View File
@@ -1,5 +1,5 @@
defmodule LastFm do defmodule LastFm do
alias LastFm.{API, Feed, Refresh} alias LastFm.{API, Feed, Refresh, Scrobble}
def get_scrobbled_tracks, do: Feed.all_tracks() def get_scrobbled_tracks, do: Feed.all_tracks()
@@ -47,10 +47,12 @@ defmodule LastFm do
API.get_session(token, last_fm_config) API.get_session(token, last_fm_config)
end end
def scrobble(tracks, session_key) do def scrobble(scrobbles, session_key) do
last_fm_config = last_fm_config() 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 end
def auth_url do def auth_url do
+9
View File
@@ -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
+4 -4
View File
@@ -1,7 +1,7 @@
defmodule LastFmTest do defmodule LastFmTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
alias LastFm.{Artist, Fixtures} alias LastFm.{Artist, Fixtures, Scrobble}
describe "get_artist_info/1" do describe "get_artist_info/1" do
test "it returns the artist info" do test "it returns the artist info" do
@@ -23,8 +23,8 @@ defmodule LastFmTest do
describe "scrobble/2" do describe "scrobble/2" do
test "it returns the scrobbled track" do test "it returns the scrobbled track" do
tracks = [ scrobbles = [
%{ %Scrobble{
track: "Wonderland", track: "Wonderland",
artist: "IQ", artist: "IQ",
album: "Dominion", album: "Dominion",
@@ -58,7 +58,7 @@ defmodule LastFmTest do
Req.Test.json(conn, response_body) Req.Test.json(conn, response_body)
end) end)
assert {:ok, response_body} == LastFm.scrobble(tracks, session_key) assert {:ok, response_body} == LastFm.scrobble(scrobbles, session_key)
end end
end end
end end