Packages
rambla
1.2.5
1.5.0
1.4.2
1.4.1
1.4.0
1.3.0
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.16.6
0.16.5
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.0
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.1
0.13.0
0.12.2
0.12.1
0.11.1
0.11.0
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
Easy publishing to many different targets. Supported back-ends: - Rabbit [Amqp](https://hexdocs.pm/amqp/) - Redis [Redix](https://hexdocs.pm/redix) - Http [:httpc](http://erlang.org/doc/man/httpc.html) - Smtp [:gen_smtp](https://hexdocs.pm/gen_smtp) - Slack [Envío](https://hexdocs.pm/envio)
Current section
Files
Jump to
Current section
Files
lib/rambla/handlers/stub.ex
defmodule Rambla.Handlers.Stub do
@moduledoc """
Default handler for _Stub_ testing doubles. Unlike `Rambla.Handlers.Mock`,
this module might be used as a stub for remote service calls when
no expectation is to be defined, or when there is no room to define
such an expectation (e. g. while application start.)
By default it’d be simply return `:ok`.
```elixir
config :rambla, stub: [
connections: [stubbed: :conn],
channels: [chan_0: [connection: :stubbed]]
]
# Then you can access the connection/channel via `Rambla.Handlers.Stub` or
# implicitly via `Rambla` as
Rambla.Handlers.Stub.publish(:chan_0, %{message: %{foo: 42}, serializer: Jason})
Rambla.publish(:chan_0, %{message: %{foo: 42}, serializer: Jason})
```
## Stub modules
To implement the custom `Stub`, returning any value, or like, use
```elixir
defmodule ConnStub do
use Rambla.Handlers.Stub, %{token: "FOOBAR"}
@behaviour Rambla.Handlers.Stub
def on_publish(_name, _message, _options) do
{:ok, @stub_options}
end
end
```
"""
use Rambla.Handler
@callback on_publish(name :: atom(), message :: any(), options :: map()) ::
Rambla.Handler.resolution()
@impl Rambla.Handler
@doc false
def handle_publish(%{message: message}, options, %{connection: %{channel: name}}) do
{preferred_format, options} = Map.pop(options, :preferred_format, :binary)
{stub, options} = Map.pop(options, :stub, Rambla.Mocks.Stub)
do_handle_publish(stub, name, converter(preferred_format, message), options)
end
def handle_publish(callback, options, %{connection: %{channel: name}})
when is_function(callback, 1) do
callback.(source: __MODULE__, destination: name, options: options)
end
def handle_publish(payload, options, state),
do: handle_publish(%{message: payload}, options, state)
@impl Rambla.Handler
@doc false
def config, do: Application.get_env(:rambla, :stub)
def do_handle_publish(stub, name, message, options) do
stub.on_publish(name, message, options)
end
@doc false
defmacro __using__(stub_options \\ []) do
quote do
require Logger
@stub_options unquote(Macro.expand(stub_options, __CALLER__))
@behaviour Rambla.Handlers.Stub
def on_publish(name, message, options) do
Logger.info(
"[🖇️] STUBBED CALL [#{name}] with " <> inspect(message: message, options: options)
)
{:ok, @stub_options}
end
defoverridable on_publish: 3
end
end
end