Class: Optimizely::OdpSegmentManager

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/odp/odp_segment_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments_cache, api_manager = nil, logger = nil, proxy_config = nil, timeout: nil) ⇒ OdpSegmentManager

Returns a new instance of OdpSegmentManager.



28
29
30
31
32
33
# File 'lib/optimizely/odp/odp_segment_manager.rb', line 28

def initialize(segments_cache, api_manager = nil, logger = nil, proxy_config = nil, timeout: nil)
  @odp_config = nil
  @logger = logger || NoOpLogger.new
  @api_manager = api_manager || OdpSegmentApiManager.new(logger: @logger, proxy_config: proxy_config, timeout: timeout)
  @segments_cache = segments_cache
end

Instance Attribute Details

#api_managerObject (readonly)

Returns the value of attribute api_manager.



26
27
28
# File 'lib/optimizely/odp/odp_segment_manager.rb', line 26

def api_manager
  @api_manager
end

#loggerObject (readonly)

Returns the value of attribute logger.



26
27
28
# File 'lib/optimizely/odp/odp_segment_manager.rb', line 26

def logger
  @logger
end

#odp_configObject

Schedules connections to ODP for audience segmentation and caches the results



25
26
27
# File 'lib/optimizely/odp/odp_segment_manager.rb', line 25

def odp_config
  @odp_config
end

#segments_cacheObject (readonly)

Returns the value of attribute segments_cache.



26
27
28
# File 'lib/optimizely/odp/odp_segment_manager.rb', line 26

def segments_cache
  @segments_cache
end

Instance Method Details

#fetch_qualified_segments(user_key, user_value, options) ⇒ Object

Returns qualified segments for the user from the cache or the ODP server if not in the cache.

Parameters:

  • user_key
    • The key for identifying the id type.

  • user_value
    • The id itself.

  • options
    • An array of OptimizelySegmentOptions used to ignore and/or reset the cache.

Returns:

    • Array of qualified segments.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/optimizely/odp/odp_segment_manager.rb', line 42

def fetch_qualified_segments(user_key, user_value, options)
  odp_api_key = @odp_config&.api_key
  odp_api_host = @odp_config&.api_host
  segments_to_check = @odp_config&.segments_to_check

  if odp_api_key.nil? || odp_api_host.nil?
    @logger.log(Logger::ERROR, format(Optimizely::Helpers::Constants::ODP_LOGS[:FETCH_SEGMENTS_FAILED], 'ODP is not enabled'))
    return nil
  end

  unless segments_to_check&.size&.positive?
    @logger.log(Logger::DEBUG, 'No segments are used in the project. Returning empty list')
    return []
  end

  cache_key = make_cache_key(user_key, user_value)

  ignore_cache = options.include?(OptimizelySegmentOption::IGNORE_CACHE)
  reset_cache = options.include?(OptimizelySegmentOption::RESET_CACHE)

  reset if reset_cache

  unless ignore_cache || reset_cache
    segments = @segments_cache.lookup(cache_key)
    unless segments.nil?
      @logger.log(Logger::DEBUG, 'ODP cache hit. Returning segments from cache.')
      return segments
    end
    @logger.log(Logger::DEBUG, 'ODP cache miss.')
  end

  @logger.log(Logger::DEBUG, 'Making a call to ODP server.')

  segments = @api_manager.fetch_segments(odp_api_key, odp_api_host, user_key, user_value, segments_to_check)
  @segments_cache.save(cache_key, segments) unless segments.nil? || ignore_cache
  segments
end

#resetObject



80
81
82
83
# File 'lib/optimizely/odp/odp_segment_manager.rb', line 80

def reset
  @segments_cache.reset
  nil
end