optimizely-sdk 3.9.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +202 -202
  3. data/lib/optimizely/audience.rb +127 -97
  4. data/lib/optimizely/bucketer.rb +156 -156
  5. data/lib/optimizely/condition_tree_evaluator.rb +123 -123
  6. data/lib/optimizely/config/datafile_project_config.rb +539 -508
  7. data/lib/optimizely/config/proxy_config.rb +34 -34
  8. data/lib/optimizely/config_manager/async_scheduler.rb +95 -95
  9. data/lib/optimizely/config_manager/http_project_config_manager.rb +330 -321
  10. data/lib/optimizely/config_manager/project_config_manager.rb +24 -24
  11. data/lib/optimizely/config_manager/static_project_config_manager.rb +53 -47
  12. data/lib/optimizely/decide/optimizely_decide_option.rb +28 -28
  13. data/lib/optimizely/decide/optimizely_decision.rb +60 -60
  14. data/lib/optimizely/decide/optimizely_decision_message.rb +26 -26
  15. data/lib/optimizely/decision_service.rb +563 -500
  16. data/lib/optimizely/error_handler.rb +39 -39
  17. data/lib/optimizely/event/batch_event_processor.rb +235 -234
  18. data/lib/optimizely/event/entity/conversion_event.rb +44 -43
  19. data/lib/optimizely/event/entity/decision.rb +38 -38
  20. data/lib/optimizely/event/entity/event_batch.rb +86 -86
  21. data/lib/optimizely/event/entity/event_context.rb +50 -50
  22. data/lib/optimizely/event/entity/impression_event.rb +48 -47
  23. data/lib/optimizely/event/entity/snapshot.rb +33 -33
  24. data/lib/optimizely/event/entity/snapshot_event.rb +48 -48
  25. data/lib/optimizely/event/entity/user_event.rb +22 -22
  26. data/lib/optimizely/event/entity/visitor.rb +36 -35
  27. data/lib/optimizely/event/entity/visitor_attribute.rb +38 -37
  28. data/lib/optimizely/event/event_factory.rb +156 -155
  29. data/lib/optimizely/event/event_processor.rb +25 -25
  30. data/lib/optimizely/event/forwarding_event_processor.rb +44 -43
  31. data/lib/optimizely/event/user_event_factory.rb +88 -88
  32. data/lib/optimizely/event_builder.rb +221 -228
  33. data/lib/optimizely/event_dispatcher.rb +71 -71
  34. data/lib/optimizely/exceptions.rb +135 -139
  35. data/lib/optimizely/helpers/constants.rb +415 -397
  36. data/lib/optimizely/helpers/date_time_utils.rb +30 -30
  37. data/lib/optimizely/helpers/event_tag_utils.rb +132 -132
  38. data/lib/optimizely/helpers/group.rb +31 -31
  39. data/lib/optimizely/helpers/http_utils.rb +65 -64
  40. data/lib/optimizely/helpers/validator.rb +183 -183
  41. data/lib/optimizely/helpers/variable_type.rb +67 -67
  42. data/lib/optimizely/logger.rb +46 -45
  43. data/lib/optimizely/notification_center.rb +174 -176
  44. data/lib/optimizely/optimizely_config.rb +271 -272
  45. data/lib/optimizely/optimizely_factory.rb +181 -181
  46. data/lib/optimizely/optimizely_user_context.rb +204 -107
  47. data/lib/optimizely/params.rb +31 -31
  48. data/lib/optimizely/project_config.rb +99 -91
  49. data/lib/optimizely/semantic_version.rb +166 -166
  50. data/lib/optimizely/{custom_attribute_condition_evaluator.rb → user_condition_evaluator.rb} +391 -369
  51. data/lib/optimizely/user_profile_service.rb +35 -35
  52. data/lib/optimizely/version.rb +21 -21
  53. data/lib/optimizely.rb +1130 -1117
  54. metadata +13 -13
@@ -1,107 +1,204 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Copyright 2020, Optimizely and contributors
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require 'json'
20
-
21
- module Optimizely
22
- class OptimizelyUserContext
23
- # Representation of an Optimizely User Context using which APIs are to be called.
24
-
25
- attr_reader :user_id
26
-
27
- def initialize(optimizely_client, user_id, user_attributes)
28
- @attr_mutex = Mutex.new
29
- @optimizely_client = optimizely_client
30
- @user_id = user_id
31
- @user_attributes = user_attributes.nil? ? {} : user_attributes.clone
32
- end
33
-
34
- def clone
35
- OptimizelyUserContext.new(@optimizely_client, @user_id, user_attributes)
36
- end
37
-
38
- def user_attributes
39
- @attr_mutex.synchronize { @user_attributes.clone }
40
- end
41
-
42
- # Set an attribute for a given key
43
- #
44
- # @param key - An attribute key
45
- # @param value - An attribute value
46
-
47
- def set_attribute(attribute_key, attribute_value)
48
- @attr_mutex.synchronize { @user_attributes[attribute_key] = attribute_value }
49
- end
50
-
51
- # Returns a decision result (OptimizelyDecision) for a given flag key and a user context, which contains all data required to deliver the flag.
52
- #
53
- # 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`
54
- #
55
- # @param key -A flag key for which a decision will be made
56
- # @param options - A list of options for decision making.
57
- #
58
- # @return [OptimizelyDecision] A decision result
59
-
60
- def decide(key, options = nil)
61
- @optimizely_client&.decide(clone, key, options)
62
- end
63
-
64
- # Returns a hash of decision results (OptimizelyDecision) for multiple flag keys and a user context.
65
- #
66
- # If the SDK finds an error for a key, the response will include a decision for the key showing `reasons` for the error.
67
- # The SDK will always return hash of decisions. When it can not process requests, it'll return an empty hash after logging the errors.
68
- #
69
- # @param keys - A list of flag keys for which the decisions will be made.
70
- # @param options - A list of options for decision making.
71
- #
72
- # @return - Hash of decisions containing flag keys as hash keys and corresponding decisions as their values.
73
-
74
- def decide_for_keys(keys, options = nil)
75
- @optimizely_client&.decide_for_keys(clone, keys, options)
76
- end
77
-
78
- # Returns a hash of decision results (OptimizelyDecision) for all active flag keys.
79
- #
80
- # @param options - A list of options for decision making.
81
- #
82
- # @return - Hash of decisions containing flag keys as hash keys and corresponding decisions as their values.
83
-
84
- def decide_all(options = nil)
85
- @optimizely_client&.decide_all(clone, options)
86
- end
87
-
88
- # Track an event
89
- #
90
- # @param event_key - Event key representing the event which needs to be recorded.
91
-
92
- def track_event(event_key, event_tags = nil)
93
- @optimizely_client&.track(event_key, @user_id, user_attributes, event_tags)
94
- end
95
-
96
- def as_json
97
- {
98
- user_id: @user_id,
99
- attributes: @user_attributes
100
- }
101
- end
102
-
103
- def to_json(*args)
104
- as_json.to_json(*args)
105
- end
106
- end
107
- end
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright 2020-2022, Optimizely and contributors
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'json'
20
+
21
+ module Optimizely
22
+ class OptimizelyUserContext
23
+ # Representation of an Optimizely User Context using which APIs are to be called.
24
+
25
+ attr_reader :user_id, :forced_decisions, :optimizely_client
26
+
27
+ OptimizelyDecisionContext = Struct.new(:flag_key, :rule_key)
28
+ OptimizelyForcedDecision = Struct.new(:variation_key)
29
+ def initialize(optimizely_client, user_id, user_attributes)
30
+ @attr_mutex = Mutex.new
31
+ @forced_decision_mutex = Mutex.new
32
+ @qualified_segment_mutex = Mutex.new
33
+ @optimizely_client = optimizely_client
34
+ @user_id = user_id
35
+ @user_attributes = user_attributes.nil? ? {} : user_attributes.clone
36
+ @forced_decisions = {}
37
+ @qualified_segments = []
38
+ end
39
+
40
+ def clone
41
+ user_context = OptimizelyUserContext.new(@optimizely_client, @user_id, user_attributes)
42
+ @forced_decision_mutex.synchronize { user_context.instance_variable_set('@forced_decisions', @forced_decisions.dup) unless @forced_decisions.empty? }
43
+ @qualified_segment_mutex.synchronize { user_context.instance_variable_set('@qualified_segments', @qualified_segments.dup) unless @qualified_segments.empty? }
44
+ user_context
45
+ end
46
+
47
+ def user_attributes
48
+ @attr_mutex.synchronize { @user_attributes.clone }
49
+ end
50
+
51
+ # Set an attribute for a given key
52
+ #
53
+ # @param key - An attribute key
54
+ # @param value - An attribute value
55
+
56
+ def set_attribute(attribute_key, attribute_value)
57
+ @attr_mutex.synchronize { @user_attributes[attribute_key] = attribute_value }
58
+ end
59
+
60
+ # Returns a decision result (OptimizelyDecision) for a given flag key and a user context, which contains all data required to deliver the flag.
61
+ #
62
+ # 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`
63
+ #
64
+ # @param key -A flag key for which a decision will be made
65
+ # @param options - A list of options for decision making.
66
+ #
67
+ # @return [OptimizelyDecision] A decision result
68
+
69
+ def decide(key, options = nil)
70
+ @optimizely_client&.decide(clone, key, options)
71
+ end
72
+
73
+ # Returns a hash of decision results (OptimizelyDecision) for multiple flag keys and a user context.
74
+ #
75
+ # If the SDK finds an error for a key, the response will include a decision for the key showing `reasons` for the error.
76
+ # The SDK will always return hash of decisions. When it can not process requests, it'll return an empty hash after logging the errors.
77
+ #
78
+ # @param keys - A list of flag keys for which the decisions will be made.
79
+ # @param options - A list of options for decision making.
80
+ #
81
+ # @return - Hash of decisions containing flag keys as hash keys and corresponding decisions as their values.
82
+
83
+ def decide_for_keys(keys, options = nil)
84
+ @optimizely_client&.decide_for_keys(clone, keys, options)
85
+ end
86
+
87
+ # Returns a hash of decision results (OptimizelyDecision) for all active flag keys.
88
+ #
89
+ # @param options - A list of options for decision making.
90
+ #
91
+ # @return - Hash of decisions containing flag keys as hash keys and corresponding decisions as their values.
92
+
93
+ def decide_all(options = nil)
94
+ @optimizely_client&.decide_all(clone, options)
95
+ end
96
+
97
+ # Sets the forced decision (variation key) for a given flag and an optional rule.
98
+ #
99
+ # @param context - An OptimizelyDecisionContext object containg flag key and rule key.
100
+ # @param decision - An OptimizelyForcedDecision object containing variation key
101
+ #
102
+ # @return - true if the forced decision has been set successfully.
103
+
104
+ def set_forced_decision(context, decision)
105
+ flag_key = context[:flag_key]
106
+ return false if flag_key.nil?
107
+
108
+ @forced_decision_mutex.synchronize { @forced_decisions[context] = decision }
109
+
110
+ true
111
+ end
112
+
113
+ def find_forced_decision(context)
114
+ return nil if @forced_decisions.empty?
115
+
116
+ decision = nil
117
+ @forced_decision_mutex.synchronize { decision = @forced_decisions[context] }
118
+ decision
119
+ end
120
+
121
+ # Returns the forced decision for a given flag and an optional rule.
122
+ #
123
+ # @param context - An OptimizelyDecisionContext object containg flag key and rule key.
124
+ #
125
+ # @return - A variation key or nil if forced decisions are not set for the parameters.
126
+
127
+ def get_forced_decision(context)
128
+ find_forced_decision(context)
129
+ end
130
+
131
+ # Removes the forced decision for a given flag and an optional rule.
132
+ #
133
+ # @param context - An OptimizelyDecisionContext object containg flag key and rule key.
134
+ #
135
+ # @return - true if the forced decision has been removed successfully.
136
+
137
+ def remove_forced_decision(context)
138
+ deleted = false
139
+ @forced_decision_mutex.synchronize do
140
+ if @forced_decisions.key?(context)
141
+ @forced_decisions.delete(context)
142
+ deleted = true
143
+ end
144
+ end
145
+ deleted
146
+ end
147
+
148
+ # Removes all forced decisions bound to this user context.
149
+ #
150
+ # @return - true if forced decisions have been removed successfully.
151
+
152
+ def remove_all_forced_decisions
153
+ return false if @optimizely_client&.get_optimizely_config.nil?
154
+
155
+ @forced_decision_mutex.synchronize { @forced_decisions.clear }
156
+ true
157
+ end
158
+
159
+ # Track an event
160
+ #
161
+ # @param event_key - Event key representing the event which needs to be recorded.
162
+
163
+ def track_event(event_key, event_tags = nil)
164
+ @optimizely_client&.track(event_key, @user_id, user_attributes, event_tags)
165
+ end
166
+
167
+ def as_json
168
+ {
169
+ user_id: @user_id,
170
+ attributes: @user_attributes
171
+ }
172
+ end
173
+
174
+ def to_json(*args)
175
+ as_json.to_json(*args)
176
+ end
177
+
178
+ # Returns An array of qualified segments for this user
179
+ #
180
+ # @return - An array of segments names.
181
+
182
+ def qualified_segments
183
+ @qualified_segment_mutex.synchronize { @qualified_segments.clone }
184
+ end
185
+
186
+ # Replace qualified segments with provided segments
187
+ #
188
+ # @param segments - An array of segment names
189
+
190
+ def qualified_segments=(segments)
191
+ @qualified_segment_mutex.synchronize { @qualified_segments = segments.clone }
192
+ end
193
+
194
+ # Checks if user is qualified for the provided segment.
195
+ #
196
+ # @param segment - A segment name
197
+
198
+ def qualified_for?(segment)
199
+ return false if @qualified_segments.empty?
200
+
201
+ @qualified_segment_mutex.synchronize { @qualified_segments.include?(segment) }
202
+ end
203
+ end
204
+ end
@@ -1,31 +1,31 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Copyright 2016-2017, Optimizely and contributors
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
- module Optimizely
19
- class Params
20
- ACCOUNT_ID = 'd'
21
- PROJECT_ID = 'a'
22
- EXPERIMENT_PREFIX = 'x'
23
- GOAL_ID = 'g'
24
- GOAL_NAME = 'n'
25
- SEGMENT_PREFIX = 's'
26
- END_USER_ID = 'u'
27
- EVENT_VALUE = 'v'
28
- SOURCE = 'src'
29
- TIME = 'time'
30
- end
31
- end
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright 2016-2017, Optimizely and contributors
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ module Optimizely
19
+ class Params
20
+ ACCOUNT_ID = 'd'
21
+ PROJECT_ID = 'a'
22
+ EXPERIMENT_PREFIX = 'x'
23
+ GOAL_ID = 'g'
24
+ GOAL_NAME = 'n'
25
+ SEGMENT_PREFIX = 's'
26
+ END_USER_ID = 'u'
27
+ EVENT_VALUE = 'v'
28
+ SOURCE = 'src'
29
+ TIME = 'time'
30
+ end
31
+ end
@@ -1,91 +1,99 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2016-2021, Optimizely and contributors
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- module Optimizely
19
- class ProjectConfig
20
- # ProjectConfig is an interface capturing the experiment, variation and feature definitions.
21
- # The default implementation of ProjectConfig can be found in DatafileProjectConfig.
22
-
23
- def datafile; end
24
-
25
- def account_id; end
26
-
27
- def attributes; end
28
-
29
- def audiences; end
30
-
31
- def typed_audiences; end
32
-
33
- def events; end
34
-
35
- def experiments; end
36
-
37
- def feature_flags; end
38
-
39
- def groups; end
40
-
41
- def project_id; end
42
-
43
- def anonymize_ip; end
44
-
45
- def bot_filtering; end
46
-
47
- def revision; end
48
-
49
- def sdk_key; end
50
-
51
- def environment_key; end
52
-
53
- def send_flag_decisions; end
54
-
55
- def rollouts; end
56
-
57
- def experiment_running?(experiment); end
58
-
59
- def get_experiment_from_key(experiment_key); end
60
-
61
- def get_experiment_from_id(experiment_id); end
62
-
63
- def get_experiment_key(experiment_id); end
64
-
65
- def get_event_from_key(event_key); end
66
-
67
- def get_audience_from_id(audience_id); end
68
-
69
- def get_variation_from_id(experiment_key, variation_id); end
70
-
71
- def get_variation_from_id_by_experiment_id(experiment_id, variation_id); end
72
-
73
- def get_variation_id_from_key_by_experiment_id(experiment_id, variation_key); end
74
-
75
- def get_variation_id_from_key(experiment_key, variation_key); end
76
-
77
- def get_whitelisted_variations(experiment_id); end
78
-
79
- def get_attribute_id(attribute_key); end
80
-
81
- def variation_id_exists?(experiment_id, variation_id); end
82
-
83
- def get_feature_flag_from_key(feature_flag_key); end
84
-
85
- def get_feature_variable(feature_flag, variable_key); end
86
-
87
- def get_rollout_from_id(rollout_id); end
88
-
89
- def feature_experiment?(experiment_id); end
90
- end
91
- end
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016-2021, Optimizely and contributors
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module Optimizely
19
+ class ProjectConfig
20
+ # ProjectConfig is an interface capturing the experiment, variation and feature definitions.
21
+ # The default implementation of ProjectConfig can be found in DatafileProjectConfig.
22
+
23
+ def datafile; end
24
+
25
+ def account_id; end
26
+
27
+ def attributes; end
28
+
29
+ def audiences; end
30
+
31
+ def typed_audiences; end
32
+
33
+ def events; end
34
+
35
+ def experiments; end
36
+
37
+ def feature_flags; end
38
+
39
+ def groups; end
40
+
41
+ def project_id; end
42
+
43
+ def anonymize_ip; end
44
+
45
+ def bot_filtering; end
46
+
47
+ def revision; end
48
+
49
+ def sdk_key; end
50
+
51
+ def environment_key; end
52
+
53
+ def send_flag_decisions; end
54
+
55
+ def rollouts; end
56
+
57
+ def integrations; end
58
+
59
+ def public_key_for_odp; end
60
+
61
+ def host_for_odp; end
62
+
63
+ def all_segments; end
64
+
65
+ def experiment_running?(experiment); end
66
+
67
+ def get_experiment_from_key(experiment_key); end
68
+
69
+ def get_experiment_from_id(experiment_id); end
70
+
71
+ def get_experiment_key(experiment_id); end
72
+
73
+ def get_event_from_key(event_key); end
74
+
75
+ def get_audience_from_id(audience_id); end
76
+
77
+ def get_variation_from_id(experiment_key, variation_id); end
78
+
79
+ def get_variation_from_id_by_experiment_id(experiment_id, variation_id); end
80
+
81
+ def get_variation_id_from_key_by_experiment_id(experiment_id, variation_key); end
82
+
83
+ def get_variation_id_from_key(experiment_key, variation_key); end
84
+
85
+ def get_whitelisted_variations(experiment_id); end
86
+
87
+ def get_attribute_id(attribute_key); end
88
+
89
+ def variation_id_exists?(experiment_id, variation_id); end
90
+
91
+ def get_feature_flag_from_key(feature_flag_key); end
92
+
93
+ def get_feature_variable(feature_flag, variable_key); end
94
+
95
+ def get_rollout_from_id(rollout_id); end
96
+
97
+ def feature_experiment?(experiment_id); end
98
+ end
99
+ end