The Mix Tool¶
Easier to navigate project files.
Facilitates collaboration from other developers on the team.
Using Mix to Create a New project¶
build tool.
mix new budget
The mix run command.
mix run -e "Budget.current_balance(100, 20) |> IO.puts"
What does the mix run command does.
Compiles the budget application.
Loads the generated bytecode into the Erlang Virtual Machine.
Detects the -e option and evaluates the argument as code.
The Third Party Dependencies with Mix Projects¶
mix run -e "Budget.Conversion.from_euro_to_dollar(15) | IO.puts"
Declaring Third-party Dependencies¶
defmodule Budget.Mixfile do
defp deps do
[{:httpoison, "~> 0.10.0"}, {:poison, "~> 3.0"}]
end
end
mix deps.get
Making HTTP Calls with the HTTPoison Library¶
defmodule Budget.Conversion do
def from_euro_to_dollar(amount) do
url = cs-currency-rates.herokuapp.com/currency-rates"
case HTTPoison.get(url) do
{:ok, response} -> parse(response) |> convert(amount)
{:error, _} -> "Error fetching rates"
end
end
defp parse(%{status_code: 200, body: json_response}) do
Poison.Parser.parse(json_response)
end
defp convert({:ok, rates}, amount) do
rate = find_euro(rates)
amount * rate
end
defp find_euro([%{"currency" => "euro", "rate" => rate} | _] do
rate
end
defp find_euro([_ | tail]) do
find_euro(tail)
end
defp find_euro([]) do
raise "No rate found for Euro"
end
end
Running the complete program¶
mix run -e "Budget.Conversion.from_euro_to_dollar(15) |> IO.puts"