@@ -430,6 +430,32 @@ def get_feature_variable_string(feature_flag_key, variable_key, user_id, attribu
430
430
variable_value
431
431
end
432
432
433
+ # Get the Json value of the specified variable in the feature flag in a Dict.
434
+ #
435
+ # @param feature_flag_key - String key of feature flag the variable belongs to
436
+ # @param variable_key - String key of variable for which we are getting the string value
437
+ # @param user_id - String user ID
438
+ # @param attributes - Hash representing visitor attributes and values which need to be recorded.
439
+ #
440
+ # @return [Dict] the Dict containing variable value.
441
+ # @return [nil] if the feature flag or variable are not found.
442
+
443
+ def get_feature_variable_json ( feature_flag_key , variable_key , user_id , attributes = nil )
444
+ unless is_valid
445
+ @logger . log ( Logger ::ERROR , InvalidProjectConfigError . new ( 'get_feature_variable_json' ) . message )
446
+ return nil
447
+ end
448
+ variable_value = get_feature_variable_for_type (
449
+ feature_flag_key ,
450
+ variable_key ,
451
+ Optimizely ::Helpers ::Constants ::VARIABLE_TYPES [ 'JSON' ] ,
452
+ user_id ,
453
+ attributes
454
+ )
455
+
456
+ variable_value
457
+ end
458
+
433
459
# Get the Boolean value of the specified variable in the feature flag.
434
460
#
435
461
# @param feature_flag_key - String key of feature flag the variable belongs to
@@ -484,6 +510,71 @@ def get_feature_variable_double(feature_flag_key, variable_key, user_id, attribu
484
510
variable_value
485
511
end
486
512
513
+ # Get values of all the variables in the feature flag and returns them in a Dict
514
+ #
515
+ # @param feature_flag_key - String key of feature flag
516
+ # @param user_id - String user ID
517
+ # @param attributes - Hash representing visitor attributes and values which need to be recorded.
518
+ #
519
+ # @return [Dict] the Dict containing all the varible values
520
+ # @return [nil] if the feature flag is not found.
521
+
522
+ def get_all_feature_variables ( feature_flag_key , user_id , attributes = nil )
523
+ unless is_valid
524
+ @logger . log ( Logger ::ERROR , InvalidProjectConfigError . new ( 'get_all_feature_variables' ) . message )
525
+ return nil
526
+ end
527
+
528
+ return nil unless Optimizely ::Helpers ::Validator . inputs_valid? (
529
+ {
530
+ feature_flag_key : feature_flag_key ,
531
+ user_id : user_id
532
+ } ,
533
+ @logger , Logger ::ERROR
534
+ )
535
+
536
+ return nil unless user_inputs_valid? ( attributes )
537
+
538
+ config = project_config
539
+
540
+ feature_flag = config . get_feature_flag_from_key ( feature_flag_key )
541
+ unless feature_flag
542
+ @logger . log ( Logger ::INFO , "No feature flag was found for key '#{ feature_flag_key } '." )
543
+ return nil
544
+ end
545
+
546
+ decision = @decision_service . get_variation_for_feature ( config , feature_flag , user_id , attributes )
547
+ variation = decision ? decision [ 'variation' ] : nil
548
+ feature_enabled = variation ? variation [ 'featureEnabled' ] : false
549
+ all_variables = { }
550
+
551
+ feature_flag [ 'variables' ] . each do |variable |
552
+ variable_value = get_feature_variable_for_variation ( feature_flag_key , feature_enabled , variation , variable , user_id )
553
+ all_variables [ variable [ 'key' ] ] = Helpers ::VariableType . cast_value_to_type ( variable_value , variable [ 'type' ] , @logger )
554
+ end
555
+
556
+ source_string = Optimizely ::DecisionService ::DECISION_SOURCES [ 'ROLLOUT' ]
557
+ if decision && decision [ 'source' ] == Optimizely ::DecisionService ::DECISION_SOURCES [ 'FEATURE_TEST' ]
558
+ source_info = {
559
+ experiment_key : decision . experiment [ 'key' ] ,
560
+ variation_key : variation [ 'key' ]
561
+ }
562
+ source_string = Optimizely ::DecisionService ::DECISION_SOURCES [ 'FEATURE_TEST' ]
563
+ end
564
+
565
+ @notification_center . send_notifications (
566
+ NotificationCenter ::NOTIFICATION_TYPES [ :DECISION ] ,
567
+ Helpers ::Constants ::DECISION_NOTIFICATION_TYPES [ 'ALL_FEATURE_VARIABLES' ] , user_id , ( attributes || { } ) ,
568
+ feature_key : feature_flag_key ,
569
+ feature_enabled : feature_enabled ,
570
+ source : source_string ,
571
+ variable_values : all_variables ,
572
+ source_info : source_info || { }
573
+ )
574
+
575
+ all_variables
576
+ end
577
+
487
578
# Get the Integer value of the specified variable in the feature flag.
488
579
#
489
580
# @param feature_flag_key - String key of feature flag the variable belongs to
@@ -649,52 +740,31 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
649
740
# Error message logged in DatafileProjectConfig- get_feature_flag_from_key
650
741
return nil if variable . nil?
651
742
652
- feature_enabled = false
653
-
654
743
# If variable_type is nil, set it equal to variable['type']
655
744
variable_type ||= variable [ 'type' ]
656
745
# Returns nil if type differs
657
746
if variable [ 'type' ] != variable_type
658
747
@logger . log ( Logger ::WARN ,
659
748
"Requested variable as type '#{ variable_type } ' but variable '#{ variable_key } ' is of type '#{ variable [ 'type' ] } '." )
660
749
return nil
661
- else
662
- source_string = Optimizely ::DecisionService ::DECISION_SOURCES [ 'ROLLOUT' ]
663
- decision = @decision_service . get_variation_for_feature ( config , feature_flag , user_id , attributes )
664
- variable_value = variable [ 'defaultValue' ]
665
- if decision
666
- variation = decision [ 'variation' ]
667
- if decision [ 'source' ] == Optimizely ::DecisionService ::DECISION_SOURCES [ 'FEATURE_TEST' ]
668
- source_info = {
669
- experiment_key : decision . experiment [ 'key' ] ,
670
- variation_key : variation [ 'key' ]
671
- }
672
- source_string = Optimizely ::DecisionService ::DECISION_SOURCES [ 'FEATURE_TEST' ]
673
- end
674
- feature_enabled = variation [ 'featureEnabled' ]
675
- if feature_enabled == true
676
- variation_variable_usages = config . variation_id_to_variable_usage_map [ variation [ 'id' ] ]
677
- variable_id = variable [ 'id' ]
678
- if variation_variable_usages &.key? ( variable_id )
679
- variable_value = variation_variable_usages [ variable_id ] [ 'value' ]
680
- @logger . log ( Logger ::INFO ,
681
- "Got variable value '#{ variable_value } ' for variable '#{ variable_key } ' of feature flag '#{ feature_flag_key } '." )
682
- else
683
- @logger . log ( Logger ::DEBUG ,
684
- "Variable '#{ variable_key } ' is not used in variation '#{ variation [ 'key' ] } '. Returning the default variable value '#{ variable_value } '." )
685
- end
686
- else
687
- @logger . log ( Logger ::DEBUG ,
688
- "Feature '#{ feature_flag_key } ' for variation '#{ variation [ 'key' ] } ' is not enabled. Returning the default variable value '#{ variable_value } '." )
689
- end
690
- else
691
- @logger . log ( Logger ::INFO ,
692
- "User '#{ user_id } ' was not bucketed into any variation for feature flag '#{ feature_flag_key } '. Returning the default variable value '#{ variable_value } '." )
693
- end
694
750
end
695
751
752
+ decision = @decision_service . get_variation_for_feature ( config , feature_flag , user_id , attributes )
753
+ variation = decision ? decision [ 'variation' ] : nil
754
+ feature_enabled = variation ? variation [ 'featureEnabled' ] : false
755
+
756
+ variable_value = get_feature_variable_for_variation ( feature_flag_key , feature_enabled , variation , variable , user_id )
696
757
variable_value = Helpers ::VariableType . cast_value_to_type ( variable_value , variable_type , @logger )
697
758
759
+ source_string = Optimizely ::DecisionService ::DECISION_SOURCES [ 'ROLLOUT' ]
760
+ if decision && decision [ 'source' ] == Optimizely ::DecisionService ::DECISION_SOURCES [ 'FEATURE_TEST' ]
761
+ source_info = {
762
+ experiment_key : decision . experiment [ 'key' ] ,
763
+ variation_key : variation [ 'key' ]
764
+ }
765
+ source_string = Optimizely ::DecisionService ::DECISION_SOURCES [ 'FEATURE_TEST' ]
766
+ end
767
+
698
768
@notification_center . send_notifications (
699
769
NotificationCenter ::NOTIFICATION_TYPES [ :DECISION ] ,
700
770
Helpers ::Constants ::DECISION_NOTIFICATION_TYPES [ 'FEATURE_VARIABLE' ] , user_id , ( attributes || { } ) ,
@@ -710,6 +780,45 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
710
780
variable_value
711
781
end
712
782
783
+ def get_feature_variable_for_variation ( feature_flag_key , feature_enabled , variation , variable , user_id )
784
+ # Helper method to get the non type-casted value for a variable attached to a
785
+ # feature flag. Returns appropriate variable value depending on whether there
786
+ # was a matching variation, feature was enabled or not or varible was part of the
787
+ # available variation or not. Also logs the appropriate message explaining how it
788
+ # evaluated the value of the variable.
789
+ #
790
+ # feature_flag_key - String key of feature flag the variable belongs to
791
+ # feature_enabled - Boolean indicating if feature is enabled or not
792
+ # variation - varition returned by decision service
793
+ # user_id - String user ID
794
+ #
795
+ # Returns string value of the variable.
796
+
797
+ config = project_config
798
+ variable_value = variable [ 'defaultValue' ]
799
+ if variation
800
+ if feature_enabled == true
801
+ variation_variable_usages = config . variation_id_to_variable_usage_map [ variation [ 'id' ] ]
802
+ variable_id = variable [ 'id' ]
803
+ if variation_variable_usages &.key? ( variable_id )
804
+ variable_value = variation_variable_usages [ variable_id ] [ 'value' ]
805
+ @logger . log ( Logger ::INFO ,
806
+ "Got variable value '#{ variable_value } ' for variable '#{ variable [ 'key' ] } ' of feature flag '#{ feature_flag_key } '." )
807
+ else
808
+ @logger . log ( Logger ::DEBUG ,
809
+ "Variable '#{ variable [ 'key' ] } ' is not used in variation '#{ variation [ 'key' ] } '. Returning the default variable value '#{ variable_value } '." )
810
+ end
811
+ else
812
+ @logger . log ( Logger ::DEBUG ,
813
+ "Feature '#{ feature_flag_key } ' for variation '#{ variation [ 'key' ] } ' is not enabled. Returning the default variable value '#{ variable_value } '." )
814
+ end
815
+ else
816
+ @logger . log ( Logger ::INFO ,
817
+ "User '#{ user_id } ' was not bucketed into any variation for feature flag '#{ feature_flag_key } '. Returning the default variable value '#{ variable_value } '." )
818
+ end
819
+ variable_value
820
+ end
821
+
713
822
def user_inputs_valid? ( attributes = nil , event_tags = nil )
714
823
# Helper method to validate user inputs.
715
824
#
0 commit comments