1
0
mirror of https://github.com/algora-io/tv.git synced 2025-03-17 20:17:45 +02:00
2024-04-27 22:06:46 +03:00

49 lines
1.1 KiB
Elixir

defmodule Algora.Stargazer do
require Logger
use GenServer
@url "https://api.github.com/repos/algora-io/tv"
@poll_interval :timer.minutes(10)
def start_link(cmd) do
GenServer.start_link(__MODULE__, cmd, name: __MODULE__)
end
@impl true
def init(cmd) do
{:ok, schedule_fetch(%{count: nil}, cmd, 0)}
end
@impl true
def handle_info(cmd, state) do
count = fetch_count() || state.count
{:noreply, schedule_fetch(%{state | count: count}, cmd)}
end
defp schedule_fetch(state, cmd, after_ms \\ @poll_interval) do
Process.send_after(self(), cmd, after_ms)
state
end
defp fetch_count() do
with {:ok, %Finch.Response{status: 200, body: body}} <-
:get
|> Finch.build(@url)
|> Finch.request(Algora.Finch),
{:ok, %{"stargazers_count" => count}} <- Jason.decode(body) do
count
else
_ -> nil
end
end
def count() do
GenServer.call(__MODULE__, :get_count)
end
@impl true
def handle_call(:get_count, _from, state) do
{:reply, state.count, state}
end
end