1
0
Fork 0
mirror of https://github.com/edgurgel/httparrot synced 2025-04-10 12:12:24 -04:00
httparrot/lib/httparrot/general_request_info.ex
Po Chen 3b1da703f0 fix group_by_keys (#20)
When there are more than 2 values that have the same key,
group_by_keys misbehave and produce something like
`%{list: [["a", "b"], "c"]}`
2016-09-09 09:10:22 +12:00

31 lines
856 B
Elixir

defmodule HTTParrot.GeneralRequestInfo do
def retrieve(req) do
{args, req} = :cowboy_req.qs_vals(req)
{headers, req} = :cowboy_req.headers(req)
{url, req} = :cowboy_req.url(req)
{{ip, _port}, req} = :cowboy_req.peer(req)
ip = :inet_parse.ntoa(ip) |> to_string
args = group_by_keys(args)
{[args: args, headers: headers, url: url, origin: ip], req}
end
@doc """
Group by keys and if duplicated keys, aggregate them as a list
iex> group_by_keys([a: "v1", a: "v2", b: "v3", a: "v4"])
%{a: ["v1", "v2", "v4"], b: "v3"}
"""
@spec group_by_keys(list) :: map
def group_by_keys([]), do: %{}
def group_by_keys(args) do
args
|> Enum.map(fn {k, v} -> %{k => v} end)
|> Enum.reduce(fn m, acc ->
Map.merge(m, acc, fn _k, v1, v2 ->
List.wrap(v2) ++ List.wrap(v1)
end)
end)
end
end