Class: FeatBit::OpenFeature::DetailsConverter

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

Constant Summary collapse

API =
::OpenFeature::SDK::Provider
ERROR_CODES =
{
  client_not_ready: API::ErrorCode::PROVIDER_NOT_READY,
  flag_not_found: API::ErrorCode::FLAG_NOT_FOUND,
  user_not_specified: API::ErrorCode::TARGETING_KEY_MISSING,
  wrong_type: API::ErrorCode::TYPE_MISMATCH
}.freeze
REASONS =
{
  "flag off" => API::Reason::DISABLED,
  "target match" => API::Reason::TARGETING_MATCH,
  "rule match" => API::Reason::TARGETING_MATCH,
  "fall through all rules" => API::Reason::DEFAULT
}.freeze
TYPES =
{
  boolean: [TrueClass, FalseClass], string: [String], number: [Numeric],
  integer: [Numeric], float: [Numeric], object: [Hash, Array]
}.freeze

Class Method Summary collapse

Class Method Details

.convert(detail, type, default_value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/featbit/openfeature/details_converter.rb', line 29

def self.convert(detail, type, default_value)
  unless detail.success?
    return error(default_value, ERROR_CODES.fetch(detail.error_kind, API::ErrorCode::GENERAL),
                 detail.error_message || detail.reason)
  end

  value = detail.value
  unless TYPES.fetch(type).any? { |klass| value.is_a?(klass) }
    return error(default_value, API::ErrorCode::TYPE_MISMATCH, "Flag value is not of type #{type}")
  end

  value = value.to_i if type == :integer
  value = value.to_f if type == :float
   = { "featbit.reason" => detail.reason, "featbit.in_experiment" => detail.send_to_experiment == true }
  ["featbit.flag_name"] = detail.flag_name unless detail.flag_name.nil?
  API::ResolutionDetails.new(value: value, reason: REASONS.fetch(detail.reason, API::Reason::UNKNOWN),
                             variant: detail.variation_id&.to_s, flag_metadata: )
end

.error(default_value, code, message) ⇒ Object



24
25
26
27
# File 'lib/featbit/openfeature/details_converter.rb', line 24

def self.error(default_value, code, message)
  API::ResolutionDetails.new(value: default_value, reason: API::Reason::ERROR,
                             error_code: code, error_message: message)
end