Oasis.Controller (oasis v0.5.2)

View Source

Base on Plug.Builder, this module can be use-d into a module in order to build a plug pipeline:

defmodule MyApp.HelloController do
  use Oasis.Controller

  plug(Plug.Parsers,
    parsers: [:urlencoded],
    pass: ["*/*"]
  )

  def call(conn, opts) do
    conn = super(conn, opts)
    json(conn, %{"body_params" => conn.body_params})
  end
end

And provide some common functionality for easily use, this realization comes from Phoenix.Controller.

Summary

Functions

Sends html response.

Sends JSON response.

Returns the router module as an atom, raises if unavailable.

Sends text response.

Functions

html(conn, data)

@spec html(Plug.Conn.t(), iodata()) :: Plug.Conn.t()

Sends html response.

Examples

iex> html(conn, "<html>...</html>")

json(conn, data)

@spec json(Plug.Conn.t(), term()) :: Plug.Conn.t()

Sends JSON response.

It uses Jason to encode the input as an iodata data.

Examples

iex> json(conn, %{id: 1})

router_module(conn)

@spec router_module(Plug.Conn.t()) :: atom()

Returns the router module as an atom, raises if unavailable.

text(conn, data)

@spec text(Plug.Conn.t(), String.Chars.t()) :: Plug.Conn.t()

Sends text response.

Examples

iex> text(conn, "hello")

iex> text(conn, :implements_to_string)