Skip to content

Commit

Permalink
Refactor get identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielpra1 committed Feb 14, 2020
1 parent 91f1e93 commit 5c7d833
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/middlewares/rate_limiter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ defmodule Rajska.RateLimiter do
end
end

defp get_identifier(%Resolution{context: context, arguments: arguments}, keys, id) do
case {keys, id} do
{nil, nil} -> Rajska.apply_auth_mod(context, :get_ip, [context])
{keys, nil} -> get_in(arguments, List.wrap(keys)) || raise "Invalid configuration in Rate Limiter. Key not found in arguments."
{nil, id} -> id
{_key, _id} -> raise "Invalid configuration in Rate Limiter. If key is defined, then id must not be defined"
end
end
defp get_identifier(%Resolution{context: context}, nil, nil),
do: Rajska.apply_auth_mod(context, :get_ip, [context])

defp get_identifier(%Resolution{arguments: arguments}, keys, nil),
do: get_in(arguments, List.wrap(keys)) || raise "Invalid configuration in Rate Limiter. Key not found in arguments."

defp get_identifier(%Resolution{}, nil, id), do: id

defp get_identifier(%Resolution{}, _keys, _id), do: raise "Invalid configuration in Rate Limiter. If key is defined, then id must not be defined"
end

0 comments on commit 5c7d833

Please sign in to comment.