Class: FeatBit::OpenFeature::ContextConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/featbit/openfeature/context_converter.rb

Defined Under Namespace

Classes: InvalidContext

Class Method Summary collapse

Class Method Details

.convert(context) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/featbit/openfeature/context_converter.rb', line 15

def self.convert(context)
  codes = ::OpenFeature::SDK::Provider::ErrorCode
  unless context.nil? || context.is_a?(::OpenFeature::SDK::EvaluationContext)
    raise InvalidContext.new(codes::INVALID_CONTEXT, "Expected an OpenFeature evaluation context")
  end

  key = context&.targeting_key
  if key.nil? || key == ""
    raise InvalidContext.new(codes::TARGETING_KEY_MISSING, "A non-empty targeting_key is required")
  end
  unless key.is_a?(String)
    raise InvalidContext.new(codes::INVALID_CONTEXT, "targeting_key must be a string")
  end

  name = context.field("name")
  unless name.nil? || name.is_a?(String)
    raise InvalidContext.new(codes::INVALID_CONTEXT, "name must be a string")
  end

  custom = context.fields.reject { |field, _| %w[targeting_key name].include?(field) }
  FeatBit::User.new(key, name: name, custom: custom)
end