SlideShare a Scribd company logo
1 of 33
Download to read offline
Introduction to Elixir and
Phoenix
Created by Ridwan Fadjar Septian,
At SenseHealth B.V.
Bandung, 26th September 2018
Disclaimer
- I am just curious about Elixir and Phoenix Web Framework for personal usage only
- Currently I don’t have an experience using Elixir in production
- I bought Elixir and Phoenix course on Udemy for my hobby
- Alam embrace me to learn Elixir and set up a sharing session
Background and Motivation
Why Elixir?
Elixir at Glance
Fundamental
Hello world
iex > IO.puts("hello world!")
hello world!
:ok
Data Type
iex> 255
255
iex> 0b0110
6
iex> 0o644
420
iex> 0x1F
31
iex> 3.14
3.14
iex> 1.0e-10
1.0e-10
iex> true
true
iex> false
False
Data Type
iex> :foo
:foo
iex> :foo == :bar
false
iex> "Hello"
"Hello"
iex> "さようなら"
"さようなら"
iex> "foo
...> bar"
"foonbar"
iex> "foonbar"
"foonbar"
Basic Operator
iex> 2 + 2
4
iex> 2 - 1
1
iex> 2 * 5
10
iex> 10 / 5
2.0
iex> div(10, 5)
2
iex> rem(10, 3)
1
Basic Operator
iex> -20 || true
-20
iex> false || 42
42
iex> 42 && true
true
iex> 42 && nil
nil
iex> !42
false
iex> !false
true
Basic Operator
iex> true and 42
42
iex> false or true
true
iex> not false
true
iex> 1 > 2
false
iex> 1 != 2
true
iex> 2 == 2
true
iex> 2 <= 3
true
iex> 2 == 2.0
true
iex> 2 === 2.0
false
Basic Operator
iex> "Elixir rocks" |> String.upcase() |> String.split()
["ELIXIR", "ROCKS"]
Conditional
iex> if String.valid?("Hello") do
...> "Valid string!"
...> else
...> "Invalid string."
...> end
"Valid string!"
iex> if "a string value" do
...> "Truthy"
...> end
"Truthy"
Conditional
iex> unless is_integer("hello") do
...> "Not an Int"
...> end
"Not an Int"
Conditional
iex> case {:ok, "Hello World"} do
...> {:ok, result} -> result
...> {:error} -> "Uh oh!"
...> _ -> "Catch all"
...> end
"Hello World"
Conditional
iex> cond do
...> 2 + 2 == 5 ->
...> "This will not be true"
...> 2 * 2 == 3 ->
...> "Nor this"
...> 1 + 1 == 2 ->
...> "But this will"
...> end
"But this will"
Recursion
defmodule Loop do
def print_multiple_times
(msg, n) when n <= 1 do
IO.puts msg
end
def print_multiple_times
(msg, n) do
IO.puts msg
print_multiple_times
(msg, n - 1)
end
end
Loop.print_multiple_times
("Hello", 10)
Function
iex> sum = fn (a, b) -> a + b end
iex> sum.(2, 3)
5
iex> sum = &(&1 + &2)
iex> sum.(2, 3)
5
Function and Module
defmodule Greeter do
def hello(name) do
"Hello, " <> name
end
end
iex> Greeter.hello("Sean")
"Hello, Sean"
Function and Module
defmodule Greeter2 do
def hello(), do: "Hello, anonymous person!" # hello/0
def hello(name), do: "Hello, " <> name # hello/1
def hello(name1, name2), do: "Hello, #{name1} and #{name2}"
# hello/2
end
iex> Greeter2.hello()
"Hello, anonymous person!"
iex> Greeter2.hello("Fred")
"Hello, Fred"
iex> Greeter2.hello("Fred", "Jane")
"Hello, Fred and Jane"
Mix
Package Manager in Elixir
Phoenix
Web Framework for Elixir
Getting Started
$ mix archive.install
https://github.com/phoenixframework/archives/raw/master/phx_new.ez
$ mix phx.new tracking-api --app my_app --module TrackingAPI --no-brunch --no-html
$ cd tracking-api
$ mix phx.server
Supported Database
- PostgreSQL
- MySQL
Configs
use Mix.Config
config :tracking_api, TrackingAPIWeb.Endpoint,
http: [port: 4000],
debug_errors: false,
code_reloader: true,
check_origin: false,
watchers: []
config :logger, :console, format: "[$level] $messagen"
config :phoenix, :stacktrace_depth, 20
config :tracking_api, TrackingAPI.Repo,
adapter: Ecto.Adapters.Postgres,
username: "phoenix",
password: "phoenix",
database: "tracking_api_dev",
hostname: "localhost",
pool_size: 10
Models
schema "gps_tracking" do
field :application_id, Ecto.UUID
field :created_at, :utc_datetime
field :latitude, :string
field :longitude, :string
field :user_id, Ecto.UUID
timestamps()
end
@doc false
def changeset(gps_tracking, attrs) do
gps_tracking
|> cast(attrs, [:application_id, :user_id, :latitude, :longitude, :created_at])
|> validate_required([:application_id, :user_id, :latitude, :longitude, :created_at])
end
Repo
def list_gps_tracking do
Repo.all(GPSTracking)
end
def get_gps_tracking!(id), do: Repo.get!(GPSTracking, id)
def create_gps_tracking(attrs  %{}) do
%GPSTracking{}
|> GPSTracking.changeset(attrs)
|> Repo.insert()
end
def update_gps_tracking(%GPSTracking{} = gps_tracking, attrs) do
gps_tracking
|> GPSTracking.changeset(attrs)
|> Repo.update()
end
def delete_gps_tracking(%GPSTracking{} = gps_tracking) do
Repo.delete(gps_tracking)
end
def change_gps_tracking(%GPSTracking{} = gps_tracking) do
GPSTracking.changeset(gps_tracking, %{})
end
Controllers
defmodule TrackingAPIWeb.GPSTrackingController do
use TrackingAPIWeb, :controller
alias TrackingAPI.Tracking
alias TrackingAPI.Tracking.GPSTracking
action_fallback TrackingAPIWeb.FallbackController
def index(conn, _params) do
gps_tracking = Tracking.list_gps_tracking()
render(conn, "index.json", gps_tracking: gps_tracking)
end
def create(conn, %{"gps_tracking" => gps_tracking_params}) do
with {:ok, %GPSTracking{} = gps_tracking} <- Tracking.create_gps_tracking(gps_tracking_params) do
conn
|> put_status(:created)
|> put_resp_header("location", gps_tracking_path(conn, :show, gps_tracking))
|> render("show.json", gps_tracking: gps_tracking)
end
end
end
We cannot use update endpoint! We exclude it.
Views
def render("index.json", %{gps_tracking: gps_tracking}) do
%{data: render_many(gps_tracking, GPSTrackingView, "gps_tracking.json")}
end
def render("show.json", %{gps_tracking: gps_tracking}) do
%{data: render_one(gps_tracking, GPSTrackingView, "gps_tracking.json")}
end
def render("gps_tracking.json", %{gps_tracking: gps_tracking}) do
%{id: gps_tracking.id,
application_id: gps_tracking.application_id,
user_id: gps_tracking.user_id,
latitude: gps_tracking.latitude,
longitude: gps_tracking.longitude,
created_at: gps_tracking.created_at}
end
Router
defmodule TrackingAPIWeb.Router do
use TrackingAPIWeb, :router
pipeline :api do
plug :accepts, ["json"]
end
scope "/api", TrackingAPIWeb do
pipe_through :api
resources "/gps_tracking", GPSTrackingController, except: [:edit]
end
end
Testing
test "list_gps_tracking/0 returns all gps_tracking" do
gps_tracking = gps_tracking_fixture()
assert Tracking.list_gps_tracking() == [gps_tracking]
end
test "get_gps_tracking!/1 returns the gps_tracking with given id" do
gps_tracking = gps_tracking_fixture()
assert Tracking.get_gps_tracking!(gps_tracking.id) == gps_tracking
end
test "create_gps_tracking/1 with valid data creates a gps_tracking" do
assert {:ok, %GPSTracking{} = gps_tracking} = Tracking.create_gps_tracking(@valid_attrs)
assert gps_tracking.application_id == "7488a646-e31f-11e4-aace-600308960662"
assert gps_tracking.created_at == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC")
assert gps_tracking.latitude == "some latitude"
assert gps_tracking.longitude == "some longitude"
assert gps_tracking.user_id == "7488a646-e31f-11e4-aace-600308960662"
end
test "create_gps_tracking/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Tracking.create_gps_tracking(@invalid_attrs)
end
References
- https://hexdocs.pm/phoenix/
- https://elixirschool.com/
- https://www.tutorialspoint.com/elixir/
- https://elixir-lang.org/docs.html
- https://github.com/h4cc/awesome-elixir
- https://github.com/droptheplot/awesome-phoenix
Demo

More Related Content

Similar to Introduction to Elixir and Phoenix.pdf

Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersAaron Patterson
 
DAHO.AM 2015 - Abusing phones to make the internet of things
DAHO.AM 2015 - Abusing phones to make the internet of thingsDAHO.AM 2015 - Abusing phones to make the internet of things
DAHO.AM 2015 - Abusing phones to make the internet of thingsJan Jongboom
 
The Ring programming language version 1.5.4 book - Part 46 of 185
The Ring programming language version 1.5.4 book - Part 46 of 185The Ring programming language version 1.5.4 book - Part 46 of 185
The Ring programming language version 1.5.4 book - Part 46 of 185Mahmoud Samir Fayed
 
The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010Fabien Potencier
 
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY SyntaxRubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY SyntaxDr Nic Williams
 
Alexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for DevelopersAlexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for DevelopersDevDay Dresden
 
Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"
Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"
Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"Ralf Eggert
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hopeMarcus Ramberg
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRaimonds Simanovskis
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick touraztack
 
A re introduction to webpack - reactfoo - mumbai
A re introduction to webpack - reactfoo - mumbaiA re introduction to webpack - reactfoo - mumbai
A re introduction to webpack - reactfoo - mumbaiPraveen Puglia
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is HumanAlex Liu
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud CastlesBen Scofield
 

Similar to Introduction to Elixir and Phoenix.pdf (20)

Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty Burgers
 
Elixir and OTP Apps introduction
Elixir and OTP Apps introductionElixir and OTP Apps introduction
Elixir and OTP Apps introduction
 
DAHO.AM 2015 - Abusing phones to make the internet of things
DAHO.AM 2015 - Abusing phones to make the internet of thingsDAHO.AM 2015 - Abusing phones to make the internet of things
DAHO.AM 2015 - Abusing phones to make the internet of things
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
The Ring programming language version 1.5.4 book - Part 46 of 185
The Ring programming language version 1.5.4 book - Part 46 of 185The Ring programming language version 1.5.4 book - Part 46 of 185
The Ring programming language version 1.5.4 book - Part 46 of 185
 
Elixir cheatsheet
Elixir cheatsheetElixir cheatsheet
Elixir cheatsheet
 
The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010
 
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY SyntaxRubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
 
Alexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for DevelopersAlexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for Developers
 
Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"
Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"
Drehbuch zum Talk "Rapid Prototyping mit PHP Frameworks"
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
 
A re introduction to webpack - reactfoo - mumbai
A re introduction to webpack - reactfoo - mumbaiA re introduction to webpack - reactfoo - mumbai
A re introduction to webpack - reactfoo - mumbai
 
Play vs Rails
Play vs RailsPlay vs Rails
Play vs Rails
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is Human
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud Castles
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
PhpBB meets Symfony2
PhpBB meets Symfony2PhpBB meets Symfony2
PhpBB meets Symfony2
 
CodeIgniter 3.0
CodeIgniter 3.0CodeIgniter 3.0
CodeIgniter 3.0
 

More from Ridwan Fadjar

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
PyCon ID 2023 - Ridwan Fadjar Septian.pdf
PyCon ID 2023 - Ridwan Fadjar Septian.pdfPyCon ID 2023 - Ridwan Fadjar Septian.pdf
PyCon ID 2023 - Ridwan Fadjar Septian.pdfRidwan Fadjar
 
Cloud Infrastructure automation with Python-3.pdf
Cloud Infrastructure automation with Python-3.pdfCloud Infrastructure automation with Python-3.pdf
Cloud Infrastructure automation with Python-3.pdfRidwan Fadjar
 
GraphQL- Presentation
GraphQL- PresentationGraphQL- Presentation
GraphQL- PresentationRidwan Fadjar
 
Bugs and Where to Find Them (Study Case_ Backend).pdf
Bugs and Where to Find Them (Study Case_ Backend).pdfBugs and Where to Find Them (Study Case_ Backend).pdf
Bugs and Where to Find Them (Study Case_ Backend).pdfRidwan Fadjar
 
Ridwan Fadjar Septian PyCon ID 2021 Regular Talk - django application monitor...
Ridwan Fadjar Septian PyCon ID 2021 Regular Talk - django application monitor...Ridwan Fadjar Septian PyCon ID 2021 Regular Talk - django application monitor...
Ridwan Fadjar Septian PyCon ID 2021 Regular Talk - django application monitor...Ridwan Fadjar
 
CS meetup 2020 - Introduction to DevOps
CS meetup 2020 - Introduction to DevOpsCS meetup 2020 - Introduction to DevOps
CS meetup 2020 - Introduction to DevOpsRidwan Fadjar
 
SenseHealth Indonesia Sharing Session - Do we really need growth mindset (1)
SenseHealth Indonesia Sharing Session - Do we really need growth mindset (1)SenseHealth Indonesia Sharing Session - Do we really need growth mindset (1)
SenseHealth Indonesia Sharing Session - Do we really need growth mindset (1)Ridwan Fadjar
 
Risk Analysis of Dutch Healthcare Company Information System using ISO 27001:...
Risk Analysis of Dutch Healthcare Company Information System using ISO 27001:...Risk Analysis of Dutch Healthcare Company Information System using ISO 27001:...
Risk Analysis of Dutch Healthcare Company Information System using ISO 27001:...Ridwan Fadjar
 
A Study Review of Common Big Data Architecture for Small-Medium Enterprise
A Study Review of Common Big Data Architecture for Small-Medium EnterpriseA Study Review of Common Big Data Architecture for Small-Medium Enterprise
A Study Review of Common Big Data Architecture for Small-Medium EnterpriseRidwan Fadjar
 
Mongodb intro-2-asbasdat-2018-v2
Mongodb intro-2-asbasdat-2018-v2Mongodb intro-2-asbasdat-2018-v2
Mongodb intro-2-asbasdat-2018-v2Ridwan Fadjar
 
Mongodb intro-2-asbasdat-2018
Mongodb intro-2-asbasdat-2018Mongodb intro-2-asbasdat-2018
Mongodb intro-2-asbasdat-2018Ridwan Fadjar
 
Mongodb intro-1-asbasdat-2018
Mongodb intro-1-asbasdat-2018Mongodb intro-1-asbasdat-2018
Mongodb intro-1-asbasdat-2018Ridwan Fadjar
 
Resftul API Web Development with Django Rest Framework & Celery
Resftul API Web Development with Django Rest Framework & CeleryResftul API Web Development with Django Rest Framework & Celery
Resftul API Web Development with Django Rest Framework & CeleryRidwan Fadjar
 
Memulai Data Processing dengan Spark dan Python
Memulai Data Processing dengan Spark dan PythonMemulai Data Processing dengan Spark dan Python
Memulai Data Processing dengan Spark dan PythonRidwan Fadjar
 
Kisah Dua Sejoli: Arduino & Python
Kisah Dua Sejoli: Arduino & PythonKisah Dua Sejoli: Arduino & Python
Kisah Dua Sejoli: Arduino & PythonRidwan Fadjar
 
Mengenal Si Ular Berbisa - Kopi Darat Python Bandung Desember 2014
Mengenal Si Ular Berbisa - Kopi Darat Python Bandung Desember 2014Mengenal Si Ular Berbisa - Kopi Darat Python Bandung Desember 2014
Mengenal Si Ular Berbisa - Kopi Darat Python Bandung Desember 2014Ridwan Fadjar
 
Modul pelatihan-django-dasar-possupi-v1
Modul pelatihan-django-dasar-possupi-v1Modul pelatihan-django-dasar-possupi-v1
Modul pelatihan-django-dasar-possupi-v1Ridwan Fadjar
 
Membuat game-shooting-dengan-pygame
Membuat game-shooting-dengan-pygameMembuat game-shooting-dengan-pygame
Membuat game-shooting-dengan-pygameRidwan Fadjar
 

More from Ridwan Fadjar (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
PyCon ID 2023 - Ridwan Fadjar Septian.pdf
PyCon ID 2023 - Ridwan Fadjar Septian.pdfPyCon ID 2023 - Ridwan Fadjar Septian.pdf
PyCon ID 2023 - Ridwan Fadjar Septian.pdf
 
Cloud Infrastructure automation with Python-3.pdf
Cloud Infrastructure automation with Python-3.pdfCloud Infrastructure automation with Python-3.pdf
Cloud Infrastructure automation with Python-3.pdf
 
GraphQL- Presentation
GraphQL- PresentationGraphQL- Presentation
GraphQL- Presentation
 
Bugs and Where to Find Them (Study Case_ Backend).pdf
Bugs and Where to Find Them (Study Case_ Backend).pdfBugs and Where to Find Them (Study Case_ Backend).pdf
Bugs and Where to Find Them (Study Case_ Backend).pdf
 
Ridwan Fadjar Septian PyCon ID 2021 Regular Talk - django application monitor...
Ridwan Fadjar Septian PyCon ID 2021 Regular Talk - django application monitor...Ridwan Fadjar Septian PyCon ID 2021 Regular Talk - django application monitor...
Ridwan Fadjar Septian PyCon ID 2021 Regular Talk - django application monitor...
 
CS meetup 2020 - Introduction to DevOps
CS meetup 2020 - Introduction to DevOpsCS meetup 2020 - Introduction to DevOps
CS meetup 2020 - Introduction to DevOps
 
Why Serverless?
Why Serverless?Why Serverless?
Why Serverless?
 
SenseHealth Indonesia Sharing Session - Do we really need growth mindset (1)
SenseHealth Indonesia Sharing Session - Do we really need growth mindset (1)SenseHealth Indonesia Sharing Session - Do we really need growth mindset (1)
SenseHealth Indonesia Sharing Session - Do we really need growth mindset (1)
 
Risk Analysis of Dutch Healthcare Company Information System using ISO 27001:...
Risk Analysis of Dutch Healthcare Company Information System using ISO 27001:...Risk Analysis of Dutch Healthcare Company Information System using ISO 27001:...
Risk Analysis of Dutch Healthcare Company Information System using ISO 27001:...
 
A Study Review of Common Big Data Architecture for Small-Medium Enterprise
A Study Review of Common Big Data Architecture for Small-Medium EnterpriseA Study Review of Common Big Data Architecture for Small-Medium Enterprise
A Study Review of Common Big Data Architecture for Small-Medium Enterprise
 
Mongodb intro-2-asbasdat-2018-v2
Mongodb intro-2-asbasdat-2018-v2Mongodb intro-2-asbasdat-2018-v2
Mongodb intro-2-asbasdat-2018-v2
 
Mongodb intro-2-asbasdat-2018
Mongodb intro-2-asbasdat-2018Mongodb intro-2-asbasdat-2018
Mongodb intro-2-asbasdat-2018
 
Mongodb intro-1-asbasdat-2018
Mongodb intro-1-asbasdat-2018Mongodb intro-1-asbasdat-2018
Mongodb intro-1-asbasdat-2018
 
Resftul API Web Development with Django Rest Framework & Celery
Resftul API Web Development with Django Rest Framework & CeleryResftul API Web Development with Django Rest Framework & Celery
Resftul API Web Development with Django Rest Framework & Celery
 
Memulai Data Processing dengan Spark dan Python
Memulai Data Processing dengan Spark dan PythonMemulai Data Processing dengan Spark dan Python
Memulai Data Processing dengan Spark dan Python
 
Kisah Dua Sejoli: Arduino & Python
Kisah Dua Sejoli: Arduino & PythonKisah Dua Sejoli: Arduino & Python
Kisah Dua Sejoli: Arduino & Python
 
Mengenal Si Ular Berbisa - Kopi Darat Python Bandung Desember 2014
Mengenal Si Ular Berbisa - Kopi Darat Python Bandung Desember 2014Mengenal Si Ular Berbisa - Kopi Darat Python Bandung Desember 2014
Mengenal Si Ular Berbisa - Kopi Darat Python Bandung Desember 2014
 
Modul pelatihan-django-dasar-possupi-v1
Modul pelatihan-django-dasar-possupi-v1Modul pelatihan-django-dasar-possupi-v1
Modul pelatihan-django-dasar-possupi-v1
 
Membuat game-shooting-dengan-pygame
Membuat game-shooting-dengan-pygameMembuat game-shooting-dengan-pygame
Membuat game-shooting-dengan-pygame
 

Recently uploaded

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 

Recently uploaded (20)

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 

Introduction to Elixir and Phoenix.pdf

  • 1. Introduction to Elixir and Phoenix Created by Ridwan Fadjar Septian, At SenseHealth B.V. Bandung, 26th September 2018
  • 2. Disclaimer - I am just curious about Elixir and Phoenix Web Framework for personal usage only - Currently I don’t have an experience using Elixir in production - I bought Elixir and Phoenix course on Udemy for my hobby - Alam embrace me to learn Elixir and set up a sharing session
  • 5. Hello world iex > IO.puts("hello world!") hello world! :ok
  • 6. Data Type iex> 255 255 iex> 0b0110 6 iex> 0o644 420 iex> 0x1F 31 iex> 3.14 3.14 iex> 1.0e-10 1.0e-10 iex> true true iex> false False
  • 7. Data Type iex> :foo :foo iex> :foo == :bar false iex> "Hello" "Hello" iex> "さようなら" "さようなら" iex> "foo ...> bar" "foonbar" iex> "foonbar" "foonbar"
  • 8. Basic Operator iex> 2 + 2 4 iex> 2 - 1 1 iex> 2 * 5 10 iex> 10 / 5 2.0 iex> div(10, 5) 2 iex> rem(10, 3) 1
  • 9. Basic Operator iex> -20 || true -20 iex> false || 42 42 iex> 42 && true true iex> 42 && nil nil iex> !42 false iex> !false true
  • 10. Basic Operator iex> true and 42 42 iex> false or true true iex> not false true iex> 1 > 2 false iex> 1 != 2 true iex> 2 == 2 true iex> 2 <= 3 true iex> 2 == 2.0 true iex> 2 === 2.0 false
  • 11. Basic Operator iex> "Elixir rocks" |> String.upcase() |> String.split() ["ELIXIR", "ROCKS"]
  • 12. Conditional iex> if String.valid?("Hello") do ...> "Valid string!" ...> else ...> "Invalid string." ...> end "Valid string!" iex> if "a string value" do ...> "Truthy" ...> end "Truthy"
  • 13. Conditional iex> unless is_integer("hello") do ...> "Not an Int" ...> end "Not an Int"
  • 14. Conditional iex> case {:ok, "Hello World"} do ...> {:ok, result} -> result ...> {:error} -> "Uh oh!" ...> _ -> "Catch all" ...> end "Hello World"
  • 15. Conditional iex> cond do ...> 2 + 2 == 5 -> ...> "This will not be true" ...> 2 * 2 == 3 -> ...> "Nor this" ...> 1 + 1 == 2 -> ...> "But this will" ...> end "But this will"
  • 16. Recursion defmodule Loop do def print_multiple_times (msg, n) when n <= 1 do IO.puts msg end def print_multiple_times (msg, n) do IO.puts msg print_multiple_times (msg, n - 1) end end Loop.print_multiple_times ("Hello", 10)
  • 17. Function iex> sum = fn (a, b) -> a + b end iex> sum.(2, 3) 5 iex> sum = &(&1 + &2) iex> sum.(2, 3) 5
  • 18. Function and Module defmodule Greeter do def hello(name) do "Hello, " <> name end end iex> Greeter.hello("Sean") "Hello, Sean"
  • 19. Function and Module defmodule Greeter2 do def hello(), do: "Hello, anonymous person!" # hello/0 def hello(name), do: "Hello, " <> name # hello/1 def hello(name1, name2), do: "Hello, #{name1} and #{name2}" # hello/2 end iex> Greeter2.hello() "Hello, anonymous person!" iex> Greeter2.hello("Fred") "Hello, Fred" iex> Greeter2.hello("Fred", "Jane") "Hello, Fred and Jane"
  • 22. Getting Started $ mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez $ mix phx.new tracking-api --app my_app --module TrackingAPI --no-brunch --no-html $ cd tracking-api $ mix phx.server
  • 24. Configs use Mix.Config config :tracking_api, TrackingAPIWeb.Endpoint, http: [port: 4000], debug_errors: false, code_reloader: true, check_origin: false, watchers: [] config :logger, :console, format: "[$level] $messagen" config :phoenix, :stacktrace_depth, 20 config :tracking_api, TrackingAPI.Repo, adapter: Ecto.Adapters.Postgres, username: "phoenix", password: "phoenix", database: "tracking_api_dev", hostname: "localhost", pool_size: 10
  • 25. Models schema "gps_tracking" do field :application_id, Ecto.UUID field :created_at, :utc_datetime field :latitude, :string field :longitude, :string field :user_id, Ecto.UUID timestamps() end @doc false def changeset(gps_tracking, attrs) do gps_tracking |> cast(attrs, [:application_id, :user_id, :latitude, :longitude, :created_at]) |> validate_required([:application_id, :user_id, :latitude, :longitude, :created_at]) end
  • 26. Repo def list_gps_tracking do Repo.all(GPSTracking) end def get_gps_tracking!(id), do: Repo.get!(GPSTracking, id) def create_gps_tracking(attrs %{}) do %GPSTracking{} |> GPSTracking.changeset(attrs) |> Repo.insert() end def update_gps_tracking(%GPSTracking{} = gps_tracking, attrs) do gps_tracking |> GPSTracking.changeset(attrs) |> Repo.update() end def delete_gps_tracking(%GPSTracking{} = gps_tracking) do Repo.delete(gps_tracking) end def change_gps_tracking(%GPSTracking{} = gps_tracking) do GPSTracking.changeset(gps_tracking, %{}) end
  • 27. Controllers defmodule TrackingAPIWeb.GPSTrackingController do use TrackingAPIWeb, :controller alias TrackingAPI.Tracking alias TrackingAPI.Tracking.GPSTracking action_fallback TrackingAPIWeb.FallbackController def index(conn, _params) do gps_tracking = Tracking.list_gps_tracking() render(conn, "index.json", gps_tracking: gps_tracking) end def create(conn, %{"gps_tracking" => gps_tracking_params}) do with {:ok, %GPSTracking{} = gps_tracking} <- Tracking.create_gps_tracking(gps_tracking_params) do conn |> put_status(:created) |> put_resp_header("location", gps_tracking_path(conn, :show, gps_tracking)) |> render("show.json", gps_tracking: gps_tracking) end end end
  • 28. We cannot use update endpoint! We exclude it.
  • 29. Views def render("index.json", %{gps_tracking: gps_tracking}) do %{data: render_many(gps_tracking, GPSTrackingView, "gps_tracking.json")} end def render("show.json", %{gps_tracking: gps_tracking}) do %{data: render_one(gps_tracking, GPSTrackingView, "gps_tracking.json")} end def render("gps_tracking.json", %{gps_tracking: gps_tracking}) do %{id: gps_tracking.id, application_id: gps_tracking.application_id, user_id: gps_tracking.user_id, latitude: gps_tracking.latitude, longitude: gps_tracking.longitude, created_at: gps_tracking.created_at} end
  • 30. Router defmodule TrackingAPIWeb.Router do use TrackingAPIWeb, :router pipeline :api do plug :accepts, ["json"] end scope "/api", TrackingAPIWeb do pipe_through :api resources "/gps_tracking", GPSTrackingController, except: [:edit] end end
  • 31. Testing test "list_gps_tracking/0 returns all gps_tracking" do gps_tracking = gps_tracking_fixture() assert Tracking.list_gps_tracking() == [gps_tracking] end test "get_gps_tracking!/1 returns the gps_tracking with given id" do gps_tracking = gps_tracking_fixture() assert Tracking.get_gps_tracking!(gps_tracking.id) == gps_tracking end test "create_gps_tracking/1 with valid data creates a gps_tracking" do assert {:ok, %GPSTracking{} = gps_tracking} = Tracking.create_gps_tracking(@valid_attrs) assert gps_tracking.application_id == "7488a646-e31f-11e4-aace-600308960662" assert gps_tracking.created_at == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC") assert gps_tracking.latitude == "some latitude" assert gps_tracking.longitude == "some longitude" assert gps_tracking.user_id == "7488a646-e31f-11e4-aace-600308960662" end test "create_gps_tracking/1 with invalid data returns error changeset" do assert {:error, %Ecto.Changeset{}} = Tracking.create_gps_tracking(@invalid_attrs) end
  • 32. References - https://hexdocs.pm/phoenix/ - https://elixirschool.com/ - https://www.tutorialspoint.com/elixir/ - https://elixir-lang.org/docs.html - https://github.com/h4cc/awesome-elixir - https://github.com/droptheplot/awesome-phoenix
  • 33. Demo