Sha256: 96020a26ff4596482827b008ad6bbcb8563b1ad4e51c709d529efeb9ec3c36d2

Contents?: true

Size: 797 Bytes

Versions: 4

Compression:

Stored size: 797 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 => 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),
          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

4 entries across 4 versions & 1 rubygems

Version Path
authtrail-0.3.1 app/jobs/auth_trail/geocode_job.rb
authtrail-0.3.0 app/jobs/auth_trail/geocode_job.rb
authtrail-0.2.2 app/jobs/auth_trail/geocode_job.rb
authtrail-0.2.1 app/jobs/auth_trail/geocode_job.rb