Created
March 26, 2018 16:19
-
-
Save zachdaniel/9ed11650c527b572ee32c7b7e429da11 to your computer and use it in GitHub Desktop.
This file contains 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 Mandark.Web.Plug.IndexedArrays do | |
@moduledoc """ | |
Looks for any | |
Attaches hostname as a response heade to requests. | |
""" | |
@behaviour Plug | |
@impl Plug | |
@spec init(Keyword.t) :: Keyword.t | |
def init(_opts), do: [] | |
@impl Plug | |
@spec call(Plug.Conn.t, Keyword.t) :: Plug.Conn.t | |
def call(conn, _opts) do | |
%{conn | params: parse(conn.params)} | |
end | |
defp parse(params) when is_map(params) do | |
IO.inspect(params) | |
list_params = | |
params | |
|> Enum.reduce([], fn | |
(_, false) -> | |
false | |
({key, value}, acc) -> | |
case Integer.parse(key) do | |
{i, ""} -> [{i, value} | acc] | |
_ -> false | |
end | |
end) | |
if list_params do | |
list_params | |
|> Enum.sort_by(&elem(&1, 0)) | |
|> Enum.map(fn {_i, value} -> | |
parse(value) | |
end) | |
else | |
Enum.into(params, %{}, fn {key, value} -> | |
{key, parse(value)} | |
end) | |
end | |
end | |
defp parse(params) when is_list(params) do | |
Enum.map(params, &parse/1) | |
end | |
defp parse(params), do: params | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment