Class: Kameleoon::Managers::Warehouse::WarehouseManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/managers/warehouse/warehouse_manager.rb

Constant Summary collapse

WAREHOUSE_AUDIENCES_FIELD_NAME =
'warehouseAudiences'

Instance Method Summary collapse

Constructor Details

#initialize(network_manager, visitor_manager) ⇒ WarehouseManager

Returns a new instance of WarehouseManager.



13
14
15
16
17
18
# File 'lib/kameleoon/managers/warehouse/warehouse_manager.rb', line 13

def initialize(network_manager, visitor_manager)
  Logging::KameleoonLogger.debug('CALL: WarehouseManager.new(networkManager, visitorManager)')
  @network_manager = network_manager
  @visitor_manager = visitor_manager
  Logging::KameleoonLogger.debug('RETURN: WarehouseManager.new(networkManager, visitorManager)')
end

Instance Method Details

#get_visitor_warehouse_audience(visitor_code, custom_data_index, warehouse_key = nil, timeout = nil) ⇒ Object



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
47
48
# File 'lib/kameleoon/managers/warehouse/warehouse_manager.rb', line 20

def get_visitor_warehouse_audience(visitor_code, custom_data_index, warehouse_key = nil, timeout = nil)
  Logging::KameleoonLogger.debug(
    "CALL: WarehouseManager.get_visitor_warehouse_audience(visitor_code: '%s', custom_data_index: %s," \
      " warehouse_key: '%s', timeout: %s)", visitor_code, custom_data_index, warehouse_key, timeout
  )
  Utils::VisitorCode.validate(visitor_code)
  remote_data_key = warehouse_key.nil? || warehouse_key.empty? ? visitor_code : warehouse_key
  response = @network_manager.get_remote_data(remote_data_key, timeout)
  unless response.is_a?(String)
    Logging::KameleoonLogger.debug(
      "RETURN: WarehouseManager.get_visitor_warehouse_audience(visitor_code: '%s', custom_data_index: %s," \
        " warehouse_key: '%s', timeout: %s) -> (custom_data: nil)",
      visitor_code, custom_data_index, warehouse_key, timeout
    )
    return nil
  end

  remote_data = JSON.parse(response)
  warehouse_audiences = remote_data.is_a?(Hash) ? remote_data[WAREHOUSE_AUDIENCES_FIELD_NAME] : nil
  data_values = warehouse_audiences.is_a?(Hash) ? warehouse_audiences.keys : []
  warehouse_audiences_data = CustomData.new(custom_data_index, *data_values)
  @visitor_manager.add_data(visitor_code, warehouse_audiences_data)
  Logging::KameleoonLogger.debug(
    "RETURN: WarehouseManager.get_visitor_warehouse_audience(visitor_code: '%s', custom_data_index: %s," \
      " warehouse_key: '%s', timeout: %s) -> (custom_data: %s)",
    visitor_code, custom_data_index, warehouse_key, timeout, warehouse_audiences_data
  )
  warehouse_audiences_data
end