Packages
rambla
1.2.6
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/connections/redis.ex
defmodule Rambla.Redis do
@moduledoc """
Default connection implementation for 🔴 Redis.
"""
@with_redis match?({:module, _}, Code.ensure_compiled(Redix))
@behaviour Rambla.Connection
@impl Rambla.Connection
def connect(params) when is_list(params) do
if not @with_redis or is_nil(params[:host]),
do:
raise(Rambla.Exceptions.Connection,
source: __MODULE__,
info: params,
source: __MODULE__,
reason: "inconsistent params",
expected: "🔴 Redix configuration with :host key"
)
maybe_redis(params)
end
@impl Rambla.Connection
def publish(%Rambla.Connection.Config{} = conn, message) when is_binary(message),
do: publish(conn, Jason.decode!(message))
@impl Rambla.Connection
def publish(%Rambla.Connection.Config{conn: pid}, message)
when is_map(message) or is_list(message) do
to_pipeline = for {k, v} <- message, do: ["SET", k, v]
apply(Redix, :pipeline, [pid, to_pipeline])
end
if @with_redis do
defp maybe_redis(params) do
params =
params
|> Keyword.get(:password, "")
|> case do
<<_::binary-size(1), _::binary>> -> params
_ -> Keyword.delete(params, :password)
end
case Redix.start_link(params) do
{:ok, pid} ->
%Rambla.Connection{
conn: %Rambla.Connection.Config{conn: pid},
conn_type: __MODULE__,
conn_pid: pid,
conn_params: params,
errors: []
}
error ->
%Rambla.Connection{
conn: %Rambla.Connection.Config{},
conn_type: __MODULE__,
conn_pid: nil,
conn_params: params,
errors: [error]
}
end
end
else
defp maybe_redis(params) do
error =
Rambla.Exceptions.Connection.exception(
source: __MODULE__,
info: params,
reason: "🔴 Redix should be explicitly included to use this functionality"
)
%Rambla.Connection{
conn: %Rambla.Connection.Config{},
conn_type: __MODULE__,
conn_pid: nil,
conn_params: params,
errors: [error]
}
end
end
end