Skip to content

Instantly share code, notes, and snippets.

@kerryb
Created October 7, 2022 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerryb/8834e146ce5badeef92b539e8a9db465 to your computer and use it in GitHub Desktop.
Save kerryb/8834e146ce5badeef92b539e8a9db465 to your computer and use it in GitHub Desktop.
Who’s in space?
#!/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