Rolodex v0.10.1 Rolodex.Headers

Exposes functions and macros for defining reusable headers in route doc annotations or responses.

It exposes the following macros, which when used together will set up the headers:

  • headers/2 - for declaring the headers
  • header/3 - for declaring a single header for the set

It also exposes the following functions:

  • is_headers_module?/1 - determines if the provided item is a module that has defined a reusable headers set
  • to_map/1 - serializes the headers module into a map

Link to this section Summary

Functions

Opens up the headers definition for the current module. Will name the headers set and generate a list of header fields based on the macro calls within

Determines if an arbitrary item is a module that has defined a reusable headers set via Rolodex.Headers macros

Serializes the Rolodex.Headers metadata into a formatted map

Link to this section Functions

Link to this macro

field(identifier, type, opts \\ []) (macro)

Sets a header field.

Accepts

  • identifier - the header name
  • type - the header field type
  • opts (optional) - additional metadata. See Field.new/1 for a list of valid options.
Link to this macro

headers(name, list) (macro)

Opens up the headers definition for the current module. Will name the headers set and generate a list of header fields based on the macro calls within.

Accepts

  • name - the headers name
  • block - headers shape definition

Example

defmodule SimpleHeaders do
  use Rolodex.Headers

  headers "SimpleHeaders" do
    field "X-Rate-Limited", :boolean
    field "X-Per-Page", :integer, desc: "Number of items in the response"
  end
end
Link to this function

is_headers_module?(mod)
is_headers_module?(any()) :: boolean()

Determines if an arbitrary item is a module that has defined a reusable headers set via Rolodex.Headers macros

Example

defmodule SimpleHeaders do
...>   use Rolodex.Headers
...>   headers "SimpleHeaders" do
...>     field "X-Rate-Limited", :boolean
...>   end
...> end
iex>
# Validating a headers module
Rolodex.Headers.is_headers_module?(SimpleHeaders)
true
iex> # Validating some other module
iex> Rolodex.Headers.is_headers_module?(OtherModule)
false
Link to this function

to_map(mod)
to_map(module()) :: map()

Serializes the Rolodex.Headers metadata into a formatted map

Example

iex> defmodule SimpleHeaders do
...>   use Rolodex.Headers
...>
...>   headers "SimpleHeaders" do
...>     field "X-Rate-Limited", :boolean
...>     field "X-Per-Page", :integer, desc: "Number of items in the response"
...>   end
...> end
iex>
iex> Rolodex.Headers.to_map(SimpleHeaders)
%{
  "X-Per-Page" => %{desc: "Number of items in the response", type: :integer},
  "X-Rate-Limited" => %{type: :boolean}
}