-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
253 additions
and
156 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
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,42 @@ | ||
defmodule Predictions.LastTrip do | ||
@hour_in_seconds 3600 | ||
|
||
def parse_json_response("") do | ||
%{"entity" => []} | ||
end | ||
|
||
def parse_json_response(body) do | ||
Jason.decode!(body) | ||
end | ||
|
||
defp get_running_trips(predictions_feed) do | ||
predictions_feed["entity"] | ||
|> Stream.map(& &1["trip_update"]) | ||
|> Enum.reject(&(&1["trip"]["schedule_relationship"] == "CANCELED")) | ||
end | ||
|
||
def get_last_trips(predictions_feed) do | ||
get_running_trips(predictions_feed) | ||
|> Stream.filter(&(&1["trip"]["last_trip"] == true)) | ||
|> Enum.map(& &1["trip"]["trip_id"]) | ||
end | ||
|
||
def get_recent_departures(predictions_feed) do | ||
current_time = Timex.now() | ||
|
||
predictions_by_trip = | ||
get_running_trips(predictions_feed) | ||
|> Enum.map(&{&1["trip"]["trip_id"], &1["trip"]["route_id"], &1["stop_time_update"]}) | ||
|
||
for {trip_id, route_id, predictions} <- predictions_by_trip, | ||
prediction <- predictions, | ||
prediction["departure"] do | ||
seconds_until_departure = prediction["departure"]["time"] - DateTime.to_unix(current_time) | ||
|
||
if seconds_until_departure in -@hour_in_seconds..0 do | ||
{prediction["stop_id"], trip_id, route_id, prediction["departure"]["time"]} | ||
end | ||
end | ||
|> Enum.reject(&is_nil/1) | ||
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
Oops, something went wrong.