class Optimizely::OptimizelyUserContext

Attributes

user_id[R]

Representation of an Optimizely User Context using which APIs are to be called.

Public Class Methods

new(optimizely_client, user_id, user_attributes) click to toggle source
# File lib/optimizely/optimizely_user_context.rb, line 27
def initialize(optimizely_client, user_id, user_attributes)
  @attr_mutex = Mutex.new
  @optimizely_client = optimizely_client
  @user_id = user_id
  @user_attributes = user_attributes.nil? ? {} : user_attributes.clone
end

Public Instance Methods

as_json() click to toggle source
# File lib/optimizely/optimizely_user_context.rb, line 96
def as_json
  {
    user_id: @user_id,
    attributes: @user_attributes
  }
end
clone() click to toggle source
# File lib/optimizely/optimizely_user_context.rb, line 34
def clone
  OptimizelyUserContext.new(@optimizely_client, @user_id, user_attributes)
end
decide(key, options = nil) click to toggle source

Returns a decision result (OptimizelyDecision) for a given flag key and a user context, which contains all data required to deliver the flag.

If the SDK finds an error, it'll return a `decision` with nil for `variation_key`. The decision will include an error message in `reasons`

@param key -A flag key for which a decision will be made @param options - A list of options for decision making.

@return [OptimizelyDecision] A decision result

# File lib/optimizely/optimizely_user_context.rb, line 60
def decide(key, options = nil)
  @optimizely_client&.decide(clone, key, options)
end
decide_all(options = nil) click to toggle source

Returns a hash of decision results (OptimizelyDecision) for all active flag keys.

@param options - A list of options for decision making.

@return - Hash of decisions containing flag keys as hash keys and corresponding decisions as their values.

# File lib/optimizely/optimizely_user_context.rb, line 84
def decide_all(options = nil)
  @optimizely_client&.decide_all(clone, options)
end
decide_for_keys(keys, options = nil) click to toggle source

Returns a hash of decision results (OptimizelyDecision) for multiple flag keys and a user context.

If the SDK finds an error for a key, the response will include a decision for the key showing `reasons` for the error. The SDK will always return hash of decisions. When it can not process requests, it'll return an empty hash after logging the errors.

@param keys - A list of flag keys for which the decisions will be made. @param options - A list of options for decision making.

@return - Hash of decisions containing flag keys as hash keys and corresponding decisions as their values.

# File lib/optimizely/optimizely_user_context.rb, line 74
def decide_for_keys(keys, options = nil)
  @optimizely_client&.decide_for_keys(clone, keys, options)
end
set_attribute(attribute_key, attribute_value) click to toggle source

Set an attribute for a given key

@param key - An attribute key @param value - An attribute value

# File lib/optimizely/optimizely_user_context.rb, line 47
def set_attribute(attribute_key, attribute_value)
  @attr_mutex.synchronize { @user_attributes[attribute_key] = attribute_value }
end
to_json(*args) click to toggle source
# File lib/optimizely/optimizely_user_context.rb, line 103
def to_json(*args)
  as_json.to_json(*args)
end
track_event(event_key, event_tags = nil) click to toggle source

Track an event

@param event_key - Event key representing the event which needs to be recorded.

# File lib/optimizely/optimizely_user_context.rb, line 92
def track_event(event_key, event_tags = nil)
  @optimizely_client&.track(event_key, @user_id, user_attributes, event_tags)
end
user_attributes() click to toggle source
# File lib/optimizely/optimizely_user_context.rb, line 38
def user_attributes
  @attr_mutex.synchronize { @user_attributes.clone }
end