Class: Spree::Addresses::GeocodeAddressJob

Inherits:
BaseJob
  • Object
show all
Defined in:
app/jobs/spree/addresses/geocode_address_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(address_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/jobs/spree/addresses/geocode_address_job.rb', line 8

def perform(address_id)
  address = Spree::Address.find(address_id)

  coordinates = Geocoder.coordinates(
    address.geocoder_address,
    country: address.country_iso3
  )

  if coordinates.present?
    address.update_columns(latitude: coordinates[0], longitude: coordinates[1], updated_at: Time.current)
  else
    # Unfortunately there is no way to get the error message from Geocoder,
    # but the request is fully displayed in the server logs
    Rails.error.report(
      GeocodeAddressError.new("Cannot geocode address ID: #{address.id}"),
      handled: false,
      context: { address_id: address_id },
      source: 'spree.core'
    )
  end
end