From eb892dde9adc3323903a26b2c33d1e2a7addecf3 Mon Sep 17 00:00:00 2001 From: Sascha Zepter <60738409+saschazepter@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:49:11 +0000 Subject: [PATCH] Refactor parse_timestamp: move to lib/timestamps.rb --- app/services/google_maps/records_parser.rb | 17 +------------- .../google_maps/semantic_history_parser.rb | 23 ++++--------------- lib/timestamps.rb | 19 +++++++++++++++ 3 files changed, 24 insertions(+), 35 deletions(-) create mode 100644 lib/timestamps.rb diff --git a/app/services/google_maps/records_parser.rb b/app/services/google_maps/records_parser.rb index 2e97a885..401d2ac6 100644 --- a/app/services/google_maps/records_parser.rb +++ b/app/services/google_maps/records_parser.rb @@ -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 diff --git a/app/services/google_maps/semantic_history_parser.rb b/app/services/google_maps/semantic_history_parser.rb index dc0583c0..85594e45 100644 --- a/app/services/google_maps/semantic_history_parser.rb +++ b/app/services/google_maps/semantic_history_parser.rb @@ -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 diff --git a/lib/timestamps.rb b/lib/timestamps.rb new file mode 100644 index 00000000..ea7358cc --- /dev/null +++ b/lib/timestamps.rb @@ -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