optimizely-sdk 5.0.0 → 5.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +202 -202
- data/lib/optimizely/audience.rb +127 -127
- data/lib/optimizely/bucketer.rb +156 -156
- data/lib/optimizely/condition_tree_evaluator.rb +123 -123
- data/lib/optimizely/config/datafile_project_config.rb +558 -558
- data/lib/optimizely/config/proxy_config.rb +34 -34
- data/lib/optimizely/config_manager/async_scheduler.rb +95 -95
- data/lib/optimizely/config_manager/http_project_config_manager.rb +340 -340
- data/lib/optimizely/config_manager/project_config_manager.rb +25 -25
- data/lib/optimizely/config_manager/static_project_config_manager.rb +55 -55
- data/lib/optimizely/decide/optimizely_decide_option.rb +28 -28
- data/lib/optimizely/decide/optimizely_decision.rb +60 -60
- data/lib/optimizely/decide/optimizely_decision_message.rb +26 -26
- data/lib/optimizely/decision_service.rb +563 -563
- data/lib/optimizely/error_handler.rb +39 -39
- data/lib/optimizely/event/batch_event_processor.rb +235 -235
- data/lib/optimizely/event/entity/conversion_event.rb +44 -44
- data/lib/optimizely/event/entity/decision.rb +38 -38
- data/lib/optimizely/event/entity/event_batch.rb +86 -86
- data/lib/optimizely/event/entity/event_context.rb +50 -50
- data/lib/optimizely/event/entity/impression_event.rb +48 -48
- data/lib/optimizely/event/entity/snapshot.rb +33 -33
- data/lib/optimizely/event/entity/snapshot_event.rb +48 -48
- data/lib/optimizely/event/entity/user_event.rb +22 -22
- data/lib/optimizely/event/entity/visitor.rb +36 -36
- data/lib/optimizely/event/entity/visitor_attribute.rb +38 -38
- data/lib/optimizely/event/event_factory.rb +156 -156
- data/lib/optimizely/event/event_processor.rb +25 -25
- data/lib/optimizely/event/forwarding_event_processor.rb +44 -44
- data/lib/optimizely/event/user_event_factory.rb +88 -88
- data/lib/optimizely/event_builder.rb +221 -221
- data/lib/optimizely/event_dispatcher.rb +69 -69
- data/lib/optimizely/exceptions.rb +193 -193
- data/lib/optimizely/helpers/constants.rb +459 -459
- data/lib/optimizely/helpers/date_time_utils.rb +30 -30
- data/lib/optimizely/helpers/event_tag_utils.rb +132 -132
- data/lib/optimizely/helpers/group.rb +31 -31
- data/lib/optimizely/helpers/http_utils.rb +68 -68
- data/lib/optimizely/helpers/sdk_settings.rb +61 -61
- data/lib/optimizely/helpers/validator.rb +236 -236
- data/lib/optimizely/helpers/variable_type.rb +67 -67
- data/lib/optimizely/logger.rb +46 -46
- data/lib/optimizely/notification_center.rb +174 -174
- data/lib/optimizely/notification_center_registry.rb +71 -71
- data/lib/optimizely/odp/lru_cache.rb +114 -114
- data/lib/optimizely/odp/odp_config.rb +102 -102
- data/lib/optimizely/odp/odp_event.rb +75 -75
- data/lib/optimizely/odp/odp_event_api_manager.rb +70 -70
- data/lib/optimizely/odp/odp_event_manager.rb +286 -286
- data/lib/optimizely/odp/odp_manager.rb +159 -159
- data/lib/optimizely/odp/odp_segment_api_manager.rb +122 -122
- data/lib/optimizely/odp/odp_segment_manager.rb +97 -97
- data/lib/optimizely/optimizely_config.rb +273 -273
- data/lib/optimizely/optimizely_factory.rb +184 -184
- data/lib/optimizely/optimizely_user_context.rb +238 -238
- data/lib/optimizely/params.rb +31 -31
- data/lib/optimizely/project_config.rb +99 -99
- data/lib/optimizely/semantic_version.rb +166 -166
- data/lib/optimizely/user_condition_evaluator.rb +391 -391
- data/lib/optimizely/user_profile_service.rb +35 -35
- data/lib/optimizely/version.rb +21 -21
- data/lib/optimizely.rb +1262 -1262
- metadata +7 -5
@@ -1,236 +1,236 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
#
|
4
|
-
# Copyright 2016-2019, 2022-2023, 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
|
-
require_relative 'constants'
|
19
|
-
require 'json'
|
20
|
-
require 'json-schema'
|
21
|
-
|
22
|
-
module Optimizely
|
23
|
-
module Helpers
|
24
|
-
module Validator
|
25
|
-
module_function
|
26
|
-
|
27
|
-
def attributes_valid?(attributes)
|
28
|
-
# Determines if provided attributes are valid.
|
29
|
-
#
|
30
|
-
# attributes - User attributes to be validated.
|
31
|
-
#
|
32
|
-
# Returns boolean depending on validity of attributes.
|
33
|
-
|
34
|
-
attributes.is_a?(Hash)
|
35
|
-
end
|
36
|
-
|
37
|
-
def attribute_valid?(attribute_key, attribute_value)
|
38
|
-
# Determines if provided attribute_key and attribute_value are valid.
|
39
|
-
#
|
40
|
-
# attribute_key - Variable which needs to be validated.
|
41
|
-
# attribute_value - Variable which needs to be validated.
|
42
|
-
#
|
43
|
-
# Returns boolean depending on validity of attribute_key and attribute_value.
|
44
|
-
|
45
|
-
return false unless attribute_key.is_a?(String) || attribute_key.is_a?(Symbol)
|
46
|
-
|
47
|
-
return true if (boolean? attribute_value) || (attribute_value.is_a? String)
|
48
|
-
|
49
|
-
finite_number?(attribute_value)
|
50
|
-
end
|
51
|
-
|
52
|
-
def event_tags_valid?(event_tags)
|
53
|
-
# Determines if provided event tags are valid.
|
54
|
-
#
|
55
|
-
# event_tags - Event tags to be validated.
|
56
|
-
#
|
57
|
-
# Returns boolean depending on validity of event tags.
|
58
|
-
|
59
|
-
event_tags.is_a?(Hash)
|
60
|
-
end
|
61
|
-
|
62
|
-
def datafile_valid?(datafile)
|
63
|
-
# Determines if a given datafile is valid.
|
64
|
-
#
|
65
|
-
# datafile - String JSON representing the project.
|
66
|
-
#
|
67
|
-
# Returns boolean depending on validity of datafile.
|
68
|
-
|
69
|
-
begin
|
70
|
-
datafile = JSON.parse(datafile)
|
71
|
-
rescue
|
72
|
-
return false
|
73
|
-
end
|
74
|
-
|
75
|
-
JSON::Validator.validate(Helpers::Constants::JSON_SCHEMA_V2, datafile)
|
76
|
-
end
|
77
|
-
|
78
|
-
def error_handler_valid?(error_handler)
|
79
|
-
# Determines if a given error handler is valid.
|
80
|
-
#
|
81
|
-
# error_handler - error_handler to be validated.
|
82
|
-
#
|
83
|
-
# Returns boolean depending on whether error_handler has a handle_error method.
|
84
|
-
|
85
|
-
error_handler.respond_to?(:handle_error)
|
86
|
-
end
|
87
|
-
|
88
|
-
def event_dispatcher_valid?(event_dispatcher)
|
89
|
-
# Determines if a given event dispatcher is valid.
|
90
|
-
#
|
91
|
-
# event_dispatcher - event_dispatcher to be validated.
|
92
|
-
#
|
93
|
-
# Returns boolean depending on whether event_dispatcher has a dispatch_event method.
|
94
|
-
|
95
|
-
event_dispatcher.respond_to?(:dispatch_event)
|
96
|
-
end
|
97
|
-
|
98
|
-
def logger_valid?(logger)
|
99
|
-
# Determines if a given logger is valid.
|
100
|
-
#
|
101
|
-
# logger - logger to be validated.
|
102
|
-
#
|
103
|
-
# Returns boolean depending on whether logger has a log method.
|
104
|
-
|
105
|
-
logger.respond_to?(:log)
|
106
|
-
end
|
107
|
-
|
108
|
-
def string_numeric?(str)
|
109
|
-
!Float(str).nil?
|
110
|
-
rescue
|
111
|
-
false
|
112
|
-
end
|
113
|
-
|
114
|
-
def inputs_valid?(variables, logger = NoOpLogger.new, level = Logger::ERROR)
|
115
|
-
# Determines if values of variables in given array are non empty string.
|
116
|
-
#
|
117
|
-
# variables - array values to validate.
|
118
|
-
#
|
119
|
-
# logger - logger.
|
120
|
-
#
|
121
|
-
# Returns boolean True if all of the values are valid, False otherwise.
|
122
|
-
|
123
|
-
return false unless variables.respond_to?(:each) && !variables.empty?
|
124
|
-
|
125
|
-
is_valid = true
|
126
|
-
if variables.include? :user_id
|
127
|
-
# Empty str is a valid user ID.
|
128
|
-
unless variables[:user_id].is_a?(String)
|
129
|
-
is_valid = false
|
130
|
-
logger.log(level, "#{Constants::INPUT_VARIABLES['USER_ID']} is invalid")
|
131
|
-
end
|
132
|
-
variables.delete :user_id
|
133
|
-
end
|
134
|
-
|
135
|
-
if variables.include? :variable_type
|
136
|
-
# Empty variable_type is a valid user ID.
|
137
|
-
unless variables[:variable_type].is_a?(String) || !variables[:variable_type]
|
138
|
-
is_valid = false
|
139
|
-
logger.log(level, "#{Constants::INPUT_VARIABLES['VARIABLE_TYPE']} is invalid")
|
140
|
-
end
|
141
|
-
variables.delete :variable_type
|
142
|
-
end
|
143
|
-
|
144
|
-
variables.each do |key, value|
|
145
|
-
next if value.is_a?(String) && !value.empty?
|
146
|
-
|
147
|
-
is_valid = false
|
148
|
-
next unless logger_valid?(logger) && level
|
149
|
-
|
150
|
-
logger.log(level, "#{Constants::INPUT_VARIABLES[key.to_s.upcase]} is invalid")
|
151
|
-
end
|
152
|
-
is_valid
|
153
|
-
end
|
154
|
-
|
155
|
-
def boolean?(value)
|
156
|
-
# Returns true if given value type is boolean.
|
157
|
-
# false otherwise.
|
158
|
-
|
159
|
-
value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
160
|
-
end
|
161
|
-
|
162
|
-
def same_types?(value_1, value_2)
|
163
|
-
# Returns true if given values are of same types.
|
164
|
-
# false otherwise.
|
165
|
-
# Numeric values are considered as same type.
|
166
|
-
|
167
|
-
return true if value_1.is_a?(Numeric) && value_2.is_a?(Numeric)
|
168
|
-
|
169
|
-
return true if boolean?(value_1) && boolean?(value_2)
|
170
|
-
|
171
|
-
value_1.instance_of?(value_2.class)
|
172
|
-
end
|
173
|
-
|
174
|
-
def finite_number?(value)
|
175
|
-
# Returns true if the given value is a number, enforces
|
176
|
-
# absolute limit of 2^53 and restricts NaN, Infinity, -Infinity.
|
177
|
-
# false otherwise.
|
178
|
-
|
179
|
-
value.is_a?(Numeric) && value.to_f.finite? && value.abs <= Constants::FINITE_NUMBER_LIMIT
|
180
|
-
end
|
181
|
-
|
182
|
-
def odp_data_types_valid?(data)
|
183
|
-
valid_types = [String, Float, Integer, TrueClass, FalseClass, NilClass]
|
184
|
-
data&.values&.all? { |e| valid_types.member? e.class }
|
185
|
-
end
|
186
|
-
|
187
|
-
def segments_cache_valid?(segments_cache)
|
188
|
-
# Determines if a given segments_cache is valid.
|
189
|
-
#
|
190
|
-
# segments_cache - custom cache to be validated.
|
191
|
-
#
|
192
|
-
# Returns boolean depending on whether cache has required methods.
|
193
|
-
|
194
|
-
segments_cache.respond_to?(:reset) &&
|
195
|
-
segments_cache.method(:reset)&.parameters&.empty? &&
|
196
|
-
segments_cache.respond_to?(:lookup) &&
|
197
|
-
segments_cache.method(:lookup)&.parameters&.length&.positive? &&
|
198
|
-
segments_cache.respond_to?(:save) &&
|
199
|
-
segments_cache.method(:save)&.parameters&.length&.positive?
|
200
|
-
end
|
201
|
-
|
202
|
-
def segment_manager_valid?(segment_manager)
|
203
|
-
# Determines if a given segment_manager is valid.
|
204
|
-
#
|
205
|
-
# segment_manager - custom manager to be validated.
|
206
|
-
#
|
207
|
-
# Returns boolean depending on whether manager has required methods.
|
208
|
-
|
209
|
-
segment_manager.respond_to?(:odp_config) &&
|
210
|
-
segment_manager.respond_to?(:reset) &&
|
211
|
-
segment_manager.method(:reset)&.parameters&.empty? &&
|
212
|
-
segment_manager.respond_to?(:fetch_qualified_segments) &&
|
213
|
-
(segment_manager.method(:fetch_qualified_segments)&.parameters&.length || 0) >= 3
|
214
|
-
end
|
215
|
-
|
216
|
-
def event_manager_valid?(event_manager)
|
217
|
-
# Determines if a given event_manager is valid.
|
218
|
-
#
|
219
|
-
# event_manager - custom manager to be validated.
|
220
|
-
#
|
221
|
-
# Returns boolean depending on whether manager has required method and parameters.
|
222
|
-
return false unless
|
223
|
-
event_manager.respond_to?(:send_event) &&
|
224
|
-
event_manager.respond_to?(:start!) &&
|
225
|
-
(event_manager.method(:start!)&.parameters&.length || 0) >= 1 &&
|
226
|
-
event_manager.respond_to?(:update_config) &&
|
227
|
-
event_manager.respond_to?(:stop!)
|
228
|
-
|
229
|
-
required_parameters = Set[%i[keyreq type], %i[keyreq action], %i[keyreq identifiers], %i[keyreq data]]
|
230
|
-
existing_parameters = event_manager.method(:send_event).parameters.to_set
|
231
|
-
|
232
|
-
existing_parameters & required_parameters == required_parameters
|
233
|
-
end
|
234
|
-
end
|
235
|
-
end
|
236
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright 2016-2019, 2022-2023, 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
|
+
require_relative 'constants'
|
19
|
+
require 'json'
|
20
|
+
require 'json-schema'
|
21
|
+
|
22
|
+
module Optimizely
|
23
|
+
module Helpers
|
24
|
+
module Validator
|
25
|
+
module_function
|
26
|
+
|
27
|
+
def attributes_valid?(attributes)
|
28
|
+
# Determines if provided attributes are valid.
|
29
|
+
#
|
30
|
+
# attributes - User attributes to be validated.
|
31
|
+
#
|
32
|
+
# Returns boolean depending on validity of attributes.
|
33
|
+
|
34
|
+
attributes.is_a?(Hash)
|
35
|
+
end
|
36
|
+
|
37
|
+
def attribute_valid?(attribute_key, attribute_value)
|
38
|
+
# Determines if provided attribute_key and attribute_value are valid.
|
39
|
+
#
|
40
|
+
# attribute_key - Variable which needs to be validated.
|
41
|
+
# attribute_value - Variable which needs to be validated.
|
42
|
+
#
|
43
|
+
# Returns boolean depending on validity of attribute_key and attribute_value.
|
44
|
+
|
45
|
+
return false unless attribute_key.is_a?(String) || attribute_key.is_a?(Symbol)
|
46
|
+
|
47
|
+
return true if (boolean? attribute_value) || (attribute_value.is_a? String)
|
48
|
+
|
49
|
+
finite_number?(attribute_value)
|
50
|
+
end
|
51
|
+
|
52
|
+
def event_tags_valid?(event_tags)
|
53
|
+
# Determines if provided event tags are valid.
|
54
|
+
#
|
55
|
+
# event_tags - Event tags to be validated.
|
56
|
+
#
|
57
|
+
# Returns boolean depending on validity of event tags.
|
58
|
+
|
59
|
+
event_tags.is_a?(Hash)
|
60
|
+
end
|
61
|
+
|
62
|
+
def datafile_valid?(datafile)
|
63
|
+
# Determines if a given datafile is valid.
|
64
|
+
#
|
65
|
+
# datafile - String JSON representing the project.
|
66
|
+
#
|
67
|
+
# Returns boolean depending on validity of datafile.
|
68
|
+
|
69
|
+
begin
|
70
|
+
datafile = JSON.parse(datafile)
|
71
|
+
rescue
|
72
|
+
return false
|
73
|
+
end
|
74
|
+
|
75
|
+
JSON::Validator.validate(Helpers::Constants::JSON_SCHEMA_V2, datafile)
|
76
|
+
end
|
77
|
+
|
78
|
+
def error_handler_valid?(error_handler)
|
79
|
+
# Determines if a given error handler is valid.
|
80
|
+
#
|
81
|
+
# error_handler - error_handler to be validated.
|
82
|
+
#
|
83
|
+
# Returns boolean depending on whether error_handler has a handle_error method.
|
84
|
+
|
85
|
+
error_handler.respond_to?(:handle_error)
|
86
|
+
end
|
87
|
+
|
88
|
+
def event_dispatcher_valid?(event_dispatcher)
|
89
|
+
# Determines if a given event dispatcher is valid.
|
90
|
+
#
|
91
|
+
# event_dispatcher - event_dispatcher to be validated.
|
92
|
+
#
|
93
|
+
# Returns boolean depending on whether event_dispatcher has a dispatch_event method.
|
94
|
+
|
95
|
+
event_dispatcher.respond_to?(:dispatch_event)
|
96
|
+
end
|
97
|
+
|
98
|
+
def logger_valid?(logger)
|
99
|
+
# Determines if a given logger is valid.
|
100
|
+
#
|
101
|
+
# logger - logger to be validated.
|
102
|
+
#
|
103
|
+
# Returns boolean depending on whether logger has a log method.
|
104
|
+
|
105
|
+
logger.respond_to?(:log)
|
106
|
+
end
|
107
|
+
|
108
|
+
def string_numeric?(str)
|
109
|
+
!Float(str).nil?
|
110
|
+
rescue
|
111
|
+
false
|
112
|
+
end
|
113
|
+
|
114
|
+
def inputs_valid?(variables, logger = NoOpLogger.new, level = Logger::ERROR)
|
115
|
+
# Determines if values of variables in given array are non empty string.
|
116
|
+
#
|
117
|
+
# variables - array values to validate.
|
118
|
+
#
|
119
|
+
# logger - logger.
|
120
|
+
#
|
121
|
+
# Returns boolean True if all of the values are valid, False otherwise.
|
122
|
+
|
123
|
+
return false unless variables.respond_to?(:each) && !variables.empty?
|
124
|
+
|
125
|
+
is_valid = true
|
126
|
+
if variables.include? :user_id
|
127
|
+
# Empty str is a valid user ID.
|
128
|
+
unless variables[:user_id].is_a?(String)
|
129
|
+
is_valid = false
|
130
|
+
logger.log(level, "#{Constants::INPUT_VARIABLES['USER_ID']} is invalid")
|
131
|
+
end
|
132
|
+
variables.delete :user_id
|
133
|
+
end
|
134
|
+
|
135
|
+
if variables.include? :variable_type
|
136
|
+
# Empty variable_type is a valid user ID.
|
137
|
+
unless variables[:variable_type].is_a?(String) || !variables[:variable_type]
|
138
|
+
is_valid = false
|
139
|
+
logger.log(level, "#{Constants::INPUT_VARIABLES['VARIABLE_TYPE']} is invalid")
|
140
|
+
end
|
141
|
+
variables.delete :variable_type
|
142
|
+
end
|
143
|
+
|
144
|
+
variables.each do |key, value|
|
145
|
+
next if value.is_a?(String) && !value.empty?
|
146
|
+
|
147
|
+
is_valid = false
|
148
|
+
next unless logger_valid?(logger) && level
|
149
|
+
|
150
|
+
logger.log(level, "#{Constants::INPUT_VARIABLES[key.to_s.upcase]} is invalid")
|
151
|
+
end
|
152
|
+
is_valid
|
153
|
+
end
|
154
|
+
|
155
|
+
def boolean?(value)
|
156
|
+
# Returns true if given value type is boolean.
|
157
|
+
# false otherwise.
|
158
|
+
|
159
|
+
value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
160
|
+
end
|
161
|
+
|
162
|
+
def same_types?(value_1, value_2)
|
163
|
+
# Returns true if given values are of same types.
|
164
|
+
# false otherwise.
|
165
|
+
# Numeric values are considered as same type.
|
166
|
+
|
167
|
+
return true if value_1.is_a?(Numeric) && value_2.is_a?(Numeric)
|
168
|
+
|
169
|
+
return true if boolean?(value_1) && boolean?(value_2)
|
170
|
+
|
171
|
+
value_1.instance_of?(value_2.class)
|
172
|
+
end
|
173
|
+
|
174
|
+
def finite_number?(value)
|
175
|
+
# Returns true if the given value is a number, enforces
|
176
|
+
# absolute limit of 2^53 and restricts NaN, Infinity, -Infinity.
|
177
|
+
# false otherwise.
|
178
|
+
|
179
|
+
value.is_a?(Numeric) && value.to_f.finite? && value.abs <= Constants::FINITE_NUMBER_LIMIT
|
180
|
+
end
|
181
|
+
|
182
|
+
def odp_data_types_valid?(data)
|
183
|
+
valid_types = [String, Float, Integer, TrueClass, FalseClass, NilClass]
|
184
|
+
data&.values&.all? { |e| valid_types.member? e.class }
|
185
|
+
end
|
186
|
+
|
187
|
+
def segments_cache_valid?(segments_cache)
|
188
|
+
# Determines if a given segments_cache is valid.
|
189
|
+
#
|
190
|
+
# segments_cache - custom cache to be validated.
|
191
|
+
#
|
192
|
+
# Returns boolean depending on whether cache has required methods.
|
193
|
+
|
194
|
+
segments_cache.respond_to?(:reset) &&
|
195
|
+
segments_cache.method(:reset)&.parameters&.empty? &&
|
196
|
+
segments_cache.respond_to?(:lookup) &&
|
197
|
+
segments_cache.method(:lookup)&.parameters&.length&.positive? &&
|
198
|
+
segments_cache.respond_to?(:save) &&
|
199
|
+
segments_cache.method(:save)&.parameters&.length&.positive?
|
200
|
+
end
|
201
|
+
|
202
|
+
def segment_manager_valid?(segment_manager)
|
203
|
+
# Determines if a given segment_manager is valid.
|
204
|
+
#
|
205
|
+
# segment_manager - custom manager to be validated.
|
206
|
+
#
|
207
|
+
# Returns boolean depending on whether manager has required methods.
|
208
|
+
|
209
|
+
segment_manager.respond_to?(:odp_config) &&
|
210
|
+
segment_manager.respond_to?(:reset) &&
|
211
|
+
segment_manager.method(:reset)&.parameters&.empty? &&
|
212
|
+
segment_manager.respond_to?(:fetch_qualified_segments) &&
|
213
|
+
(segment_manager.method(:fetch_qualified_segments)&.parameters&.length || 0) >= 3
|
214
|
+
end
|
215
|
+
|
216
|
+
def event_manager_valid?(event_manager)
|
217
|
+
# Determines if a given event_manager is valid.
|
218
|
+
#
|
219
|
+
# event_manager - custom manager to be validated.
|
220
|
+
#
|
221
|
+
# Returns boolean depending on whether manager has required method and parameters.
|
222
|
+
return false unless
|
223
|
+
event_manager.respond_to?(:send_event) &&
|
224
|
+
event_manager.respond_to?(:start!) &&
|
225
|
+
(event_manager.method(:start!)&.parameters&.length || 0) >= 1 &&
|
226
|
+
event_manager.respond_to?(:update_config) &&
|
227
|
+
event_manager.respond_to?(:stop!)
|
228
|
+
|
229
|
+
required_parameters = Set[%i[keyreq type], %i[keyreq action], %i[keyreq identifiers], %i[keyreq data]]
|
230
|
+
existing_parameters = event_manager.method(:send_event).parameters.to_set
|
231
|
+
|
232
|
+
existing_parameters & required_parameters == required_parameters
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
@@ -1,67 +1,67 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
#
|
4
|
-
# Copyright 2017, 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
|
-
require 'optimizely/logger'
|
19
|
-
|
20
|
-
module Optimizely
|
21
|
-
module Helpers
|
22
|
-
module VariableType
|
23
|
-
module_function
|
24
|
-
|
25
|
-
def cast_value_to_type(value, variable_type, logger)
|
26
|
-
# Attempts to cast the given value to the specified type
|
27
|
-
#
|
28
|
-
# value - The string value to cast
|
29
|
-
# variable_type - String variable type
|
30
|
-
#
|
31
|
-
# Returns the cast value or nil if not able to cast
|
32
|
-
return_value = nil
|
33
|
-
|
34
|
-
case variable_type
|
35
|
-
when 'boolean'
|
36
|
-
return_value = value == 'true'
|
37
|
-
when 'double'
|
38
|
-
begin
|
39
|
-
return_value = Float(value)
|
40
|
-
rescue => e
|
41
|
-
logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\
|
42
|
-
"'#{variable_type}': #{e.message}.")
|
43
|
-
end
|
44
|
-
when 'integer'
|
45
|
-
begin
|
46
|
-
return_value = Integer(value)
|
47
|
-
rescue => e
|
48
|
-
logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\
|
49
|
-
"'#{variable_type}': #{e.message}.")
|
50
|
-
end
|
51
|
-
when 'json'
|
52
|
-
begin
|
53
|
-
return_value = JSON.parse(value)
|
54
|
-
rescue => e
|
55
|
-
logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\
|
56
|
-
"'#{variable_type}': #{e.message}.")
|
57
|
-
end
|
58
|
-
else
|
59
|
-
# default case is string
|
60
|
-
return_value = value
|
61
|
-
end
|
62
|
-
|
63
|
-
return_value
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright 2017, 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
|
+
require 'optimizely/logger'
|
19
|
+
|
20
|
+
module Optimizely
|
21
|
+
module Helpers
|
22
|
+
module VariableType
|
23
|
+
module_function
|
24
|
+
|
25
|
+
def cast_value_to_type(value, variable_type, logger)
|
26
|
+
# Attempts to cast the given value to the specified type
|
27
|
+
#
|
28
|
+
# value - The string value to cast
|
29
|
+
# variable_type - String variable type
|
30
|
+
#
|
31
|
+
# Returns the cast value or nil if not able to cast
|
32
|
+
return_value = nil
|
33
|
+
|
34
|
+
case variable_type
|
35
|
+
when 'boolean'
|
36
|
+
return_value = value == 'true'
|
37
|
+
when 'double'
|
38
|
+
begin
|
39
|
+
return_value = Float(value)
|
40
|
+
rescue => e
|
41
|
+
logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\
|
42
|
+
"'#{variable_type}': #{e.message}.")
|
43
|
+
end
|
44
|
+
when 'integer'
|
45
|
+
begin
|
46
|
+
return_value = Integer(value)
|
47
|
+
rescue => e
|
48
|
+
logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\
|
49
|
+
"'#{variable_type}': #{e.message}.")
|
50
|
+
end
|
51
|
+
when 'json'
|
52
|
+
begin
|
53
|
+
return_value = JSON.parse(value)
|
54
|
+
rescue => e
|
55
|
+
logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\
|
56
|
+
"'#{variable_type}': #{e.message}.")
|
57
|
+
end
|
58
|
+
else
|
59
|
+
# default case is string
|
60
|
+
return_value = value
|
61
|
+
end
|
62
|
+
|
63
|
+
return_value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|