Add LastFm.Scrobble abstraction
This commit is contained in:
+5
-3
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user