Current section

Files

Jump to
ping_plug lib ping_plug.ex
Raw

lib/ping_plug.ex

defmodule PingPlug do
@moduledoc """
A PingPlug module to echo defined message back.
"""
import Plug.Conn
@spec defaults() :: Keyword.t
def defaults do
[
message: "pong",
content_type: "text/plain",
]
end
@spec init(Keyword.t) :: Keyword.t
def init(options) do
defaults
|> Keyword.merge(options)
|> sanitize_options()
end
@spec call(Plug.Conn.t, Keyword.t) :: Plug.Conn.t | no_return
def call(%Plug.Conn{} = conn, options) do
conn
|> put_resp_content_type(options[:content_type])
|> send_resp(200, options[:message])
end
defp sanitize_options(options) do
[
message: to_string(options[:message]),
content_type: options[:content_type],
]
end
end