Class: Kameleoon::Managers::RemoteData::RemoteDataManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/managers/remote_data/remote_data_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_manager, network_manager, visitor_manger) ⇒ RemoteDataManager

Returns a new instance of RemoteDataManager.



14
15
16
17
18
19
20
# File 'lib/kameleoon/managers/remote_data/remote_data_manager.rb', line 14

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

Instance Attribute Details

#network_managerObject (readonly)

Returns the value of attribute network_manager.



12
13
14
# File 'lib/kameleoon/managers/remote_data/remote_data_manager.rb', line 12

def network_manager
  @network_manager
end

#visitor_mangerObject (readonly)

Returns the value of attribute visitor_manger.



12
13
14
# File 'lib/kameleoon/managers/remote_data/remote_data_manager.rb', line 12

def visitor_manger
  @visitor_manger
end

Instance Method Details

#get_data(key, timeout) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kameleoon/managers/remote_data/remote_data_manager.rb', line 22

def get_data(key, timeout)
  Logging::KameleoonLogger.debug("CALL: RemoteDataManager.get_data(key: '%s', timeout: %s)", key, timeout)
  response = @network_manager.get_remote_data(key, timeout)
  data = JSON.parse(response) if response
  Logging::KameleoonLogger.debug(
    "RETURN: RemoteDataManager.get_data(key: '%s', timeout: %s) -> (remote_data: %s)",
    key, timeout, data
  )
  data
rescue StandardError => e
  Logging::KameleoonLogger.error("Parsing of remote data of '#{key}' failed: #{e}")
  raise
end

#get_visitor_data(visitor_code, add_data, filter = nil, timeout = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kameleoon/managers/remote_data/remote_data_manager.rb', line 36

def get_visitor_data(visitor_code, add_data, filter = nil, timeout = nil)
  Logging::KameleoonLogger.debug(
    "CALL: RemoteDataManager.get_visitor_data(visitor_code: '%s', add_data: %s, filter: %s, timeout: %s)",
    visitor_code, add_data, filter, timeout
  )
  Utils::VisitorCode.validate(visitor_code)
  filter = Types::RemoteVisitorDataFilter.new unless filter.is_a?(Types::RemoteVisitorDataFilter)
  is_unique_identifier = @visitor_manger.get_visitor(visitor_code)&.is_unique_identifier || false
  response = @network_manager.get_remote_visitor_data(visitor_code, filter, is_unique_identifier, timeout)
  remote_visitor_data = parse_custom_data_array(visitor_code, response)
  remote_visitor_data.mark_data_as_sent(@data_manager.data_file.custom_data_info)
  data_to_add = remote_visitor_data.collect_data_to_add
  if add_data && !data_to_add.empty?
    # Cannot use `VisitorManager.add_data` because it could use remote visitor data for mapping.
    visitor = @visitor_manger.get_or_create_visitor(visitor_code)
    visitor.add_data(*data_to_add, overwrite: false)
  end
  if filter.visitor_code && !remote_visitor_data.visitor_code.nil?
    # We apply visitor code from the latest visit fetched from Data API
    visitor = @visitor_manger.get_or_create_visitor(visitor_code)
    visitor.mapping_identifier = remote_visitor_data.visitor_code
  end
  data_to_return = remote_visitor_data.collect_data_to_return
  Logging::KameleoonLogger.debug(
    "RETURN: RemoteDataManager.get_visitor_data(visitor_code: '%s', add_data: %s, filter: %s," \
      ' timeout: %s) -> (visitor_data: %s)',
    visitor_code, add_data, filter, timeout, data_to_return
  )
  data_to_return
end

#parse_custom_data_array(visitor_code, response) ⇒ Object

helper method used by ‘get_remote_visitor_data`



69
70
71
72
73
74
# File 'lib/kameleoon/managers/remote_data/remote_data_manager.rb', line 69

def parse_custom_data_array(visitor_code, response)
  RemoteVisitorData.new(JSON.parse(response))
rescue StandardError => e
  Logging::KameleoonLogger.error("Parsing of remote visitor data of '#{visitor_code}' failed: #{e}")
  raise
end