majic/lib/gen_magic.ex
2020-06-15 13:57:46 +02:00

40 lines
1.1 KiB
Elixir

defmodule GenMagic do
@moduledoc """
Top-level namespace for GenMagic, the libmagic client for Elixir.
See `GenMagic.Server` or the README for usage.
"""
@doc """
Perform on `path`.
An option of `server: ServerName`, `pool: PoolName` or `once: true` must be passed.
"""
@type option :: name
when name: {:pool, atom()} | {:server, GenMagic.Server.t()} | {:once, true}
@spec perform(GenMagic.Server.target(), [option()]) :: GenMagic.Server.result()
def perform(path, opts, timeout \\ 5000) do
mod = cond do
Keyword.has_key?(opts, :pool) -> {GenMagic.Pool, Keyword.get(opts, :pool)}
Keyword.has_key?(opts, :server) -> {GenMagic.Server, Keyword.get(opts, :server)}
Keyword.has_key?(opts, :once) -> {GenMagic.Helpers, nil}
true -> nil
end
if mod do
do_perform(mod, path, timeout)
else
{:error, :no_method}
end
end
defp do_perform({GenMagic.Helpers, _}, timeout) do
GenMagic.Helpers.perform_once(path, timeout)
end
defp do_perform({mod, name}, path) do
mod.perform(name, path, tiemout)
end
end