feat: added formats(google_phone_takeout, google_records, google_semantic_history, owntracks to the watcher

This commit is contained in:
GED 2024-12-16 12:13:38 +01:00
parent e67693f43c
commit ab84d282d3
6 changed files with 65 additions and 1285 deletions

View file

@ -32,11 +32,8 @@ class ImportsController < ApplicationController
raw_data =
case params[:import][:source]
when 'google_semantic_history' then GoogleMaps::SemanticHistoryParser.new(import, current_user.id).call
when 'owntracks' then OwnTracks::RecParser.new(file).call
when 'geojson' then Geojson::ImportParser.new(import, current_user.id).call
when 'google_phone_takeout' then GoogleMaps::PhoneTakeoutParser.new(import, current_user.id).call
when 'gpx' then Hash.from_xml(file)
when 'owntracks' then OwnTracks::RecParser.new(file).call
else JSON.parse(file)
end

View file

@ -36,7 +36,7 @@ class Imports::Watcher
def file_names(directory_path)
Dir.entries(directory_path).select do |file|
['.gpx', '.json'].include?(File.extname(file))
['.gpx', '.json', '.rec'].include?(File.extname(file))
end
end
@ -73,8 +73,18 @@ class Imports::Watcher
def source(file_name)
case file_name.split('.').last
when 'json' then :geojson
when 'gpx' then :gpx
when 'json'
if file_name.match?(/location-history/i)
:google_phone_takeout
elsif file_name.match?(/Records/i)
:google_records
elsif file_name.match?(/\d{4}_\w+/i)
:google_semantic_history
else
:geojson
end
when 'rec' then :owntracks
when 'gpx' then :gpx
else raise UnsupportedSourceError, 'Unsupported source '
end
end
@ -82,6 +92,19 @@ class Imports::Watcher
def raw_data(file_path, source)
file = File.read(file_path)
source.to_sym == :gpx ? Hash.from_xml(file) : JSON.parse(file)
case source
when :google_phone_takeout
GoogleMaps::PhoneTakeoutParser.new(file).call
when :google_semantic_history
GoogleMaps::SemanticHistoryParser.new(file).call
when :google_records
GoogleMaps::RecordsParser.new(file).call
when :owntracks
OwnTracks::RecParser.new(file).call
when :gpx
Hash.from_xml(file)
else
JSON.parse(file)
end
end
end

View file

@ -1,20 +0,0 @@
# spec/features/import_process_spec.rb
require 'rails_helper'
RSpec.feature 'Import Process', type: :feature do
let(:user) { create(:user) }
before do
sign_in user
end
scenario 'User imports a Google Phone Takeout file' do
visit new_import_path
choose 'Google Phone Takeout'
attach_file 'import_files', Rails.root.join('spec/fixtures/files/google/phone-takeout.json')
click_button 'Create Import'
expect(page).to have_content('files are queued to be imported in background')
end
end

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

View file

@ -5,14 +5,18 @@ require 'rails_helper'
RSpec.describe Imports::Watcher do
describe '#call' do
subject(:service) { described_class.new.call }
let(:watched_dir_path) { Rails.root.join('spec/fixtures/files/watched') }
let(:user) { create(:user, email: 'user@domain.com') }
before do
FileUtils.mkdir_p(watched_dir_path.join(user.email))
stub_const('Imports::Watcher::WATCHED_DIR_PATH', watched_dir_path)
end
after do
FileUtils.rm_rf(watched_dir_path)
end
context 'when there are no files in the watched directory' do
it 'does not call ImportJob' do
expect(ImportJob).not_to receive(:perform_later)
@ -22,26 +26,42 @@ RSpec.describe Imports::Watcher do
end
context 'when there are files in the watched directory' do
Sidekiq::Testing.inline! do
context 'when the file has a valid user email' do
it 'creates an import for the user' do
expect { service }.to change(user.imports, :count).by(2)
end
context 'when the file has a valid user email' do
it 'creates an import for the user' do
Sidekiq::Testing.inline!
File.write(watched_dir_path.join(user.email, 'location-history.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join(user.email, 'Records.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join(user.email, '2023_January.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join(user.email, 'owntracks.rec'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join(user.email, 'gpx_track_single_segment.gpx'), '{"type": "FeatureCollection"}')
expect { service }.to change(user.imports, :count).by(5)
end
end
context 'when the file has an invalid user email' do
it 'does not create an import' do
expect { service }.not_to change(Import, :count)
end
context 'when the file has an invalid user email' do
it 'does not create an import' do
FileUtils.mkdir_p(watched_dir_path.join('invalid@domain.com'))
File.write(watched_dir_path.join('invalid@domain.com', 'location-history.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join('invalid@domain.com', 'Records.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join('invalid@domain.com', '2023_January.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join('invalid@domain.com', 'owntracks.rec'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join('invalid@domain.com', 'gpx_track_single_segment.gpx'), '{"type": "FeatureCollection"}')
expect { service }.not_to change(Import, :count)
end
end
context 'when the import already exists' do
it 'does not create a new import' do
create(:import, user:, name: 'export_same_points.json')
create(:import, user:, name: 'gpx_track_single_segment.gpx')
expect { service }.not_to change(Import, :count)
end
context 'when the import already exists' do
it 'does not create a new import' do
create(:import, user:, name: 'export_same_points.json')
create(:import, user:, name: 'gpx_track_single_segment.gpx')
create(:import, user:, name: 'location-history.json')
create(:import, user:, name: 'Records.json')
create(:import, user:, name: '2023_January.json')
create(:import, user:, name: 'data.geojson')
expect { service }.not_to change(Import, :count)
end
end
end