mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Use attached import file to import own tracks data
This commit is contained in:
parent
a93b49ee80
commit
f3b98ac83d
5 changed files with 25 additions and 20 deletions
|
|
@ -11,7 +11,7 @@ class Gpx::TrackImporter
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
import.file.open do |file|
|
import.file.download do |file|
|
||||||
json = Hash.from_xml(file)
|
json = Hash.from_xml(file)
|
||||||
|
|
||||||
tracks = json['gpx']['trk']
|
tracks = json['gpx']['trk']
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,18 @@
|
||||||
class OwnTracks::Importer
|
class OwnTracks::Importer
|
||||||
include Imports::Broadcaster
|
include Imports::Broadcaster
|
||||||
|
|
||||||
attr_reader :import, :data, :user_id
|
attr_reader :import, :user_id
|
||||||
|
|
||||||
def initialize(import, user_id)
|
def initialize(import, user_id)
|
||||||
@import = import
|
@import = import
|
||||||
@data = import.raw_data
|
|
||||||
@user_id = user_id
|
@user_id = user_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
points_data = data.map.with_index(1) do |point, index|
|
import.file.download do |file|
|
||||||
|
parsed_data = OwnTracks::RecParser.new(file).call
|
||||||
|
|
||||||
|
points_data = parsed_data.map do |point|
|
||||||
OwnTracks::Params.new(point).call.merge(
|
OwnTracks::Params.new(point).call.merge(
|
||||||
import_id: import.id,
|
import_id: import.id,
|
||||||
user_id: user_id,
|
user_id: user_id,
|
||||||
|
|
@ -23,6 +25,7 @@ class OwnTracks::Importer
|
||||||
|
|
||||||
bulk_insert_points(points_data)
|
bulk_insert_points(points_data)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,8 @@ class OwnTracks::RecParser
|
||||||
def call
|
def call
|
||||||
file.split("\n").map do |line|
|
file.split("\n").map do |line|
|
||||||
parts = line.split("\t")
|
parts = line.split("\t")
|
||||||
if parts.size > 2 && parts[1].strip == '*'
|
|
||||||
JSON.parse(parts[2])
|
Oj.load(parts[2]) if parts.size > 2 && parts[1].strip == '*'
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end.compact
|
end.compact
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,7 @@
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :import do
|
factory :import do
|
||||||
user
|
user
|
||||||
name { 'MARCH_2024.json' }
|
name { 'owntracks_export.json' }
|
||||||
source { Import.sources[:owntracks] }
|
source { Import.sources[:owntracks] }
|
||||||
raw_data { OwnTracks::RecParser.new(File.read('spec/fixtures/files/owntracks/2024-03.rec')).call }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,13 @@ RSpec.describe OwnTracks::Importer do
|
||||||
subject(:parser) { described_class.new(import, user.id).call }
|
subject(:parser) { described_class.new(import, user.id).call }
|
||||||
|
|
||||||
let(:user) { create(:user) }
|
let(:user) { create(:user) }
|
||||||
let(:import) { create(:import, user:, name: 'owntracks_export.json') }
|
let(:import) { create(:import, user:, name: '2024-03.rec') }
|
||||||
|
let(:file_path) { Rails.root.join('spec/fixtures/files/owntracks/2024-03.rec') }
|
||||||
|
let(:file) { Rack::Test::UploadedFile.new(file_path, 'text/plain') }
|
||||||
|
|
||||||
|
before do
|
||||||
|
import.file.attach(io: File.open(file_path), filename: '2024-03.rec', content_type: 'text/plain')
|
||||||
|
end
|
||||||
|
|
||||||
context 'when file exists' do
|
context 'when file exists' do
|
||||||
it 'creates points' do
|
it 'creates points' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue