PingPong (PingPong v0.1.0)

Copy Markdown View Source

Public API and OTP application entry point for PingPong.

PingPong provides a small, consistent interface for sending notifications to registered services. The built-in services are :discord, :telegram, and :mock; additional services can be registered through application configuration.

Results

Synchronous calls return either {:ok, response} or an error tuple. Unknown services return {:error, {:unknown_service, service}}.

Asynchronous calls return {:ok, task} when a service is found. Awaiting the task returns the service result:

{:ok, task} = PingPong.send_async(:mock, %{message: "Ping!"}, %{})
Task.await(task)

Summary

Types

A notification tuple accepted by send_multiple/1.

Notification identifier used by send_multiple/1.

Service-specific delivery options.

Service-specific notification payload.

Standard result returned by PingPong services.

Delivery mode used by PingPong.Notification.

Registered service key, such as :discord, :telegram, or :mock.

Functions

Sends a notification through a registered service.

Sends a notification in a supervised task.

Sends multiple named notifications synchronously.

Types

config()

@type config() :: {service(), payload(), options()}

A notification tuple accepted by send_multiple/1.

id()

@type id() :: atom()

Notification identifier used by send_multiple/1.

options()

@type options() :: map()

Service-specific delivery options.

payload()

@type payload() :: map()

Service-specific notification payload.

result()

@type result() :: {:ok, any()} | {:error, {atom(), any()}}

Standard result returned by PingPong services.

send_type()

@type send_type() :: :sync | :async

Delivery mode used by PingPong.Notification.

service()

@type service() :: atom()

Registered service key, such as :discord, :telegram, or :mock.

Functions

send(service, payload, options)

@spec send(service(), payload(), options()) :: result()

Sends a notification through a registered service.

The payload and options maps are validated by the selected service.

Examples

iex> PingPong.send(:mock, %{message: "Ping!"}, %{})
{:ok, "Pong! 🏓"}

iex> PingPong.send(:unknown, %{}, %{})
{:error, {:unknown_service, :unknown}}

send_async(service, payload, options)

@spec send_async(service(), payload(), options()) :: result()

Sends a notification in a supervised task.

Returns {:ok, task} when the service exists. Use Task.await/1 to receive the service result. Unknown services return an error immediately.

Examples

{:ok, task} = PingPong.send_async(:mock, %{message: "Ping!"}, %{})
{:ok, "Pong! 🏓"} = Task.await(task)

send_multiple(notifications)

@spec send_multiple(Enumerable.t({id(), config()})) :: [{id(), result()}]

Sends multiple named notifications synchronously.

Accepts an enumerable of {id, notification} pairs, where each notification is a {service, payload, options} tuple. The return value preserves each notification ID alongside its result.

Examples

notifications = [
  first: {:mock, %{message: "Ping!"}, %{}},
  second: {:mock, %{}, %{}}
]

PingPong.send_multiple(notifications)