This document discusses Maru, a REST API framework for Elixir. Maru allows developers to build REST-like APIs that can run on top of other web frameworks like Phoenix. It handles routing HTTP requests and parameter validation/coercion. Maru is a micro framework that makes it easy to write REST APIs, and can be used for applications like API gateways. An example is provided showing how to return dummy user data from a sample Maru API using routing, parameters, and JSON responses.
6. |> REST-like API framework
|> inspired by grape (Ruby)
|> running on other web frameworks like Phenix
|> routing HTTP requests
|> parameter validation and coercion
|> versioning
7. NOT do?
|> render template
|> database connection
|> plug wrappers like session, header and so
on
What does
8. Getting Started
# mix.exs
defp deps do
[ {:maru, "~> 0.11"} ]
end
def application do
[ applications: [:maru] ]
end
$ mix deps.get
$ iex -S mix
9. Dummily
|> Maru Sample Code
|> Returning dummy users data with JSON
|> kohei-kimura/dummily
case uri do
¡°/users¡± -> All users data
¡°/users/1¡± -> ID = 1 user¡¯s data
¡°/users?username=john¡± -> including ¡°john¡±
end
10. Dummily
defmodule Dummily do
defmodule Dummily.Router.Users do
use Maru.Router
namespace :users do
version "v1" do
params do
optional :username, type: String
optional :email, type: String
optional :posts, type: Integer
optional :sort, type: Atom, values: [:username, :id, :posts], default:
:id
end
get do
users = all_users() |> filter(params) |> sort_by(params[:sort])
conn |> put_status(200) |> json(users)
end
route_param :id, type: Integer do
get do
users = all_users() |> filter(params)
conn |> put_status(200) |> json(users)
end
end
end
end
end
11. Summary
|> REST-like API framework
|> alone or on Phoenix
|> routing, parameter , versioning
|> easy to write REST API
|> micro framework
|> e.g. API GW
12. Links
Awesome Elixir - github.com/h4cc/awesome-elixir
|> Elixir and Erlang libraries, resources and shiny
things
Elixir School - elixirschool.com
|> Inspired by Twitter¡¯s Scala School
Elixir org - elixir-lang.org/docs
|> Of?cial docs