mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Refactor parse_timestamp: move to lib/timestamps.rb
This commit is contained in:
parent
6077776880
commit
eb892dde9a
3 changed files with 24 additions and 35 deletions
|
|
@ -35,25 +35,10 @@ class GoogleMaps::RecordsParser
|
||||||
{
|
{
|
||||||
latitude: json['latitudeE7'].to_f / 10**7,
|
latitude: json['latitudeE7'].to_f / 10**7,
|
||||||
longitude: json['longitudeE7'].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'],
|
altitude: json['altitude'],
|
||||||
velocity: json['velocity'],
|
velocity: json['velocity'],
|
||||||
raw_data: json
|
raw_data: json
|
||||||
}
|
}
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class GoogleMaps::SemanticHistoryParser
|
||||||
{
|
{
|
||||||
latitude: waypoint['latE7'].to_f / 10**7,
|
latitude: waypoint['latE7'].to_f / 10**7,
|
||||||
longitude: waypoint['lngE7'].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
|
raw_data: timeline_object
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
@ -52,7 +52,7 @@ class GoogleMaps::SemanticHistoryParser
|
||||||
{
|
{
|
||||||
latitude: timeline_object['activitySegment']['startLocation']['latitudeE7'].to_f / 10**7,
|
latitude: timeline_object['activitySegment']['startLocation']['latitudeE7'].to_f / 10**7,
|
||||||
longitude: timeline_object['activitySegment']['startLocation']['longitudeE7'].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
|
raw_data: timeline_object
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
@ -62,7 +62,7 @@ class GoogleMaps::SemanticHistoryParser
|
||||||
{
|
{
|
||||||
latitude: timeline_object['placeVisit']['location']['latitudeE7'].to_f / 10**7,
|
latitude: timeline_object['placeVisit']['location']['latitudeE7'].to_f / 10**7,
|
||||||
longitude: timeline_object['placeVisit']['location']['longitudeE7'].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
|
raw_data: timeline_object
|
||||||
}
|
}
|
||||||
elsif timeline_object['placeVisit']['otherCandidateLocations'].any?
|
elsif timeline_object['placeVisit']['otherCandidateLocations'].any?
|
||||||
|
|
@ -73,7 +73,7 @@ class GoogleMaps::SemanticHistoryParser
|
||||||
{
|
{
|
||||||
latitude: point['latitudeE7'].to_f / 10**7,
|
latitude: point['latitudeE7'].to_f / 10**7,
|
||||||
longitude: point['longitudeE7'].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
|
raw_data: timeline_object
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -82,19 +82,4 @@ class GoogleMaps::SemanticHistoryParser
|
||||||
end
|
end
|
||||||
end.reject(&:blank?)
|
end.reject(&:blank?)
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
19
lib/timestamps.rb
Normal file
19
lib/timestamps.rb
Normal 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
|
||||||
Loading…
Reference in a new issue