Skip to content

Instantly share code, notes, and snippets.

@andrewtimberlake
Created June 12, 2016 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewtimberlake/fd688dec5e69acc8c5d1c3f0269d7592 to your computer and use it in GitHub Desktop.
Save andrewtimberlake/fd688dec5e69acc8c5d1c3f0269d7592 to your computer and use it in GitHub Desktop.
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
@andrewtimberlake
Copy link
Author

Use in controller as:
plug MyPlugs.strip_params "field"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment