Class: Optimizely::OptimizelyUserContext

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

Defined Under Namespace

Classes: OptimizelyDecisionContext, OptimizelyForcedDecision

Instance Attribute Summary collapse

Instance Method Summary collapse

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_decisionsObject (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_clientObject (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

#OptimizelyDecisionContextObject (readonly)

Returns the value of attribute OptimizelyDecisionContext.



27
28
29
# File 'lib/optimizely/optimizely_user_context.rb', line 27

def OptimizelyDecisionContext
  @OptimizelyDecisionContext
end

#OptimizelyForcedDecisionObject (readonly)

Returns the value of attribute OptimizelyForcedDecision.



28
29
30
# File 'lib/optimizely/optimizely_user_context.rb', line 28

def OptimizelyForcedDecision
  @OptimizelyForcedDecision
end

#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



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

#cloneObject



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`

Parameters:

  • key

    -A flag key for which a decision will be made

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

Returns:

  • (OptimizelyDecision)

    A decision result



70
71
72
# File 'lib/optimizely/optimizely_user_context.rb', line 70

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:

  • options (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.



94
95
96
# File 'lib/optimizely/optimizely_user_context.rb', line 94

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:

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

  • options (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_for_keys(keys, options = nil)
  @optimizely_client&.decide_for_keys(clone, keys, options)
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.

Parameters:

  • context
    • An OptimizelyDecisionContext object containg flag key and rule key.

Returns:

    • A variation key or nil if forced decisions are not set for the parameters.



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_decisionsObject

Removes all forced decisions bound to this user context.

Returns:

    • true if forced decisions have been removed successfully.



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.

Parameters:

  • context
    • An OptimizelyDecisionContext object containg flag key and rule key.

Returns:

    • true if the forced decision has been removed successfully.



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

Parameters:

  • key
    • An attribute key

  • value
    • An attribute value



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.

Parameters:

  • context
    • An OptimizelyDecisionContext object containg flag key and rule key.

  • decision
    • An OptimizelyForcedDecision object containing variation key

Returns:

    • true if the forced decision has been set successfully.



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

Parameters:

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



164
165
166
# File 'lib/optimizely/optimizely_user_context.rb', line 164

def track_event(event_key, event_tags = nil)
  @optimizely_client&.track(event_key, @user_id, user_attributes, event_tags)
end

#user_attributesObject



48
49
50
# File 'lib/optimizely/optimizely_user_context.rb', line 48

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