diff --git a/apps/parse/lib/parse/vehicle_positions.ex b/apps/parse/lib/parse/vehicle_positions.ex index d2927b9c2..257ab682f 100644 --- a/apps/parse/lib/parse/vehicle_positions.ex +++ b/apps/parse/lib/parse/vehicle_positions.ex @@ -9,16 +9,30 @@ defmodule Parse.VehiclePositions do alias Parse.Realtime.FeedMessage import Parse.Helpers + def parse(<<31, 139, _::binary>> = blob) do + # gzip encoded + blob + |> :zlib.gunzip() + |> parse + end + def parse("{" <> _ = blob) do Parse.VehiclePositionsJson.parse(blob) end def parse(blob) do - blob - |> FeedMessage.decode() - |> (fn message -> message.entity end).() - |> Stream.map(fn entity -> entity.vehicle end) - |> Stream.map(&parse_vehicle_update/1) + decoded = FeedMessage.decode(blob) + + entities = + decoded.entity + |> Stream.map(fn entity -> entity.vehicle end) + |> Stream.map(&parse_vehicle_update/1) + + if decoded.header.incrementality == :DIFFERENTIAL do + {:partial, entities} + else + entities + end end def parse_vehicle_update(update) do diff --git a/apps/parse/lib/parse/vehicle_positions_json.ex b/apps/parse/lib/parse/vehicle_positions_json.ex index e5353196c..b59880475 100644 --- a/apps/parse/lib/parse/vehicle_positions_json.ex +++ b/apps/parse/lib/parse/vehicle_positions_json.ex @@ -7,10 +7,18 @@ defmodule Parse.VehiclePositionsJson do alias Model.Vehicle def parse(body) do - body - |> Jason.decode!(strings: :copy) - |> Map.get("entity") - |> Enum.flat_map(&parse_entity/1) + decoded = Jason.decode!(body, strings: :copy) + + entities = + decoded + |> Map.get("entity") + |> Enum.flat_map(&parse_entity/1) + + if decoded["header"]["incrementality"] in ["DIFFERENTIAL", 1] do + {:partial, entities} + else + entities + end end def parse_entity( @@ -109,6 +117,10 @@ defmodule Parse.VehiclePositionsJson do Parse.Timezone.unix_to_local(timestamp) end + defp unix_to_local(timestamp) when is_float(timestamp) do + Parse.Timezone.unix_to_local(trunc(timestamp)) + end + defp unix_to_local(nil) do DateTime.utc_now() end diff --git a/apps/parse/test/parse/vehicle_positions_test.exs b/apps/parse/test/parse/vehicle_positions_test.exs index e9299d68f..ae220ebf6 100644 --- a/apps/parse/test/parse/vehicle_positions_test.exs +++ b/apps/parse/test/parse/vehicle_positions_test.exs @@ -108,6 +108,12 @@ defmodule Parse.VehiclePositionsTest do actual = parse(body) assert actual == expected end + + test "can parse gzip-encoded JSON" do + body = :zlib.gzip(Jason.encode!(%{entity: [@vehicle]})) + actual = parse(body) + assert [_] = actual + end end describe "parse_vehicle_update/1" do