lib/context.rb in absmartly-sdk-1.0.8 vs lib/context.rb in absmartly-sdk-1.1.0
- old
+ new
@@ -19,10 +19,11 @@
end
def initialize(clock, config, data_future, data_provider,
event_handler, event_logger, variable_parser, audience_matcher)
@index = []
+ @context_custom_fields = {}
@achievements = []
@assignment_cache = {}
@assignments = {}
@clock = clock.is_a?(String) ? Time.iso8601(clock) : clock
@publish_delay = config.publish_delay
@@ -201,10 +202,56 @@
end
default_value
end
+ def custom_field_keys
+ check_ready?(true)
+ keys = []
+
+ @data.experiments.each do |experiment|
+ custom_field_values = experiment.custom_field_values
+ if custom_field_values != nil
+ custom_field_values.each do |custom_field|
+ keys.append(custom_field.name)
+ end
+ end
+ end
+
+ return keys.sort.uniq
+ end
+
+ def custom_field_value(experimentName, key)
+ check_ready?(true)
+
+ experiment_custom_fields = @context_custom_fields[experimentName]
+
+ if experiment_custom_fields != nil
+ field = experiment_custom_fields[key]
+ if field != nil
+ return field.value
+ end
+ end
+
+ return nil
+ end
+
+ def custom_field_type(experimentName, key)
+ check_ready?(true)
+
+ experiment_custom_fields = @context_custom_fields[experimentName]
+
+ if experiment_custom_fields != nil
+ field = experiment_custom_fields[key]
+ if field != nil
+ return field.type
+ end
+ end
+
+ return nil
+ end
+
def peek_variable_value(key, default_value)
check_ready?(true)
assignment = variable_assignment(key)
return assignment.variables[key.to_s.to_sym] if !assignment.nil? &&
@@ -449,12 +496,15 @@
def assign_data(data)
@data = data
@index = {}
@index_variables = {}
+
if data && !data.experiments.nil? && !data.experiments.empty?
data.experiments.each do |experiment|
+ @experimentCustomFieldValues = {}
+
experiment_variables = ExperimentVariables.new
experiment_variables.data = experiment
experiment_variables.variables ||= []
experiment.variants.each do |variant|
if !variant.config.nil? && !variant.config.empty?
@@ -465,11 +515,40 @@
else
experiment_variables.variables.push({})
end
end
+ if !experiment.custom_field_values.nil?
+ experiment.custom_field_values.each do |custom_field_value|
+ value = ContextCustomFieldValues.new
+ value.type = custom_field_value.type
+
+ if !custom_field_value.value.nil?
+ custom_value = custom_field_value.value
+
+ if custom_field_value.type.start_with?("json")
+ value.value = @variable_parser.parse(self, experiment.name, custom_field_value.name, custom_value)
+
+ elsif custom_field_value.type.start_with?("boolean")
+ value.value = custom_value.to_bool
+
+ elsif custom_field_value.type.start_with?("number")
+ value.value = custom_value.to_i
+
+ else
+ value.value = custom_field_value.value
+ end
+
+ @experimentCustomFieldValues[custom_field_value.name] = value
+
+ end
+
+ end
+ end
+
@index[experiment.name] = experiment_variables
+ @context_custom_fields[experiment.name] = @experimentCustomFieldValues
end
end
end
def set_data_failed(exception)
@@ -540,9 +619,13 @@
end
end
class ExperimentVariables
attr_accessor :data, :variables
+end
+
+class ContextCustomFieldValues
+ attr_accessor :type, :value
end
class IllegalStateException < StandardError
end