Packages
eview
0.5.0
0.16.0
retired
0.15.0
0.14.0
0.13.0
0.12.5
0.12.4
0.12.3
0.12.2
0.12.1
0.12.0
0.11.1
0.11.0
0.10.8
0.10.7
0.10.6
0.10.5
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.7
0.9.6
0.9.5
0.9.4
0.9.3
0.9.2
0.9.0
0.8.2
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.0
0.3.0
0.2.9
0.2.7
0.2.6
0.2.5
0.2.3
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0
Plug that converts response to Nebo #15 API spec format.
Current section
Files
Jump to
Current section
Files
lib/eview/renders/meta_view.ex
defmodule EView.Renders.Meta do
@moduledoc """
This module renders `meta` property from response structure.
"""
import Plug.Conn
@doc """
Renders `meta` view by data_type (may be `object` or `list`) and connection `conn`.
"""
def render(data_type, conn) do
%{
url: get_url(conn),
type: data_type,
code: get_http_status(conn),
request_id: get_request_id(conn)
}
|> put_impotency_key(conn)
end
defp get_url(%Plug.Conn{scheme: scheme, host: host, port: 80, request_path: request_path}) do
Atom.to_string(scheme) <> "://" <> host <> request_path
end
defp get_url(%Plug.Conn{scheme: scheme, host: host, port: port, request_path: request_path}) do
Atom.to_string(scheme) <> "://" <> host <> ":" <> to_string(port) <> request_path
end
defp get_http_status(%Plug.Conn{status: status}) when not is_nil(status), do: status
defp get_http_status(_conn), do: 200
defp get_request_id(conn) do
conn
|> get_resp_header("x-request-id")
|> List.first
end
defp put_impotency_key(meta, %Plug.Conn{} = conn) do
case get_resp_header(conn, "x-idempotency-key") do
[idempotency_key | _] ->
meta
|> Map.put(:idempotency_key, idempotency_key)
_ ->
meta
end
end
end