Class: Evaluator

Inherits:
Evaluation show all
Defined in:
lib/ff/ruby/server/sdk/api/evaluator.rb

Instance Method Summary collapse

Constructor Details

#initialize(repository, logger = nil) ⇒ Evaluator

Returns a new instance of Evaluator.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ff/ruby/server/sdk/api/evaluator.rb', line 10

def initialize(repository, logger = nil)

  unless repository.kind_of?(Repository)
    raise "The 'repository' parameter must be of '" + Repository.to_s + "' data type"
  end

  if logger != nil
    @logger = logger
  else
    @logger = Logger.new(STDOUT)
  end

  @repository = repository
end

Instance Method Details

#bool_variation(identifier, target, default_value, callback) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ff/ruby/server/sdk/api/evaluator.rb', line 25

def bool_variation(identifier, target, default_value, callback)

  variation = evaluate(identifier, target, "boolean", callback)

  if variation != nil
    return variation.value == "true"
  end

  SdkCodes::warn_default_variation_served @logger, identifier, target, default_value.to_s
  default_value
end

#evaluate(identifier, target, expected, callback) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ff/ruby/server/sdk/api/evaluator.rb', line 84

def evaluate(identifier, target, expected, callback)

  if callback != nil
    unless callback.kind_of?(FlagEvaluateCallback)
      raise "The 'callback' parameter must be of '" + FlagEvaluateCallback.to_s + "' data type"
    end
  end

  flag = @repository.get_flag(identifier)

  # Check if flag exists
  if flag.nil?
    # Log a warning if the flag is not found
    @logger.warn "Flag not found for identifier '#{identifier}'. Serving default variation."
    return nil
  end

  # Check if the flag's kind matches the expected type
  unless flag.kind == expected
    @logger.warn "Flag kind mismatch: expected '#{expected}', but got '#{flag.kind}' for identifier '#{identifier}'. Serving default variation."
    return nil
  end

  # Proceed with prerequisite check if flag type is as expected
  if flag != nil && flag.kind == expected
    unless flag.prerequisites.empty?
      pre_req = check_pre_requisite(flag, target)
      unless pre_req
        return find_variation(flag.variations, flag.off_variation)
      end
    end

    variation = evaluate_flag(flag, target)

    if variation != nil
      if callback != nil
        callback.process_evaluation(flag, target, variation)
      end
      return variation
    end
  end

  # Returning nil will indicate to callers to serve the default variation
  nil
end

#integer_variation(identifier, target, default_value, callback) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ff/ruby/server/sdk/api/evaluator.rb', line 49

def integer_variation(identifier, target, default_value, callback)

  variation = evaluate(identifier, target, "int", callback)

  if variation != nil
    return variation.value.to_i
  end

  SdkCodes::warn_default_variation_served @logger, identifier, target, default_value.to_s
  default_value
end

#json_variation(identifier, target, default_value, callback) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ff/ruby/server/sdk/api/evaluator.rb', line 72

def json_variation(identifier, target, default_value, callback)

  variation = evaluate(identifier, target, "json", callback)

  if variation != nil
    return JSON.parse(variation.value)
  end

  SdkCodes::warn_default_variation_served @logger, identifier, target, default_value.to_s
  default_value
end

#number_variation(identifier, target, default_value, callback) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/ff/ruby/server/sdk/api/evaluator.rb', line 61

def number_variation(identifier, target, default_value, callback)
  variation = evaluate(identifier, target, "int", callback)

  if variation != nil
    return variation.value.to_f
  end

  SdkCodes::warn_default_variation_served @logger, identifier, target, default_value.to_s
  default_value
end

#string_variation(identifier, target, default_value, callback) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ff/ruby/server/sdk/api/evaluator.rb', line 37

def string_variation(identifier, target, default_value, callback)

  variation = evaluate(identifier, target, "string", callback)

  if variation != nil
    return variation.value
  end

  SdkCodes::warn_default_variation_served @logger, identifier, target, default_value.to_s
  default_value
end