Noizu.Slack.Pagination (Noizu.Slack v0.1.0)

Copy Markdown View Source

Lazy cursor walker behind every *_stream helper.

Slack cursor pagination: pass cursor, read response_metadata.next_cursor; an empty or missing next_cursor ends the walk. Pages are fetched only as the consumer pulls. A failed page fetch raises the %Noizu.Slack.Error{} (a stream cannot return an error tuple) — rescue at the enumeration site if needed.

Noizu.Slack.Pagination.stream(
  fn cursor -> Noizu.Slack.Request.get(client, "conversations.list", %{cursor: cursor}) end,
  results_key: "channels"
)
|> Enum.take(500)

Summary

Functions

Returns the page's response_metadata.next_cursor, or nil when exhausted.

Builds a stream of items. fetch receives nil for the first page and the previous page's next_cursor afterwards.

Puts (or drops) cursor in a params map.

Types

fetch()

@type fetch() :: (String.t() | nil -> {:ok, map()} | {:error, Noizu.Slack.Error.t()})

Functions

next_cursor(arg1)

@spec next_cursor(map()) :: String.t() | nil

Returns the page's response_metadata.next_cursor, or nil when exhausted.

stream(fetch, opts)

@spec stream(fetch(), keyword()) :: Enumerable.t()

Builds a stream of items. fetch receives nil for the first page and the previous page's next_cursor afterwards.

Options: :results_key (required) — the list key in each page ("channels", "members", "messages", …).

with_cursor(params, cursor)

@spec with_cursor(map(), String.t() | nil) :: map()

Puts (or drops) cursor in a params map.