ExNtfy.Action (ex_ntfy v0.1.1)

Copy Markdown View Source

An ntfy action button.

A single struct covers all four action types (:view, :broadcast, :http, :copy) with the union of their fields; fields not applicable to a type are nil. Unknown action types are kept as {:unknown, string} rather than raising, since the server may add types over time.

TypeRequired fieldsOptional fields
:viewlabel, urlclear
:broadcastlabelintent, extras, clear
:httplabel, urlmethod, headers, body, clear
:copylabel, valueclear

Summary

Functions

Builds an action from a decoded JSON map (the incoming "action" key names the type).

Encodes an action to the ntfy JSON object shape (the outgoing counterpart of from_map/1 — fixture maps round-trip exactly).

Encodes an action in ntfy's short header format (§1.5): key=value pairs joined by ,, with headers and extras maps flattened to headers.<name>= / extras.<key>= (keys sorted for determinism).

Types

t()

@type t() :: %ExNtfy.Action{
  body: String.t() | nil,
  clear: boolean(),
  extras: %{optional(String.t()) => String.t()} | nil,
  headers: %{optional(String.t()) => String.t()} | nil,
  id: String.t() | nil,
  intent: String.t() | nil,
  label: String.t() | nil,
  method: String.t() | nil,
  type: type() | nil,
  url: String.t() | nil,
  value: String.t() | nil
}

type()

@type type() :: :view | :broadcast | :http | :copy | {:unknown, String.t()}

Functions

from_map(map)

@spec from_map(map()) :: t()

Builds an action from a decoded JSON map (the incoming "action" key names the type).

Lenient: missing fields become nil (clear defaults to false), unknown fields are ignored.

to_json_map(action)

@spec to_json_map(t() | map()) :: map()

Encodes an action to the ntfy JSON object shape (the outgoing counterpart of from_map/1 — fixture maps round-trip exactly).

nil fields and a false clear are omitted; plain ntfy-shaped maps pass through untouched.

Examples

iex> ExNtfy.Action.to_json_map(%ExNtfy.Action{type: :copy, label: "Copy", value: "abc"})
%{"action" => "copy", "label" => "Copy", "value" => "abc"}

to_short(action)

@spec to_short(t() | map()) :: String.t()

Encodes an action in ntfy's short header format (§1.5): key=value pairs joined by ,, with headers and extras maps flattened to headers.<name>= / extras.<key>= (keys sorted for determinism).

Values containing ,, ;, or quotes are quoted — double quotes by default, single quotes when the value itself contains a double quote. clear appears only when true; id is server-assigned and never emitted.

Examples

iex> ExNtfy.Action.to_short(%ExNtfy.Action{type: :view, label: "Open", url: "https://x.io"})
"action=view, label=Open, url=https://x.io"