Packages
redix
0.10.2
1.6.0
1.5.3
1.5.2
1.5.1
1.5.0
1.4.2
1.4.1
1.4.0
1.3.0
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
retired
1.1.0
1.0.0
0.11.2
0.11.1
0.11.0
0.10.7
0.10.6
0.10.5
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.0
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Fast, pipelined, resilient Redis driver for Elixir.
Current section
Files
Jump to
Current section
Files
lib/redix/uri.ex
defmodule Redix.URI do
@moduledoc false
@spec opts_from_uri(binary) :: Keyword.t()
def opts_from_uri(uri) when is_binary(uri) do
%URI{host: host, port: port, scheme: scheme} = uri = URI.parse(uri)
unless scheme in ["redis", "rediss"] do
raise ArgumentError, "expected scheme to be redis:// or rediss://, got: #{scheme}://"
end
[]
|> put_if_not_nil(:host, host)
|> put_if_not_nil(:port, port)
|> put_if_not_nil(:password, password(uri))
|> put_if_not_nil(:database, database(uri))
|> enable_ssl_if_secure_scheme(scheme)
end
defp password(%URI{userinfo: nil}) do
nil
end
defp password(%URI{userinfo: userinfo}) do
case String.split(userinfo, ":", parts: 2) do
[_, password] ->
password
_other ->
raise ArgumentError,
"expected password in the Redis URI to be given as redis://:PASSWORD@HOST or " <>
"redis://DISCARDED_USER:PASSWORD@HOST"
end
end
defp database(%URI{path: path}) when path in [nil, "/"] do
nil
end
defp database(%URI{path: "/" <> path = full_path}) do
case Integer.parse(path) do
{db, ""} ->
db
_other ->
raise ArgumentError, "expected database to be an integer, got: #{inspect(full_path)}"
end
end
defp put_if_not_nil(opts, _key, nil), do: opts
defp put_if_not_nil(opts, key, value), do: Keyword.put(opts, key, value)
defp enable_ssl_if_secure_scheme(opts, "rediss"), do: Keyword.put(opts, :ssl, true)
defp enable_ssl_if_secure_scheme(opts, _scheme), do: opts
end