Class: Optimizely::OptimizelyUserContext
- Inherits:
-
Object
- Object
- Optimizely::OptimizelyUserContext
- Defined in:
- lib/optimizely/optimizely_user_context.rb
Defined Under Namespace
Classes: OptimizelyDecisionContext, OptimizelyForcedDecision
Instance Attribute Summary collapse
-
#forced_decisions ⇒ Object
readonly
Returns the value of attribute forced_decisions.
-
#optimizely_client ⇒ Object
readonly
Returns the value of attribute optimizely_client.
-
#OptimizelyDecisionContext ⇒ Object
readonly
Returns the value of attribute OptimizelyDecisionContext.
-
#OptimizelyForcedDecision ⇒ Object
readonly
Returns the value of attribute OptimizelyForcedDecision.
-
#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.
- #find_forced_decision(context) ⇒ Object
-
#get_forced_decision(context) ⇒ Object
Returns the forced decision for a given flag and an optional rule.
-
#initialize(optimizely_client, user_id, user_attributes) ⇒ OptimizelyUserContext
constructor
A new instance of OptimizelyUserContext.
-
#remove_all_forced_decisions ⇒ Object
Removes all forced decisions bound to this user context.
-
#remove_forced_decision(context) ⇒ Object
Removes the forced decision for a given flag and an optional rule.
-
#set_attribute(attribute_key, attribute_value) ⇒ Object
Set an attribute for a given key.
-
#set_forced_decision(context, decision) ⇒ Object
Sets the forced decision (variation key) for a given flag and an optional rule.
- #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.
33 34 35 36 37 38 39 40 |
# File 'lib/optimizely/optimizely_user_context.rb', line 33 def initialize(optimizely_client, user_id, user_attributes) @attr_mutex = Mutex.new @forced_decision_mutex = Mutex.new @optimizely_client = optimizely_client @user_id = user_id @user_attributes = user_attributes.nil? ? {} : user_attributes.clone @forced_decisions = {} end |
Instance Attribute Details
#forced_decisions ⇒ Object (readonly)
Returns the value of attribute forced_decisions.
26 27 28 |
# File 'lib/optimizely/optimizely_user_context.rb', line 26 def forced_decisions @forced_decisions end |
#optimizely_client ⇒ Object (readonly)
Returns the value of attribute optimizely_client.
29 30 31 |
# File 'lib/optimizely/optimizely_user_context.rb', line 29 def optimizely_client @optimizely_client end |
#OptimizelyDecisionContext ⇒ Object (readonly)
Returns the value of attribute OptimizelyDecisionContext.
27 28 29 |
# File 'lib/optimizely/optimizely_user_context.rb', line 27 def OptimizelyDecisionContext @OptimizelyDecisionContext end |
#OptimizelyForcedDecision ⇒ Object (readonly)
Returns the value of attribute OptimizelyForcedDecision.
28 29 30 |
# File 'lib/optimizely/optimizely_user_context.rb', line 28 def OptimizelyForcedDecision @OptimizelyForcedDecision end |
#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
168 169 170 171 172 173 |
# File 'lib/optimizely/optimizely_user_context.rb', line 168 def as_json { user_id: @user_id, attributes: @user_attributes } end |
#clone ⇒ Object
42 43 44 45 46 |
# File 'lib/optimizely/optimizely_user_context.rb', line 42 def clone user_context = OptimizelyUserContext.new(@optimizely_client, @user_id, user_attributes) @forced_decision_mutex.synchronize { user_context.instance_variable_set('@forced_decisions', @forced_decisions.dup) unless @forced_decisions.empty? } user_context 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
70 71 72 |
# File 'lib/optimizely/optimizely_user_context.rb', line 70 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.
94 95 96 |
# File 'lib/optimizely/optimizely_user_context.rb', line 94 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.
84 85 86 |
# File 'lib/optimizely/optimizely_user_context.rb', line 84 def decide_for_keys(keys, = nil) @optimizely_client&.decide_for_keys(clone, keys, ) end |
#find_forced_decision(context) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/optimizely/optimizely_user_context.rb', line 114 def find_forced_decision(context) return nil if @forced_decisions.empty? decision = nil @forced_decision_mutex.synchronize { decision = @forced_decisions[context] } decision end |
#get_forced_decision(context) ⇒ Object
Returns the forced decision for a given flag and an optional rule.
128 129 130 |
# File 'lib/optimizely/optimizely_user_context.rb', line 128 def get_forced_decision(context) find_forced_decision(context) end |
#remove_all_forced_decisions ⇒ Object
Removes all forced decisions bound to this user context.
153 154 155 156 157 158 |
# File 'lib/optimizely/optimizely_user_context.rb', line 153 def remove_all_forced_decisions return false if @optimizely_client&.get_optimizely_config.nil? @forced_decision_mutex.synchronize { @forced_decisions.clear } true end |
#remove_forced_decision(context) ⇒ Object
Removes the forced decision for a given flag and an optional rule.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/optimizely/optimizely_user_context.rb', line 138 def remove_forced_decision(context) deleted = false @forced_decision_mutex.synchronize do if @forced_decisions.key?(context) @forced_decisions.delete(context) deleted = true end end deleted end |
#set_attribute(attribute_key, attribute_value) ⇒ Object
Set an attribute for a given key
57 58 59 |
# File 'lib/optimizely/optimizely_user_context.rb', line 57 def set_attribute(attribute_key, attribute_value) @attr_mutex.synchronize { @user_attributes[attribute_key] = attribute_value } end |
#set_forced_decision(context, decision) ⇒ Object
Sets the forced decision (variation key) for a given flag and an optional rule.
105 106 107 108 109 110 111 112 |
# File 'lib/optimizely/optimizely_user_context.rb', line 105 def set_forced_decision(context, decision) flag_key = context[:flag_key] return false if flag_key.nil? @forced_decision_mutex.synchronize { @forced_decisions[context] = decision } true end |
#to_json(*args) ⇒ Object
175 176 177 |
# File 'lib/optimizely/optimizely_user_context.rb', line 175 def to_json(*args) as_json.to_json(*args) end |
#track_event(event_key, event_tags = nil) ⇒ Object
Track an event
164 165 166 |
# File 'lib/optimizely/optimizely_user_context.rb', line 164 def track_event(event_key, = nil) @optimizely_client&.track(event_key, @user_id, user_attributes, ) end |
#user_attributes ⇒ Object
48 49 50 |
# File 'lib/optimizely/optimizely_user_context.rb', line 48 def user_attributes @attr_mutex.synchronize { @user_attributes.clone } end |