From cbca77096dc268cca01a25597868062a2870986a Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Tue, 25 Apr 2023 17:21:22 +1000 Subject: [PATCH] Allow requests from shortcuts --- app/actions/workouts/create.rb | 12 ++++++++++-- lib/adamantium/geo/gpx_parser.rb | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/actions/workouts/create.rb b/app/actions/workouts/create.rb index 873f154..f2e8c73 100644 --- a/app/actions/workouts/create.rb +++ b/app/actions/workouts/create.rb @@ -1,3 +1,4 @@ +require "pry" module Adamantium module Actions module Workouts @@ -6,10 +7,17 @@ module Adamantium def handle(req, res) tempfile = Tempfile.new(%w/path .gpx/) - tempfile.write req.params[:file][:tempfile] + + if req.params.to_h.dig(:file, :tempfile) != nil + tempfile.write req.params[:file][:tempfile].read + else + tempfile.write req.params[:file] + end + tempfile.rewind - gpxfile = gpx_parser.call(file: tempfile) + + gpxfile = gpx_parser.call(path: tempfile.path) if gpxfile.success? create.call(**gpxfile.value!) diff --git a/lib/adamantium/geo/gpx_parser.rb b/lib/adamantium/geo/gpx_parser.rb index 538d82a..0c1e056 100644 --- a/lib/adamantium/geo/gpx_parser.rb +++ b/lib/adamantium/geo/gpx_parser.rb @@ -9,9 +9,9 @@ module Adamantium class GpxParser include Dry::Monads[:result] - def call(file:) - gpxfile = GeoRuby::Gpx4r::GpxFile.open(file.path) - gpx = GPX::GPXFile.new(gpx_file: file.path) + def call(path:) + gpxfile = GeoRuby::Gpx4r::GpxFile.open(path) + gpx = GPX::GPXFile.new(gpx_file: path) x = gpxfile.as_line_string.points.flat_map { |p| p.x } y = gpxfile.as_line_string.points.flat_map { |p| p.y }