Class: Optimizely::OptimizelyUserContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(optimizely_client, user_id, user_attributes) ⇒ OptimizelyUserContext

Returns a new instance of OptimizelyUserContext.



27
28
29
30
31
32
# 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

Instance Attribute Details

#user_idObject (readonly)

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



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

def user_id
  @user_id
end

Instance Method Details

#as_jsonObject



96
97
98
99
100
101
# File 'lib/optimizely/optimizely_user_context.rb', line 96

def as_json
  {
    user_id: @user_id,
    attributes: @user_attributes
  }
end

#cloneObject



34
35
36
# File 'lib/optimizely/optimizely_user_context.rb', line 34

def clone
  OptimizelyUserContext.new(@optimizely_client, @user_id, user_attributes)
end

#decide(key, options = nil) ⇒ OptimizelyDecision

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

Parameters:

  • -A flag key for which a decision will be made

  • (defaults to: nil)
    • A list of options for decision making.

Returns:

  • A decision result



60
61
62
# 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) ⇒ Object

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

Parameters:

  • (defaults to: nil)
    • A list of options for decision making.

Returns:

    • Hash of decisions containing flag keys as hash keys and corresponding decisions as their values.



84
85
86
# 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) ⇒ Object

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.

Parameters:

    • A list of flag keys for which the decisions will be made.

  • (defaults to: nil)
    • A list of options for decision making.

Returns:

    • Hash of decisions containing flag keys as hash keys and corresponding decisions as their values.



74
75
76
# 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) ⇒ Object

Set an attribute for a given key

Parameters:

    • An attribute key

    • An attribute value



47
48
49
# 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) ⇒ Object



103
104
105
# 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) ⇒ Object

Track an event

Parameters:

    • Event key representing the event which needs to be recorded.



92
93
94
# 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_attributesObject



38
39
40
# File 'lib/optimizely/optimizely_user_context.rb', line 38

def user_attributes
  @attr_mutex.synchronize { @user_attributes.clone }
end