Lepus behaviour (Lepus v0.1.6)

View Source

Defines a RabbitMQ client.

Example usage

defmodule MyApp.RabbitMQ do
  use Lepus, client: Lepus.BasicClient
end

Add Lepus.BasicClient to your supervision tree (see the Lepus.BasicClient documentation).

Use it to publish a message to Rabbit MQ:

MyApp.RabbitMQ.publish_json("my-exchange", "my-routing-key", %{a: 1, b: 2})

Testing

For testing purposes you can use another :client. It could be Mox mock.

defmodule MyApp.RabbitMQ do
  use Lepus, client: Application.get_env(:my_app, __MODULE__, []) |> Keyword.fetch!(:client)
end

In the prod.exs or dev.exs:

config :my_app, MyApp.RabbitMQ, client: Lepus.BasicClient

In the test.exs:

config :my_app, MyApp.RabbitMQ, client: MyApp.RabbitMQClientMock

In the tests:

test "uses Mox for Lepus" do
  MyApp.RabbitMQClientMock
  |> expect(:publish_json, fn _, "my-exchange", "my-routing-key", payload, _opts ->
    assert %{my_key: "My Value"} = payload
  end)

  MyApp.RabbitMQ.publish_json("my-exchange", "my-routing-key", %{my_key: "My Value"})
end

Summary

Callbacks

Publishes message to RabbitMQ exchange.

The same as publish/4 but payload can be any type that is convertable to JSON (map, list, etc.). Sends JSON string to RabbitMQ exchange.

Callbacks

publish(exchange, routing_key, binary_payload, opts)

(optional)

Publishes message to RabbitMQ exchange.

Options

  • amqp_opts - AMQP.Basic.publish/5 options.
  • rpc - Defines if the message uses RPC pattern. The default value is false.
  • timeout - number of milliseconds or :infinity. Valid for RPC. The default value is 5000.

Returns

Without rpc: true:

  • :ok - in case of success
  • {:error, any} in case of error

With rpc: true:

  • {:ok, any} - in case of success
  • {:error, :timeout} - in case of timeout error
  • {:error, any} - in case of error

publish_json(exchange, routing_key, payload, opts)

(optional)

The same as publish/4 but payload can be any type that is convertable to JSON (map, list, etc.). Sends JSON string to RabbitMQ exchange.