Team Oblivion
** Things might have changed
Elixir 🔗
Phoenix Framework 🔗
Vagrant 🔗
mix new awesome_project --module AwesomeProject
Mix is a build tool, run tasks and tests
mix compile
// after compile
iex -S mix
Create an iex session (Elixir’s interactive shell)
Learn more about mix 🔗
Thinking functionally
Language and syntax
Main benefits
vagrant up
vagrant ssh
Phoenix docs 🔗
mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
pipeline :search do
plug :accepts, ["json"]
plug XSearchApi.CORS # plug: connection lifecycle
end
scope "/", XSearchApi do # group similar routes together
pipe_through :search
get "/category/count", SearchController, :category_search_count
end
# eg scope "/admin" ...
defmodule XSearchApi.SearchController do
use XSearchApi.Web, :controller
def category_search_count(conn, params) do
valid_api_params = get_valid_api_params(params)
safe_result_link = get_safe_result_link(conn, params)
get_response(conn, valid_api_params, safe_result_link)
end
defp get_response(conn, {:ok, valid_api_params}, {:ok, safe_result_link}) do
{_status, response} = XSearchApi.SAPI.get(
"records/record", [], [params: valid_api_params])
conn
|> put_status(response.status_code)
|> json(handle_response(response, safe_result_link))
end
...
defmodule XSearchApi.SAPI do
use HTTPoison.Base
def get(_url, [], [{:params, params}]) when is_nil(params), do:
{:error, %XSearchApi.Error { reason: "Bad request", status_code: 400 }}
def get(url, [], [{:params, params}]) do
{status, response} = request(:get, url, "", [], [{:params, params}])
set_response(status, response)
end
...
@year_fields ["YearOfBirth", "YearOfDeath"]
@year_offset "_offset"
def append_year_offset([[year_field, :eq, value]|tail])
when year_field in @year_fields do
exact_year_phrase = [year_field, :eq, value]
Enum.concat(
[exact_year_phrase, :and, ["#{year_field}#{@year_offset}", :eq, "0"]],
append_year_offset(tail))
end
Cool piece of code :D
more on upstart 🔗
# Register
mix hex.user register
# Publish package
mix hex.publish
defmodule XSearchApi.SearchControllerTest do
use ExUnit.Case
use Phoenix.ConnTest
import Mock
test "should pass odata query to sapi ..."
do
with_mock XSearchApi.SAPI,
[get: fn() -> {:ok, %XSearchApi.Response{ ... } end]
do
conn_response = SearchController
.category_search_count(
conn, %{"$filter" => "LastName eq Kotze"}
)
assert called SAPI.get("/record", [params: %{"$filter" => "LastName eq 'Kotze'"}])
assert conn_response.status == 200
end
end
...
more on white_bread 🔗
given_ ~r/^I am a third party consumer$/, fn state ->
{:ok, state}
end
when_ ~r/I make a request to the API .../, fn state, %{first_name: first_name} ->
encoded_json = get(
put_req_header(conn(), "partnerid", "0123"),
"/category/count?$filter=FirstName eq #{first_name}")
{:ok, state |> Dict.put(:response, encoded_json)}
end
then_ ~r/I expect the response to be a link .../, fn state, %{link_first_name: first_name} ->
response = state |> Dict.get(:response)
decode_body = json_response(response, 200)
decoded_uri = decode_body["ResultUrl"]
assert decoded_uri == "#{@search_link_base}FirstName=#{first_name}"
{:ok, state}
end
config :eye_drops,
tasks: [
%{
id: :unit_tests,
name: "Unit tests",
cmd: "ssh -F .ssh-config default mix test",
paths: ["web/*", "test/*"]
}
]
Vagrant tip