Class: PUNK::GeocodeSessionWorker

Inherits:
Worker show all
Defined in:
lib/punk/workers/geocode_session_worker.rb

Instance Attribute Summary

Attributes included from Validatable

#errors

Instance Method Summary collapse

Methods inherited from Worker

#perform, perform_now

Methods inherited from Runnable

args, #method_missing, #respond_to_missing?

Methods included from Validatable

#default_validation_helpers_options, #get_column_value, #valid?, #validates_not_empty

Methods included from Plugins::Validation::InstanceMethods

#validates_email, #validates_event, #validates_phone, #validates_state, #validates_url

Methods inherited from Settings

#method_missing, #respond_to_missing?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PUNK::Runnable

Instance Method Details

#processObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/punk/workers/geocode_session_worker.rb', line 12

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_changes
end

#validateObject



7
8
9
10
# File 'lib/punk/workers/geocode_session_worker.rb', line 7

def validate
  validates_not_null :session_id
  validates_not_empty :session_id
end