Share
Embed
Phoenix & Ember Living Together - Jacksonville 11/10/2015
How DockYard is working with Phoenix and Ember
Brian Cardarella
November 12, 2015
Transcript
106 slides
-
priv !"" static #"" css $ #"" app.css $ !""
app.css.map #"" images $ #"" favicon.ico $ !"" phoenix.png #"" js $ #"" app.js $ !"" app.js.map !"" robots.txt -
priv !"" static #"" css $ #"" app.css $ !""
app.css.map #"" images $ #"" favicon.ico $ !"" phoenix.png #"" js $ #"" app.js $ !"" app.js.map !"" robots.txt -
* standardized schema for JSON based APIs * is very
verbose * requires special MIME type -
defmodule MyApp.Router do pipeline :api do accepts, [“json-api”] end scope
“/api” MyApp do pipe_through :api resources “/accounts”, AccountsController end end -
Copied from plug’s documentation # https://github.com/elixir-lang/plug/tree/master/lib/plug/mime.ex Maps MIME types to
file extensions and vice versa. MIME types can be extended in your application configuration as follows: config :plug, :mimes, %{ "application/vnd.api+json" => ["json-api"] } After adding the configuration, Plug needs to be recompiled. If you are using mix, it can be done with: $ touch deps/plug/mix.exs $ mix deps.compile plug -
Copied from plug’s documentation # config/config.exs config :plug, :mimes, %{
"application/vnd.api+json" => ["json-api"] } Add the MIME type to your Phoenix config -
Copied from plug’s documentation $ touch deps/plug/mix.exs $ mix deps.compile
plug Force plug to recompile -
{ "data": { "type": "authors", "attributes": { "first-name": "Brian", "last-name":
"Cardarella" } } } Deserialize into snake case -
defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method:
"POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end -
defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method:
"POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end -
defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method:
"POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end -
defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method:
"POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end -
defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method:
"POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end -
defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method:
"POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end -
defmodule MyApp.Router do pipeline :api do accepts, [“json-api”] end scope
“/api” MyApp do pipe_through :api resources “/accounts”, AccountsController end end -
defmodule MyApp.Router do pipeline :api do accepts, [“json-api”] plug MyApp.Deserialize
end scope “/api” MyApp do pipe_through :api resources “/accounts”, AccountsController end end -
%{ “data" => %{ “type" => "authors", “attributes" => %{
“first_name” => "Brian", “last_name” => "Cardarella" } } } -
%{ “data" => %{ “type" => "authors", “attributes" => %{
“first_name” => "Brian", “last_name” => "Cardarella" } } } -
%{ “data" => %{ “type" => "authors", “attributes" => %{
“first_name” => "Brian", “last_name” => "Cardarella" } } } -
%{ “data" => %{ “type" => "authors", “attributes" => %{
“first_name” => "Brian", “last_name” => "Cardarella" } } } -
Actions def create(conn, %{“data” => %{“attributes” => data, “type” =>
“authors”}}) def post(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def put(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) -
Actions def create(conn, %{“data” => %{“attributes” => data, “type” =>
“authors”}}) def post(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def put(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) -
Actions def create(conn, %{“data” => %{“attributes” => data, “type” =>
“authors”}}) def post(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def put(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def delete(conn, %{“id” => id}) def show(conn, %{“id” => id}) def index(conn, _params) -
Actions def show(conn, %{“id” => id}) do author = MyApp.Repo.get!(MyApp.Author,
id) render conn, model: author end -
View defmodule MyApp.AuthorsView do use PhoenixExample.Web, :view use JaSerializer.PhoenixView attributes
[:first_name, :last_name] end web/views/authors_view.ex -
View defmodule MyApp.AuthorsView do use PhoenixExample.Web, :view use JaSerializer.PhoenixView attributes
[:first_name, :last_name] end web/views/authors_view.ex -
View defmodule MyApp.AuthorsView do use PhoenixExample.Web, :view use JaSerializer.PhoenixView attributes
[:first_name, :last_name] end web/views/authors_view.ex https://github.com/AgilionApps/ja_serializer -
API Unit Tests test "`create` insert into the database and
return payload" do count = MyApp.Repo.all(MyApp.Author) |> length post(conn, authors_path(conn, :create), json_for(:author, @author)) |> json_response(201) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Gizmo"}} }) assert count + 1 == MyApp.Repo.all(MyApp.Author) |> length end -
API Unit Tests test "`create` insert into the database and
return payload" do count = MyApp.Repo.all(MyApp.Author) |> length post(conn, authors_path(conn, :create), json_for(:author, @author)) |> json_response(201) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Gizmo"}} }) assert count + 1 == MyApp.Repo.all(MyApp.Author) |> length end -
%{ “data" => %{ “type" => "authors", “attributes" => %{
“first_name” => "Brian", “last_name” => "Cardarella" } } } -
def json_for(type, attributes) do %{ "data" => %{ "type" =>
Atom.to_string(type) |> String.replace("_", "-"), "attributes" => attributes }, "format" => "json-api" } end def json_for(type, attributes, id) do json = json_for(type, attributes) put_in json["data"]["id"], id end -
def json_for(type, attributes) do %{ "data" => %{ "type" =>
Atom.to_string(type) |> String.replace("_", "-"), "attributes" => attributes }, "format" => "json-api" } end def json_for(type, attributes, id) do json = json_for(type, attributes) put_in json["data"]["id"], id end -
API Unit Tests test "`create` insert into the database and
return payload" do count = MyApp.Repo.all(MyApp.Author) |> length post(conn, authors_path(conn, :create), json_for(:author, @author)) |> json_response(201) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Gizmo"}} }) assert count + 1 == MyApp.Repo.all(MyApp.Author) |> length end https://github.com/danmcclain/voorhees -
API Unit Tests test "`show` an author" do %{authors: %{one:
author}} = fixtures(:authors) get(conn, authors_path(conn, :show, author.id)) |> json_response(200) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Brian"}} }) end -
API Unit Tests test "`show` an author" do %{authors: %{one:
author}} = fixtures(:authors) get(conn, authors_path(conn, :show, author.id)) |> json_response(200) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Brian"}} }) end https://github.com/dockyard/ecto_fixtures -
authors model: MyApp.Author, repo: MyApp.Repo do valid do first_name "George"
last_name "Washington" email "[email protected]" date_of_birth Ecto.Date.from_erl({1990,1,1}) password_hash Comeonin.Bcrypt.hashpwsalt("password") end one inherits: valid do id 1 email "[email protected]" first_name "Leroy" last_name "Jenkins" end end test/fixtures/authors.exs -
Setting Up Data Per Test if Mix.env == :test do
post "/fixtures/start", FixturesController, :start post "/fixtures", FixturesController, :create end -
Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web,
:controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end -
Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web,
:controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end -
Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web,
:controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end -
Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web,
:controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end -
Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web,
:controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end test/support/fixtures_controller.ex -
Calling the fixtures in your Ember Test Suite module(“my awesome
test module”, { setup() { Ember.$.post(“api/fixtures/start”, { async: false }); } }); -
Calling the fixtures in your Ember Test Suite module(“my awesome
test module”, { setup() { Ember.$.post(“api/fixtures/start”, { async: false }); } }); -
Calling the fixtures in your Ember Test Suite test(“my awesome
test”, function(assert) { Ember.$.post(“api/fixtures”, { data: { fixtures: [“foo”, “bar”] }, async: false }); }); -
Calling the fixtures in your Ember Test Suite test(“my awesome
test”, function(assert) { Ember.$.post(“api/fixtures”, { data: { fixtures: [“foo”, “bar”] }, async: false }); }); -
Calling the fixtures in your Ember Test Suite test(“my awesome
test”, function(assert) { Ember.$.post(“api/fixtures”, { data: { fixtures: [“foo”, “bar”] }, async: false }); }); -
Other Topics Not Covered * Running with CI * Speeding
up test suite * Deployment * Testing Email via Ember (that I will briefly cover right now)