Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Error do | |
defstruct code: nil, message: nil, details: nil | |
@doc """ | |
Creates a new error struct | |
## Usage: | |
iex> Error.new("E001", "This is an error") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ExternalUser do | |
use Ecto.Schema | |
import Ecto.Changeset | |
@primary_key false | |
embedded_schema do | |
field(:first_name, :string) | |
field(:last_name, :string) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ExternalAPIRouter do | |
use Phoenix.Router | |
import Plug.Conn | |
import Phoenix.Controller | |
scope "/api/v1" do | |
get("me2", __MODULE__, :me) | |
get("product/:product_id", __MODULE__, :product_detail) | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule UserMapperFunctions do | |
def map_my_address(%{ | |
address: address, | |
city: city, | |
province: province, | |
country: country, | |
postalCode: postalCode | |
}) do | |
"#{address}, #{city}, #{province}, #{country}, #{postalCode}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shopping_cart = [ | |
{"item": "Apple", "price": 0.5, "quantity": 10}, | |
{"item": "Milk", "price": 1.5, "quantity": 2}, | |
{"item": "Bread", "price": 2.0, "quantity": 1} | |
] | |
def item_cost(item): | |
return item["price"] * item["quantity"] | |
sum(map(item_cost, shopping_cart)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ServiceSwitcher do | |
defmacro __using__(opts \\ []) do | |
service_module_mapping = opts[:service_module_mapping] | |
quote do | |
@service_module_mapping unquote(service_module_mapping) | |
@services Keyword.keys(@service_module_mapping) | |
def get_service_module(service) when is_binary(service), | |
do: service |> String.to_atom() |> get_service_module() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule T do | |
import Ecto.Query | |
alias MyApp.Repo | |
def test() do | |
from(s in "users", select: [:id]) |> Repo.one() | |
rescue | |
Ecto.MultipleResultsError -> IO.puts("Sorry. Things didn't go well.") | |
end | |
end |
NewerOlder