Class: FeatureHub::Sdk::LocalYamlValueInterceptor

Inherits:
ValueInterceptor show all
Defined in:
lib/feature_hub/sdk/local_yaml_interceptor.rb

Overview

Reads feature flag overrides from a local YAML file. The file path is read from FEATUREHUB_LOCAL_YAML or defaults to featurehub-features.yaml. Pass watch: true to reload the file automatically when it changes. Expected format:

flagValues:
  MY_FLAG: true
  MY_STRING: "hello"

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ LocalYamlValueInterceptor

Returns a new instance of LocalYamlValueInterceptor.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/feature_hub/sdk/local_yaml_interceptor.rb', line 17

def initialize(opts = nil)
  super()
  opts ||= {}
  @yaml_file = opts[:filename] || ENV.fetch("FEATUREHUB_LOCAL_YAML", "featurehub-features.yaml")
  @logger = opts[:logger]
  @mutex = Mutex.new
  @flag_values = load_flag_values(@yaml_file)
  @logger&.debug("[featurehubsdk] loaded #{@flag_values.size} feature override(s) from #{@yaml_file}")
  @watcher = nil

  return unless opts[:watch]

  @last_mtime = File.exist?(@yaml_file) ? File.mtime(@yaml_file) : nil
  watch_interval = opts[:watch_interval] || 5
  @watcher = Concurrent::TimerTask.new(execution_interval: watch_interval, run_now: false) do
    reload_if_changed
  end
  @watcher.execute
end

Instance Method Details

#closeObject



49
50
51
52
53
54
# File 'lib/feature_hub/sdk/local_yaml_interceptor.rb', line 49

def close
  return if @watcher.nil?

  @watcher.shutdown
  @watcher = nil
end

#intercepted_value(feature_key, _repository, feature_state) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/feature_hub/sdk/local_yaml_interceptor.rb', line 37

def intercepted_value(feature_key, _repository, feature_state)
  key = feature_key.to_s
  flag_values = @mutex.synchronize { @flag_values }
  return [false, nil] unless flag_values.key?(key)

  value = flag_values[key]

  return [false, nil] if feature_state && yaml_value_type(value) != feature_state["type"]

  [true, cast_value(value)]
end