Refactor parse_timestamp: move to lib/timestamps.rb

This commit is contained in:
Sascha Zepter 2024-09-30 14:49:11 +00:00
parent 6077776880
commit eb892dde9a
3 changed files with 24 additions and 35 deletions

View file

@ -35,25 +35,10 @@ class GoogleMaps::RecordsParser
{
latitude: json['latitudeE7'].to_f / 10**7,
longitude: json['longitudeE7'].to_f / 10**7,
timestamp: parse_timestamp(json['timestamp'] || json['timestampMs']),
timestamp: Timestamps::parse_timestamp(json['timestamp'] || json['timestampMs']),
altitude: json['altitude'],
velocity: json['velocity'],
raw_data: json
}
end
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
end

View file

@ -44,7 +44,7 @@ class GoogleMaps::SemanticHistoryParser
{
latitude: waypoint['latE7'].to_f / 10**7,
longitude: waypoint['lngE7'].to_f / 10**7,
timestamp: parse_timestamp(timeline_object['activitySegment']['duration']['startTimestamp'] || timeline_object['activitySegment']['duration']['startTimestampMs']),
timestamp: Timestamps::parse_timestamp(timeline_object['activitySegment']['duration']['startTimestamp'] || timeline_object['activitySegment']['duration']['startTimestampMs']),
raw_data: timeline_object
}
end
@ -52,7 +52,7 @@ class GoogleMaps::SemanticHistoryParser
{
latitude: timeline_object['activitySegment']['startLocation']['latitudeE7'].to_f / 10**7,
longitude: timeline_object['activitySegment']['startLocation']['longitudeE7'].to_f / 10**7,
timestamp: parse_timestamp(timeline_object['activitySegment']['duration']['startTimestamp'] || timeline_object['activitySegment']['duration']['startTimestampMs']),
timestamp: Timestamps::parse_timestamp(timeline_object['activitySegment']['duration']['startTimestamp'] || timeline_object['activitySegment']['duration']['startTimestampMs']),
raw_data: timeline_object
}
end
@ -62,7 +62,7 @@ class GoogleMaps::SemanticHistoryParser
{
latitude: timeline_object['placeVisit']['location']['latitudeE7'].to_f / 10**7,
longitude: timeline_object['placeVisit']['location']['longitudeE7'].to_f / 10**7,
timestamp: parse_timestamp(timeline_object['placeVisit']['duration']['startTimestamp'] || timeline_object['placeVisit']['duration']['startTimestampMs']),
timestamp: Timestamps::parse_timestamp(timeline_object['placeVisit']['duration']['startTimestamp'] || timeline_object['placeVisit']['duration']['startTimestampMs']),
raw_data: timeline_object
}
elsif timeline_object['placeVisit']['otherCandidateLocations'].any?
@ -73,7 +73,7 @@ class GoogleMaps::SemanticHistoryParser
{
latitude: point['latitudeE7'].to_f / 10**7,
longitude: point['longitudeE7'].to_f / 10**7,
timestamp: parse_timestamp(timeline_object['placeVisit']['duration']['startTimestamp'] || timeline_object['placeVisit']['duration']['startTimestampMs']),
timestamp: Timestamps::parse_timestamp(timeline_object['placeVisit']['duration']['startTimestamp'] || timeline_object['placeVisit']['duration']['startTimestampMs']),
raw_data: timeline_object
}
else
@ -82,19 +82,4 @@ class GoogleMaps::SemanticHistoryParser
end
end.reject(&:blank?)
end
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
end

19
lib/timestamps.rb Normal file
View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
module Timestamps
def self.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
end