Sha256: 723f4fc8abe49910dd2b136beae607778ae39e3c674c5e3a70a9f07ff4a64a92

Contents?: true

Size: 1.25 KB

Versions: 7

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module PUNK
  class GeocodeSessionWorker < Worker
    args :session_id

    def validate
      validates_not_null :session_id
      validates_not_empty :session_id
    end

    def process
      require 'ipstack'

      session = Session[session_id]
      return if session.blank?

      ip_address = session.remote_addr.to_s
      return if ip_address == '127.0.0.1'

      return if PUNK.get.ipstack.api_key.blank?
      result = Ipstack::API.standard(ip_address).deep_symbolize_keys

      raise if result.blank?

      timezone = result[:time_zone][:code] if result[:time_zone].present?
      language = result[:location][:languages].first[:code] if result[:location].present? && result[:location][:languages].present?
      currency = result[:currency][:code] if result[:currency].present?
      session.update(data: session.data.merge(
        tz: timezone,
        lang: language,
        currency: currency,
        geo: {
          lat: result[:latitude],
          lng: result[:longitude]
        },
        location: {
          city: result[:city],
          region: result[:region_name],
          country: result[:country_name],
          continent: result[:continent_name]
        }
      ))

      session.save
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
punk-0.3.6 lib/punk/workers/geocode_session_worker.rb
punk-0.3.5 lib/punk/workers/geocode_session_worker.rb
punk-0.3.4 lib/punk/workers/geocode_session_worker.rb
punk-0.3.3 lib/punk/workers/geocode_session_worker.rb
punk-0.3.2 lib/punk/workers/geocode_session_worker.rb
punk-0.3.1 lib/punk/workers/geocode_session_worker.rb
punk-0.2.0 lib/punk/workers/geocode_session_worker.rb