Created
June 12, 2016 13:32
-
-
Save andrewtimberlake/fd688dec5e69acc8c5d1c3f0269d7592 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyPlugs do | | |
def strip_params(conn, param_key) when is_binary(param_key) do | | |
param = Map.get(conn.params, param_key) |> strip_param | | |
params = Map.put(conn.params, param_key, param) | | |
%{conn | params: params} | | |
end | | |
| | |
defp strip_param(%{__struct__: mod} = struct) when is_atom(mod) do | | |
struct | | |
end | | |
defp strip_param(%{}=param) do | | |
Enum.reduce(param, %{}, fn({k, v}, acc) -> | | |
Map.put(acc, k, strip_param(v)) | | |
end) | | |
end | | |
defp strip_param(param) when is_list(param) do | | |
Enum.map(param, &strip_param/1) | | |
end | | |
defp strip_param(param) when is_binary(param), do: String.strip(param) | | |
defp strip_param(param), do: param | | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use in controller as:
plug MyPlugs.strip_params "field"