Add types and specs for LastFm module
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
defmodule LastFm.Album do
|
||||
defstruct [:musicbrainz_id, :title]
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
musicbrainz_id: String.t(),
|
||||
title: String.t()
|
||||
}
|
||||
end
|
||||
|
||||
@@ -3,5 +3,5 @@ defmodule LastFm.APIBehaviour do
|
||||
|
||||
@type user :: String.t()
|
||||
@type api_key :: String.t()
|
||||
@callback get_recent_tracks(user, api_key) :: {:ok, [%Track{}]} | {:error, String.t()}
|
||||
@callback get_recent_tracks(user, api_key) :: {:ok, [Track.t()]} | {:error, String.t()}
|
||||
end
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
defmodule LastFm.Artist do
|
||||
defstruct [:musicbrainz_id, :name]
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
musicbrainz_id: String.t(),
|
||||
name: String.t()
|
||||
}
|
||||
end
|
||||
|
||||
@@ -6,11 +6,14 @@ defmodule LastFm.Feed do
|
||||
is technically prone to collision, it's very unlikely for that to happen due to the
|
||||
nature of the data.
|
||||
"""
|
||||
|
||||
@spec create_table!() :: :ok | no_return
|
||||
def create_table! do
|
||||
__MODULE__ = :ets.new(__MODULE__, [:ordered_set, :named_table, :public])
|
||||
:ok
|
||||
end
|
||||
|
||||
@spec update([LastFm.Track.t()]) :: :ok | no_return
|
||||
def update(tracks) do
|
||||
data = Enum.map(tracks, fn t -> {t.scrobbled_at_uts, t} end)
|
||||
|
||||
@@ -20,6 +23,7 @@ defmodule LastFm.Feed do
|
||||
Phoenix.PubSub.broadcast(LastFm.PubSub, "feed:update", %{tracks: tracks})
|
||||
end
|
||||
|
||||
@spec all() :: [LastFm.Track.t()]
|
||||
def all do
|
||||
m = [
|
||||
{
|
||||
@@ -29,9 +33,11 @@ defmodule LastFm.Feed do
|
||||
}
|
||||
]
|
||||
|
||||
# reversing to get tracks in DESC order
|
||||
:ets.select_reverse(__MODULE__, m)
|
||||
end
|
||||
|
||||
@spec subscribe() :: :ok
|
||||
def subscribe do
|
||||
Phoenix.PubSub.subscribe(LastFm.PubSub, "feed:update")
|
||||
end
|
||||
|
||||
@@ -5,11 +5,20 @@ defmodule LastFm.Refresh do
|
||||
|
||||
alias LastFm.Feed
|
||||
|
||||
@type config :: %{
|
||||
api: module(),
|
||||
api_key: String.t(),
|
||||
user: String.t(),
|
||||
refresh_interval: pos_integer()
|
||||
}
|
||||
|
||||
@spec start_link(config) :: GenServer.on_start()
|
||||
def start_link(config) do
|
||||
GenServer.start_link(__MODULE__, config, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
@spec init(config) :: {:ok, config, {:continue, :refresh}} | :ignore
|
||||
def init(config) do
|
||||
config = Map.new(config)
|
||||
|
||||
@@ -21,9 +30,11 @@ defmodule LastFm.Refresh do
|
||||
end
|
||||
|
||||
@impl true
|
||||
@spec handle_continue(atom(), config) :: {:noreply, config}
|
||||
def handle_continue(:refresh, config), do: refresh(config)
|
||||
|
||||
@impl true
|
||||
@spec handle_info(atom(), config) :: {:noreply, config}
|
||||
def handle_info(:refresh, config), do: refresh(config)
|
||||
|
||||
defp refresh(config) do
|
||||
|
||||
@@ -17,6 +17,16 @@ defmodule LastFm.Track do
|
||||
:scrobbled_at_label
|
||||
]
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
musicbrainz_id: String.t(),
|
||||
title: String.t(),
|
||||
artist: Artist.t(),
|
||||
album: Album.t(),
|
||||
cover_url: String.t(),
|
||||
scrobbled_at_uts: integer(),
|
||||
scrobbled_at_label: String.t()
|
||||
}
|
||||
|
||||
def from_api_response(raw_tracks) do
|
||||
Enum.map(raw_tracks, fn t ->
|
||||
album = %Album{
|
||||
|
||||
Reference in New Issue
Block a user