this repo has no description

Add functions documentation and typespecs

hauleth.dev 230fa8e1 1dae963c

verified
Changed files
+69 -4
lib
+25 -4
lib/esl_hn/cache.ex
···
end
end
+
@doc """
+
Fetch data from cache
+
"""
def fetch(tid, key) do
meta = %{key: key, tid: tid}
···
end)
end
+
@doc """
+
Get data from cache and returns `default` when value isn't present
+
"""
def get(tid, key, default \\ nil) do
case fetch(tid, key) do
{:ok, val} -> val
···
end
end
+
@doc """
+
Write `data` to cache under `key`
+
"""
def write(tid, key, data) do
meta = %{key: key, tid: tid}
···
end)
end
+
@doc """
+
Bulk write to cache
+
"""
def write_all(tid, data) do
meta = %{tid: tid}
···
end)
end
-
def prune(tid, id) do
-
:ets.delete(tid, id)
+
@doc """
+
Remove value under given key from cache
+
"""
+
def prune(tid, key) do
+
:ets.delete(tid, key)
:ok
end
-
def prune_all(tid, ids) do
-
for id <- ids, do: :ets.delete(tid, id)
+
@doc """
+
Bulk removal of values from cache
+
"""
+
def prune_all(tid, keys) do
+
for key <- keys, do: :ets.delete(tid, key)
:ok
end
+
@doc """
+
Remove all cached data
+
"""
def flush(tid) do
:ets.delete_all_objects(tid)
+15
lib/esl_hn/config_reader.ex
···
something like that
"""
+
@doc """
+
Fetch duration from environment variable `name`
+
+
The return value will be in milliseconds.
+
+
Supported formats are:
+
- `{integer}` - number of milliseconds in duration
+
- `{integer}(s|m|h)` - number of seconds|minutes|hours in duration
+
+
If environment variable is not set, then it will use `default` value.
+
"""
+
@spec duration(name :: String.t(), default :: duration) :: non_neg_integer()
+
when duration:
+
non_neg_integer()
+
| {:seconds | :minutes | :hours, non_neg_integer()}
def duration(name, default) do
case System.fetch_env(name) do
:error -> default
+19
lib/esl_hn/hn.ex
···
alias EslHn.Hn.Story
+
@type opts() :: keyword()
+
+
@doc """
+
Fetch list of top stories
+
+
## Options
+
+
- `:limit` - amount of stories to fetch (defaults to `50`)
+
"""
# TODO: Is there a way to limit amount of returned entries in query?
+
@spec top_stories_ids(opts()) :: {:ok, [Story.id()]} | {:error, term()}
def top_stories_ids(opts \\ []) do
limit = opts[:limit] || 50
···
end
end
+
@doc """
+
Fetch single story for given ID
+
"""
+
@spec story(Story.id(), opts()) :: {:ok, Story.t()} | {:error, term()}
def story(id, opts \\ []) do
with {:ok, body} <- get("item/:id.json", [id: id], opts) do
Story.changeset(body)
···
end
end
+
@doc """
+
Fetch stories for IDs in enumerable
+
"""
+
@spec stories(Enumerable.t(Story.id()), opts()) ::
+
{:ok, [Story.t()]} | {:error, term()}
def stories(ids, opts \\ []) do
ids
|> Task.async_stream(&story(&1, opts))
+9
lib/esl_hn/hn/story.ex
···
@primary_key {:id, :integer, []}
+
@type id() :: non_neg_integer()
+
+
@type t() :: %__MODULE__{
+
id: id(),
+
url: String.t() | nil,
+
type: atom(),
+
text: String.t()
+
}
+
embedded_schema do
# TODO: Make it `URL` structure
field :url, :string
+1
lib/esl_hn/refresher.ex
···
@moduledoc """
Module defining process that is responsible for refreshing cached data
"""
+
use GenServer
alias EslHn.Hn