Module: Unleash::OpenFeature::Provider::Context

Defined in:
lib/unleash/open_feature/provider/context.rb

Constant Summary collapse

BASE_CONTEXT_KEYS =
{
  'currentTime' => 'currentTime',
  'userId' => 'userId',
  'sessionId' => 'sessionId',
  'remoteAddress' => 'remoteAddress',
  'environment' => 'environment',
  'appName' => 'appName'
}.freeze

Class Method Summary collapse

Class Method Details

.scalar?(value) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/unleash/open_feature/provider/context.rb', line 45

def scalar?(value)
  value.nil? || value.is_a?(String) || value.is_a?(Numeric) ||
    value == true || value == false || value.is_a?(Time)
end

.to_unleash_context(evaluation_context, logger: Logger.new($stderr)) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/unleash/open_feature/provider/context.rb', line 21

def to_unleash_context(evaluation_context, logger: Logger.new($stderr))
  return Unleash::Context.new if evaluation_context.nil?

  context = {}
  properties = {}

  evaluation_context.fields.each do |key, value|
    next if key == 'targeting_key'

    if BASE_CONTEXT_KEYS.key?(key)
      context[BASE_CONTEXT_KEYS.fetch(key)] = value
    elsif scalar?(value)
      properties[key] = value
    else
      logger.debug("Discarding nested Unleash context property: #{key}")
    end
  end

  context['userId'] = evaluation_context.targeting_key unless evaluation_context.targeting_key.nil?
  context['properties'] = properties unless properties.empty?

  Unleash::Context.new(context)
end