-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
52 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Servy.Fetcher do | ||
def async(fun) do | ||
parent = self() | ||
|
||
spawn(fn -> send(parent, {self(), :result, fun.()}) end) | ||
end | ||
|
||
def get_result(pid) do | ||
receive do | ||
{^pid, :result, value} -> value | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Servy.Tracker do | ||
@doc """ | ||
Simulates sending a request to an external API | ||
to get the GPS coordinates of a wildthing. | ||
""" | ||
def get_location(wildthing) do | ||
# CODE GOES HERE TO SEND A REQUEST TO THE EXTERNAL API | ||
|
||
# Sleep to simulate the API delay: | ||
:timer.sleep(500) | ||
|
||
# Example responses returned from the API: | ||
locations = %{ | ||
"roscoe" => %{lat: "44.4280 N", lng: "110.5885 W"}, | ||
"smokey" => %{lat: "48.7596 N", lng: "113.7870 W"}, | ||
"brutus" => %{lat: "43.7904 N", lng: "110.6818 W"}, | ||
"bigfoot" => %{lat: "29.0469 N", lng: "98.8667 W"} | ||
} | ||
|
||
Map.get(locations, wildthing) | ||
end | ||
end |