From 8bfd12ea62fd8bff5d975263d54e8cb45f581137 Mon Sep 17 00:00:00 2001 From: Julian Gallimore Date: Wed, 18 Oct 2023 17:36:59 +0200 Subject: [PATCH] Provide a lab ID in lab geo fix task --- lib/tasks/fixes.rake | 110 ++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 42 deletions(-) diff --git a/lib/tasks/fixes.rake b/lib/tasks/fixes.rake index 3e267387..2f8c4cb1 100644 --- a/lib/tasks/fixes.rake +++ b/lib/tasks/fixes.rake @@ -5,56 +5,82 @@ namespace :fixes do desc "Any labs without GEO coordinates, fill from address" task labsgeo: :environment do - labs = Lab.where(latitude: nil) - - labs.each do |lab| - - query_lines = [] - query_lines << lab.address_1 if lab.address_1.present? - query_lines << lab.address_2 if lab.address_2.present? - query_lines << lab.city if lab.city.present? - query_lines << lab.county if lab.county.present? - query_lines << lab.subregion if lab.subregion.present? - query_lines << lab.region if lab.region.present? - query_lines << lab.postal_code if lab.postal_code.present? - query_lines << lab.country_code if lab.country_code.present? - - querytext = query_lines.join(', ') - - puts querytext - - response = HTTParty.get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', - query: { - key: ENV['GOOGLE_PLACES_API_KEY'], - input: querytext, - fields: 'formatted_address,name,geometry', - inputtype: 'textquery', - }, - ) - - data = JSON.parse(response.body) - - if !data.key?('candidates') || !data['candidates'].is_a?(Array) - warn "#{lab.slug}: No results returned." - next - end + ARGV.each { |a| task a.to_sym do ; end } + + if ARGV[1].present? + lab = Lab.find(ARGV[1].to_i) + update_geo_lab(lab) + else + labs = Lab.where(latitude: nil) - if data['candidates'].empty? - warn "#{lab.slug}: No results returned." + STDOUT.puts "You are about to run through #{labs.length} labs. Are you sure? (y/n)" + + begin + input = STDIN.gets.strip.downcase + end until %w(y n).include?(input) + + if input != 'y' + STDOUT.puts "Opsy, stopping." next end - if data['candidates'].length > 1 - warn "#{lab.slug}: More than one result, skipping..." - next + labs.each do |lab| + update_geo_lab(lab) end + end + end +end - result = data['candidates'].first +def update_geo_lab(lab) - puts "FOUND: #{result['geometry']['location']['lat']}" + STDOUT.puts "Doing #{lab.name}" - lab.update(latitude: result['geometry']['location']['lat'], longitude: result['geometry']['location']['lng']) + query_lines = [] + query_lines << lab.address_1 if lab.address_1.present? + query_lines << lab.address_2 if lab.address_2.present? + query_lines << lab.city if lab.city.present? + query_lines << lab.county if lab.county.present? + query_lines << lab.subregion if lab.subregion.present? + query_lines << lab.region if lab.region.present? + query_lines << lab.postal_code if lab.postal_code.present? + query_lines << lab.country_code if lab.country_code.present? - end + querytext = query_lines.join(', ') + + STDOUT.puts querytext + + response = HTTParty.get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', + query: { + key: ENV['GOOGLE_PLACES_API_KEY'], + input: querytext, + fields: 'formatted_address,name,geometry', + inputtype: 'textquery', + }, + ) + + data = JSON.parse(response.body) + + + if !data.key?('candidates') || !data['candidates'].is_a?(Array) + warn "#{lab.slug}: No results returned." + return end + + if data['candidates'].empty? + warn "#{lab.slug}: No results returned." + return + end + + if data['candidates'].length > 1 + warn "#{lab.slug}: More than one result, skipping..." + return + end + + result = data['candidates'].first + + puts "FOUND: #{result['geometry']['location']['lat']}" + puts result + + lab.update(latitude: result['geometry']['location']['lat'], longitude: result['geometry']['location']['lng']) + end