Class: Ahoy::GeocodeV2Job

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/ahoy/geocode_v2_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(visit_token, ip) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/jobs/ahoy/geocode_v2_job.rb', line 5

def perform(visit_token, ip)
  location =
    begin
      Geocoder.search(ip).first
    rescue => e
      Ahoy.log "Geocode error: #{e.class.name}: #{e.message}"
      nil
    end

  if location && location.country.present?
    data = {
      country: location.country,
      region: location.try(:state).presence,
      city: location.try(:city).presence,
      postal_code: location.try(:postal_code).presence,
      latitude: location.try(:latitude).presence,
      longitude: location.try(:longitude).presence
    }

    Ahoy::Tracker.new(visit_token: visit_token).geocode(data)
  end
end