Sha256: f9a4d8f712a9861fa371df9c6d62001f0157264a49801eba25fa2205167a849d

Contents?: true

Size: 945 Bytes

Versions: 5

Compression:

Stored size: 945 Bytes

Contents

module AuthTrail
  class GeocodeJob < ActiveJob::Base
    # default queue is used if queue_as returns nil
    # Rails has a test for this
    queue_as { AuthTrail.job_queue }

    def perform(login_activity)
      result =
        begin
          Geocoder.search(login_activity.ip).first
        rescue NameError
          raise "Add the geocoder gem to your Gemfile to use geocoding"
        rescue => e
          Rails.logger.info "Geocode failed: #{e.message}"
          nil
        end

      if result
        attributes = {
          city: result.try(:city),
          region: result.try(:state),
          country: result.try(:country),
          country_code: result.try(:country_code),
          latitude: result.try(:latitude),
          longitude: result.try(:longitude)
        }
        attributes.each do |k, v|
          login_activity.try("#{k}=", v.presence)
        end
        login_activity.save!
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
authtrail-0.6.0 lib/auth_trail/geocode_job.rb
authtrail-0.5.0 lib/auth_trail/geocode_job.rb
authtrail-0.4.3 app/jobs/auth_trail/geocode_job.rb
authtrail-0.4.2 app/jobs/auth_trail/geocode_job.rb
authtrail-0.4.1 app/jobs/auth_trail/geocode_job.rb