Packages
eview
0.9.7
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.ex
# TODO: validate headers and other non-json fields
defmodule EView do
@moduledoc """
This plug will take `body_params` from Plug.Conn structure
and re-render it with appropriate structure to `resp_body`.
What is appropriate structure? Take look at [Nebo #15 API Manifest](http://docs.apimanifest.apiary.io/#).
# Example:
Add to your `endpoint.ex` before Plug.Parsers plug:
plug EView
plug EView.Plugs.Idempotency
"""
@behaviour Plug
import Plug.Conn
def init(options), do: options
@spec call(Conn.t, any) :: Conn.t
def call(conn, _options) do
conn
|> register_before_send(&update_reponse_body/1)
end
defp update_reponse_body(%{resp_body: []} = conn),
do: conn
defp update_reponse_body(%{resp_body: ""} = conn),
do: conn
defp update_reponse_body(%{resp_body: nil} = conn),
do: conn
defp update_reponse_body(%{resp_body: resp_body} = conn) do
resp = resp_body
|> Poison.Parser.parse!
|> wrap_body(conn)
|> Poison.encode_to_iodata!
%{conn | resp_body: resp}
end
@doc """
Update `body` and add all meta objects that is required by API Manifest structure.
"""
def wrap_body(body, %Plug.Conn{} = conn) when is_map(body) or is_list(body) do
body
|> EView.Renders.Root.render(conn)
end
end