Shun.Rule (Shun v1.0.2) View Source
Represents what should be done based on the target pattern, which can represent an URI, IPv4, or IPv6 address. It can also be given a String literal, which represents either an IPv4 or IPv6 address, or a range of addresses using CIDR notation.
Since the Rule module only emits results based on target patterns (i.e. Elixir AST), it is best
used from macros, such as those found within Shun.Builder
, which can implement macros.
Link to this section Summary
Types
Represents a handler function, to be used with handle/2
.
Represents a Rule that is built and can be used at compile-time.
Functions
Generates an Accept rule for the given Target.
Generates a Dynamic rule for the given Target.
Generates a Reject rule for the given Target.
Link to this section Types
Specs
handle_fun() :: Shun.Provider.dynamic_uri_fun() | Shun.Provider.dynamic_address_fun()
Represents a handler function, to be used with handle/2
.
Specs
t()
Represents a Rule that is built and can be used at compile-time.
Rules are understood by Shun.Builder
.
Link to this section Functions
Specs
accept(Shun.Rule.Target.t()) :: t()
Generates an Accept rule for the given Target.
Expects a target conforming to Shun.Rule.Target.t/0
.
Specs
handle(Shun.Rule.Target.t(), handle_fun()) :: t()
Generates a Dynamic rule for the given Target.
Expects a target conforming to Shun.Rule.Target.t/0
.
The second argument must refer to a function, which accepts either an URI or an Address.
Example
You can use handle/2
to implement a Rule which will call your function at runtime if the value
was matched.
defmodule MyApp.Shun do
use Shun.Builder
handle %URI{}, &custom_handle_uri/1
def custom_handle_uri(uri) do
cond do
MyApp.Whitelist.allow_host?(uri.host) -> :accept
true -> :reject
end
end
end
You can also use guards with handle/2
:
defmodule MyApp.Shun do
use Shun.Builder
handle %URI{host: host} when host == "example.com", &custom_handle_uri/1
…
end
Specs
reject(Shun.Rule.Target.t()) :: t()
Generates a Reject rule for the given Target.
Expects a target conforming to Shun.Rule.Target.t/0
.