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
|
2025-05-17 13:14:28 -04:00
|
|
|
ExceptionReporter.call(e)
|
|
|
|
|
|
2024-07-12 15:59:03 -04:00
|
|
|
Rails.logger.error("Point with id #{point_id} not found: #{e.message}")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
2024-12-02 08:44:22 -05:00
|
|
|
return if point.reverse_geocoded?
|
2024-07-12 15:59:03 -04:00
|
|
|
|
2024-12-17 06:56:21 -05:00
|
|
|
update_point_with_geocoding_data
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def update_point_with_geocoding_data
|
2025-02-21 17:45:36 -05:00
|
|
|
response = Geocoder.search([point.lat, point.lon]).first
|
2024-07-12 15:59:03 -04:00
|
|
|
return if response.blank? || response.data['error'].present?
|
|
|
|
|
|
2025-06-30 16:29:28 -04:00
|
|
|
country_record = Country.find_by(name: response.country) if response.country
|
2025-06-26 13:24:40 -04:00
|
|
|
|
2024-12-02 08:44:22 -05:00
|
|
|
point.update!(
|
|
|
|
|
city: response.city,
|
2025-07-28 12:53:19 -04:00
|
|
|
country_name: response.country,
|
2025-06-26 13:24:40 -04:00
|
|
|
country_id: country_record&.id,
|
2024-12-02 08:44:22 -05:00
|
|
|
geodata: response.data,
|
|
|
|
|
reverse_geocoded_at: Time.current
|
|
|
|
|
)
|
2024-07-12 15:59:03 -04:00
|
|
|
end
|
|
|
|
|
end
|