2024-07-12 15:59:03 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-08-05 15:23:08 -04:00
|
|
|
class ReverseGeocoding::Points::FetchData
|
2024-07-12 15:59:03 -04:00
|
|
|
attr_reader :point
|
|
|
|
|
|
|
|
|
|
def initialize(point_id)
|
|
|
|
|
@point = Point.find(point_id)
|
|
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
|
|
|
Rails.logger.error("Point with id #{point_id} not found: #{e.message}")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
|
|
|
|
return if reverse_geocoded?
|
|
|
|
|
|
|
|
|
|
response = Geocoder.search([point.latitude, point.longitude]).first
|
|
|
|
|
return if response.blank? || response.data['error'].present?
|
|
|
|
|
|
|
|
|
|
point.update!(city: response.city, country: response.country, geodata: response.data)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def reverse_geocoded?
|
2024-08-15 13:47:59 -04:00
|
|
|
point.geodata.present?
|
2024-07-12 15:59:03 -04:00
|
|
|
end
|
|
|
|
|
end
|