Advertisement
PadmaJS

Untitled

Oct 12th, 2021
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. defmodule Weather do
  2. def start do
  3. spawn(fn -> loop(nil) end)
  4. end
  5.  
  6. defp loop(data) do
  7. new_data = receive do
  8. message -> process(message, data)
  9. end
  10. loop(new_data)
  11. end
  12.  
  13. defp process({:weather, location}, _data) do
  14. HTTPoison.get! "https://api.openweathermap.org/data/2.5/weather?q=#{location}&appid=f4a0eee4b5e6bc6d8cbb956e511528de&units=metric"
  15. end
  16.  
  17. defp process({:value, client}, data) do
  18. send(client, {:response, data})
  19. data
  20. end
  21.  
  22. defp show_result(server) do
  23. send(server, {:value, self()})
  24. receive do
  25. {:response, data} -> data
  26. end
  27. end
  28.  
  29. def get_weather(server, location) do
  30. send(server, {:weather ,location})
  31. show_result(server)
  32. end
  33. end
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement