ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
REST API with Elixir
with Maru
@kohei_kimura
@kohei_kimura
? IDC Frontier Inc.
Developing IoT Platform
Back-end on the behavior analysis project
@kohei_kimura
kohei-kimura
kohei-kimura.github.io
? Visual Studio Code
Kitakyushu -> Yokohama -> Fukuoka
IDCF Cloud - One coin IaaS
Why I love Elixir
|> pattern matching
|> processes
|> Ruby-like
|> mix
Today¡¯s topic:
|> REST-like API framework
|> inspired by grape (Ruby)
|> running on other web frameworks like Phenix
|> routing HTTP requests
|> parameter validation and coercion
|> versioning
NOT do?
|> render template
|> database connection
|> plug wrappers like session, header and so
on
What does
Getting Started
# mix.exs
defp deps do
[ {:maru, "~> 0.11"} ]
end
def application do
[ applications: [:maru] ]
end
$ mix deps.get
$ iex -S mix
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
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
Summary
|> REST-like API framework
|> alone or on Phoenix
|> routing, parameter , versioning
|> easy to write REST API
|> micro framework
|> e.g. API GW
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
Enjoy your Elixir programming!
Please follow me! ->

More Related Content

REST API with Elixir with Maru

  • 1. REST API with Elixir with Maru @kohei_kimura
  • 2. @kohei_kimura ? IDC Frontier Inc. Developing IoT Platform Back-end on the behavior analysis project @kohei_kimura kohei-kimura kohei-kimura.github.io ? Visual Studio Code Kitakyushu -> Yokohama -> Fukuoka
  • 3. IDCF Cloud - One coin IaaS
  • 4. Why I love Elixir |> pattern matching |> processes |> Ruby-like |> mix
  • 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
  • 13. Enjoy your Elixir programming! Please follow me! ->