Class: Optimizely::OptimizelyUserContext
- Inherits:
-
Object
- Object
- Optimizely::OptimizelyUserContext
- Defined in:
- lib/optimizely/optimizely_user_context.rb
Instance Attribute Summary collapse
-
#user_id ⇒ Object
readonly
Representation of an Optimizely User Context using which APIs are to be called.
Instance Method Summary collapse
- #as_json ⇒ Object
- #clone ⇒ Object
-
#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.
-
#decide_all(options = nil) ⇒ Object
Returns a hash of decision results (OptimizelyDecision) for all active flag keys.
-
#decide_for_keys(keys, options = nil) ⇒ Object
Returns a hash of decision results (OptimizelyDecision) for multiple flag keys and a user context.
-
#initialize(optimizely_client, user_id, user_attributes) ⇒ OptimizelyUserContext
constructor
A new instance of OptimizelyUserContext.
-
#set_attribute(attribute_key, attribute_value) ⇒ Object
Set an attribute for a given key.
- #to_json(*args) ⇒ Object
-
#track_event(event_key, event_tags = nil) ⇒ Object
Track an event.
- #user_attributes ⇒ Object
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_id ⇒ Object (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_json ⇒ Object
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 |
#clone ⇒ Object
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
60 61 62 |
# File 'lib/optimizely/optimizely_user_context.rb', line 60 def decide(key, = nil) @optimizely_client&.decide(clone, key, ) end |
#decide_all(options = nil) ⇒ Object
Returns a hash of decision results (OptimizelyDecision) for all active flag keys.
84 85 86 |
# File 'lib/optimizely/optimizely_user_context.rb', line 84 def decide_all( = nil) @optimizely_client&.decide_all(clone, ) 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.
74 75 76 |
# File 'lib/optimizely/optimizely_user_context.rb', line 74 def decide_for_keys(keys, = nil) @optimizely_client&.decide_for_keys(clone, keys, ) end |
#set_attribute(attribute_key, attribute_value) ⇒ Object
Set an attribute for a given key
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
92 93 94 |
# File 'lib/optimizely/optimizely_user_context.rb', line 92 def track_event(event_key, = nil) @optimizely_client&.track(event_key, @user_id, user_attributes, ) end |
#user_attributes ⇒ Object
38 39 40 |
# File 'lib/optimizely/optimizely_user_context.rb', line 38 def user_attributes @attr_mutex.synchronize { @user_attributes.clone } end |