Skip to content

Commit

Permalink
Provide a lab ID in lab geo fix task
Browse files Browse the repository at this point in the history
  • Loading branch information
MacTwister committed Oct 18, 2023
1 parent 5e8cd90 commit 8bfd12e
Showing 1 changed file with 68 additions and 42 deletions.
110 changes: 68 additions & 42 deletions lib/tasks/fixes.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8bfd12e

Please sign in to comment.