Brown Bag Beers Elixir Workshop
Goal: Get some hands on time playing with Elixir and OTP.
Things to try (pick one!)
0. Generating a new supervised app with mix.
Covers:
- mix
Goal: Get some hands on time playing with Elixir and OTP.
Covers:
defmodule Authenticate do | |
import Plug.Conn | |
def call(conn, _opts) do | |
case get_req_header(conn, "authorization") do | |
["Bearer " <> token] -> authenticate(conn, token) | |
_ -> reject(conn, "Bearer token missing") | |
end | |
end |
conn | |
|> set_current_user | |
|> parse_json_body | |
|> create_model | |
|> set_exp_headers |
defmodule App.Models.Address do | |
defstruct [:address, :address2, :city, :region, :postal, :country] | |
defmodule Type do | |
@behaviour Ecto.Type | |
alias App.Models.Address | |
def type, do: :json | |
def cast(addrs) when is_list(addrs) do |
defmodule App.Models.Settings do | |
defstruct [ | |
newsletter: false, | |
publish_profile: true, | |
email_notifications: true | |
] | |
defmodule Type do | |
@behaviour Ecto.Type | |
alias App.Models.Settings |
defmodule App.Models.User do | |
use Ecto.Model | |
schema "users" do | |
field :email, :string | |
field :crypted_pass, :string | |
field :settings, App.Models.Settings.Type | |
field :addresses, App.Models.Address.Type | |
timestamps |
# Generated with mix ecto.gen.migration add_user_settings_and_address | |
defmodule App.Repo.Migrations.AddUserSettingsAndAddress do | |
use Ecto.Migration | |
def change do | |
alter table(:users) do | |
add :settings, :json | |
add :addresses, :json | |
end | |
end |
defmodule Extensions.JSON do | |
alias Postgrex.TypeInfo | |
@behaviour Postgrex.Extension | |
@json ["json", "jsonb"] | |
def init(_parameters, opts), | |
do: Keyword.fetch!(opts, :library) | |
def matching(_library), |
# config/config.ex | |
config :app, App.Repo, | |
database: "my_app_dev", | |
username: "postgres", | |
password: "nope", | |
hostname: "localhost", | |
extensions: [{Extensions.Json, library: Poison}] |
defmodule User do | |
use Ecto.Model | |
schema "users" do | |
field :email, :string | |
field :hashed_password, :string | |
field :password, :string, virtual: true | |
field :password_confirmation, virtual: true | |
timestamps |