Rocksky.Builder (Rocksky v0.1.0)

Copy Markdown View Source

use Rocksky.Builder turns a module into a chainable, pipe-friendly request builder for a single XRPC procedure.

Example

defmodule Rocksky.Scrobble.Builder do
  use Rocksky.Builder,
    nsid: "app.rocksky.scrobble.createScrobble",
    required: [:title, :artist],
    optional: [:album, :duration, :mbId, :isrc, :albumArt, :timestamp]
end

alias Rocksky.Scrobble.Builder, as: Scrobble

Scrobble.new(title: "In Bloom", artist: "Nirvana")
|> Scrobble.album("Nevermind")
|> Scrobble.timestamp(System.system_time(:second))
|> Scrobble.submit(client)

What you get

For each field listed in :required or :optional the macro generates a field/2 setter that returns the updated builder (snake-cased: a :mbId field becomes mb_id/2).

In addition:

  • new/1 — build from a keyword list / map. Returns a %__MODULE__{}.
  • put/2 — generic batch setter that accepts a keyword list or map.
  • submit/2 — issue the underlying XRPC procedure with the current builder as the JSON body. Returns {:ok, body} | {:error, Rocksky.Error.t()}. If any :required field is nil, returns {:error, %Rocksky.Error{reason: :missing_fields, ...}} without making a network call.
  • to_body/1 — return the body that would be sent, with nil fields stripped. Useful for inspection in tests.