class LocationPositionUpdater
This class fetches the latitude and longitude of a locationโs address using the OrdnanceSurvey::PlacesAPI and stores it in the position column.
Attributes
Public Class Methods
Source
# File app/lib/location_position_updater.rb, line 25 def self.call(...) = new(...).call private_class_method :new private def query = [location.name, location.address_parts].join(", ") def position response = OrdnanceSurvey::PlacesAPI.find(query) results = response[:results] raise NoResults if results.blank? first_result = results.first latitude = first_result.dig(:dpa, :lat) longitude = first_result.dig(:dpa, :lng) raise NoResults if latitude.blank? || longitude.blank? "POINT(#{longitude} #{latitude})" end
Source
# File app/lib/location_position_updater.rb, line 13 def initialize(location) @location = location end
Public Instance Methods
Source
# File app/lib/location_position_updater.rb, line 19 def call raise MissingAddress unless location.has_address? location.update!(position:) end
Source
# File app/lib/location_position_updater.rb, line 33 def position response = OrdnanceSurvey::PlacesAPI.find(query) results = response[:results] raise NoResults if results.blank? first_result = results.first latitude = first_result.dig(:dpa, :lat) longitude = first_result.dig(:dpa, :lng) raise NoResults if latitude.blank? || longitude.blank? "POINT(#{longitude} #{latitude})" end
Source
# File app/lib/location_position_updater.rb, line 31 def query = [location.name, location.address_parts].join(", ") def position response = OrdnanceSurvey::PlacesAPI.find(query) results = response[:results] raise NoResults if results.blank? first_result = results.first latitude = first_result.dig(:dpa, :lat) longitude = first_result.dig(:dpa, :lng) raise NoResults if latitude.blank? || longitude.blank? "POINT(#{longitude} #{latitude})" end end