Created
October 7, 2022 18:27
-
-
Save kerryb/8834e146ce5badeef92b539e8a9db465 to your computer and use it in GitHub Desktop.
Who’s in space?
This file contains hidden or 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
#!/usr/bin/env elixir | |
Mix.install([ | |
{:httpoison, "~> 1.8"}, | |
{:jason, "~> 1.3"} | |
]) | |
defmodule Space do | |
def run do | |
fetch_data() | |
|> Jason.decode!() | |
|> extract_people() | |
|> Enum.group_by(fn %{"craft" => craft} -> craft end) | |
|> report() | |
end | |
defp fetch_data do | |
with {:ok, response} <- HTTPoison.get("http://api.open-notify.org/astros.json") do | |
response.body | |
end | |
end | |
defp extract_people(json), do: json["people"] | |
defp report(people_by_craft) do | |
IO.puts("The people currently in space are:") | |
for {craft, people} <- people_by_craft do | |
report_craft(craft, people) | |
end | |
end | |
defp report_craft(craft, people) do | |
IO.puts(" [#{craft}]:") | |
for %{"name" => name} <- people do | |
IO.puts(" #{name}") | |
end | |
end | |
end | |
HTTPoison.start() | |
Space.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment