2024-05-18 09:00:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class GoogleMaps::RecordsParser
|
|
|
|
|
attr_reader :import
|
|
|
|
|
|
|
|
|
|
def initialize(import)
|
|
|
|
|
@import = import
|
|
|
|
|
end
|
|
|
|
|
|
2024-05-23 14:35:31 -04:00
|
|
|
def call(json)
|
|
|
|
|
data = parse_json(json)
|
|
|
|
|
|
2024-06-30 11:47:36 -04:00
|
|
|
return if Point.exists?(
|
|
|
|
|
latitude: data[:latitude],
|
|
|
|
|
longitude: data[:longitude],
|
|
|
|
|
timestamp: data[:timestamp],
|
|
|
|
|
user_id: import.user_id
|
|
|
|
|
)
|
2024-06-10 13:08:27 -04:00
|
|
|
|
2024-05-23 14:35:31 -04:00
|
|
|
Point.create(
|
|
|
|
|
latitude: data[:latitude],
|
|
|
|
|
longitude: data[:longitude],
|
|
|
|
|
timestamp: data[:timestamp],
|
|
|
|
|
raw_data: data[:raw_data],
|
|
|
|
|
topic: 'Google Maps Timeline Export',
|
|
|
|
|
tracker_id: 'google-maps-timeline-export',
|
2024-05-25 16:27:18 -04:00
|
|
|
import_id: import.id,
|
|
|
|
|
user_id: import.user_id
|
2024-05-23 14:35:31 -04:00
|
|
|
)
|
2024-05-18 09:00:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-05-23 14:35:31 -04:00
|
|
|
def parse_json(json)
|
|
|
|
|
{
|
|
|
|
|
latitude: json['latitudeE7'].to_f / 10**7,
|
|
|
|
|
longitude: json['longitudeE7'].to_f / 10**7,
|
2024-09-24 16:46:52 -04:00
|
|
|
timestamp: parse_timestamp(json['timestamp'] || json['timestampMs']),
|
2024-06-10 13:08:27 -04:00
|
|
|
altitude: json['altitude'],
|
|
|
|
|
velocity: json['velocity'],
|
2024-05-23 14:35:31 -04:00
|
|
|
raw_data: json
|
|
|
|
|
}
|
2024-05-18 09:00:44 -04:00
|
|
|
end
|
2024-09-24 16:46:52 -04:00
|
|
|
|
|
|
|
|
def parse_timestamp(timestamp)
|
|
|
|
|
begin
|
|
|
|
|
# if the timestamp is in ISO 8601 format, try to parse it
|
|
|
|
|
DateTime.parse(timestamp).to_time.to_i
|
|
|
|
|
rescue
|
|
|
|
|
if timestamp.to_s.length > 10
|
|
|
|
|
# If the timestamp is in milliseconds, convert to seconds
|
|
|
|
|
timestamp.to_i / 1000
|
|
|
|
|
else
|
|
|
|
|
# If the timestamp is in seconds, return it without change
|
|
|
|
|
timestamp.to_i
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-05-18 09:00:44 -04:00
|
|
|
end
|