Class: IbmAppconfigurationRubySdk::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm_appconfiguration_ruby_sdk/models/feature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feature) ⇒ Feature

Returns a new instance of Feature.

Parameters:

  • feature (Hash)

    Feature configuration hash



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 28

def initialize(feature)
  @name = feature[:name]
  @feature_id = feature[:feature_id]
  @type = feature[:type]
  @format = feature[:format]
  @disabled_value = feature[:disabled_value]
  @enabled_value = feature[:enabled_value]
  @enabled = feature[:enabled]
  @rollout_type = feature.key?(:rollout_type) ? feature[:rollout_type] : Constants::MANUAL

  if feature[:rollout_configuration]
    @rollout_configuration = feature[:rollout_configuration]
  else
    @rollout_percentage = feature.key?(:rollout_percentage) ? feature[:rollout_percentage] : 100
  end

  @segment_rules = feature[:segment_rules]
  @experiment = feature[:experiment]
end

Instance Attribute Details

#disabled_valueObject (readonly)

Returns the value of attribute disabled_value.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def disabled_value
  @disabled_value
end

#enabledObject (readonly)

Returns the value of attribute enabled.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def enabled
  @enabled
end

#enabled_valueObject (readonly)

Returns the value of attribute enabled_value.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def enabled_value
  @enabled_value
end

#experimentObject (readonly)

Returns the value of attribute experiment.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def experiment
  @experiment
end

#feature_idObject (readonly)

Returns the value of attribute feature_id.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def feature_id
  @feature_id
end

#formatObject (readonly)

Returns the value of attribute format.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def name
  @name
end

#rollout_configurationObject (readonly)

Returns the value of attribute rollout_configuration.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def rollout_configuration
  @rollout_configuration
end

#rollout_percentageObject (readonly)

Returns the value of attribute rollout_percentage.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def rollout_percentage
  @rollout_percentage
end

#rollout_typeObject (readonly)

Returns the value of attribute rollout_type.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def rollout_type
  @rollout_type
end

#segment_rulesObject (readonly)

Returns the value of attribute segment_rules.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def segment_rules
  @segment_rules
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 23

def type
  @type
end

Instance Method Details

#data_formatString?

Returns Feature data format (TEXT/JSON/YAML).

Returns:

  • (String, nil)

    Feature data format (TEXT/JSON/YAML)



49
50
51
52
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 49

def data_format
  @format = "TEXT" if @format.nil? && @type == "STRING"
  @format
end

#enabled?Boolean

Returns Feature enabled state.

Returns:

  • (Boolean)

    Feature enabled state



55
56
57
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 55

def enabled?
  @enabled
end

#get_current_value(entity_id, entity_attributes = {}) ⇒ EvaluationResult?

Evaluates and returns the feature flag value for the given entity.

Examples:

feature = app_config_client.get_feature('discount')
if feature
  result = feature.get_current_value(entity_id, entity_attributes)
  result.value       # => true / false / String / Numeric
  result.enabled     # => true / false
  result.details.value_type  # => "ENABLED_VALUE" / "DISABLED_VALUE" / "SEGMENT_VALUE"
end

# Note: While an experiment is running, result.enabled == true indicates
# that the entity was part of the experiment audience.

Parameters:

  • entity_id (String)

    Id of the Entity. This will be a string identifier related to the Entity against which the feature is evaluated. For example, an entity might be an instance of an app that runs on a mobile device, a microservice that runs on the cloud, or a component of infrastructure that runs that microservice. For any entity to interact with App Configuration, it must provide a unique entity ID.

  • entity_attributes (Hash) (defaults to: {})

    A hash consisting of the attribute name and their values that defines the specified entity. This is an optional parameter if the feature flag is not configured with any targeting definition. If the targeting is configured, then entity_attributes should be provided for the rule evaluation. An attribute is a parameter that is used to define a segment. The SDK uses the attribute values to determine if the specified entity satisfies the targeting rules, and returns the appropriate feature flag value.

Returns:

  • (EvaluationResult, nil)

    Returns an EvaluationResult with:

    • result.value — the resolved value (type matches the feature flag's type)
    • result.enabled — whether the feature flag is enabled for this entity
    • result.details — an EvaluationDetails explaining how the value was reached Returns nil if entity_id is invalid.


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ibm_appconfiguration_ruby_sdk/models/feature.rb', line 93

def get_current_value(entity_id, entity_attributes = {})
  if entity_id.nil? || entity_id.to_s.strip.empty?
    logger = Logger.instance
    logger.error("Feature flag evaluation: #{Constants::INVALID_ENTITY_ID} get_current_value")
    return nil
  end

  require_relative "../configuration_handler"
  configuration_handler_instance = ConfigurationHandler.current_instance
  configuration_handler_instance.feature_evaluation(self, entity_id, entity_attributes)
end