Class: Toggly::Client
- Inherits:
-
Object
- Object
- Toggly::Client
- Defined in:
- lib/toggly/client.rb
Overview
Main client for interacting with Toggly feature flags.
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
Client configuration.
-
#definitions ⇒ Hash<String, FeatureDefinition>
readonly
Current definitions.
-
#ready ⇒ Boolean
readonly
Whether the client is ready.
Instance Method Summary collapse
-
#close ⇒ Object
Close the client and stop background refresh.
-
#closed? ⇒ Boolean
Check if client is closed.
-
#disabled?(feature_key, context: nil, default: nil) ⇒ Boolean
Check if a feature is disabled.
-
#enabled?(feature_key, context: nil, default: nil) ⇒ Boolean
Check if a feature is enabled.
-
#evaluate(feature_key, context: nil) ⇒ EvaluationResult
Get detailed evaluation result.
-
#feature(feature_key) ⇒ FeatureDefinition?
Get a feature definition.
-
#feature_keys ⇒ Array<String>
Get all feature keys.
-
#features ⇒ Array<FeatureDefinition>
Get all features.
-
#initialize(config_or_options = {}) ⇒ Client
constructor
Create a new client.
-
#refresh(force: false) ⇒ Boolean
Manually refresh definitions.
-
#wait_for_ready(timeout: 5) ⇒ Boolean
Wait for client to be ready.
Constructor Details
#initialize(config_or_options = {}) ⇒ Client
Create a new client
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/toggly/client.rb', line 36 def initialize( = {}) @config = .is_a?(Config) ? : Config.new(**) @config.validate! @definitions = {} @mutex = Mutex.new @ready = false @closed = false @engine = EvaluationEngine.new(logger: @config.logger) @provider = DefinitionsProvider.new( config: @config, logger: @config.logger, on_definitions_updated: -> { refresh } ) @refresh_thread = nil initialize_definitions Toggly.register_entity_contexts_at_startup(@config) unless @config.disable_entity_context_registration start_background_refresh unless @config.disable_background_refresh end |
Instance Attribute Details
#config ⇒ Config (readonly)
Returns Client configuration.
25 26 27 |
# File 'lib/toggly/client.rb', line 25 def config @config end |
#definitions ⇒ Hash<String, FeatureDefinition> (readonly)
Returns Current definitions.
28 29 30 |
# File 'lib/toggly/client.rb', line 28 def definitions @definitions end |
#ready ⇒ Boolean (readonly)
Returns Whether the client is ready.
31 32 33 |
# File 'lib/toggly/client.rb', line 31 def ready @ready end |
Instance Method Details
#close ⇒ Object
Close the client and stop background refresh
153 154 155 156 157 158 |
# File 'lib/toggly/client.rb', line 153 def close @closed = true @provider.stop_websocket @refresh_thread&.kill @refresh_thread = nil end |
#closed? ⇒ Boolean
Check if client is closed
163 164 165 |
# File 'lib/toggly/client.rb', line 163 def closed? @closed end |
#disabled?(feature_key, context: nil, default: nil) ⇒ Boolean
Check if a feature is disabled
88 89 90 |
# File 'lib/toggly/client.rb', line 88 def disabled?(feature_key, context: nil, default: nil) !enabled?(feature_key, context: context, default: default.nil? ? nil : !default) end |
#enabled?(feature_key, context: nil, default: nil) ⇒ Boolean
Check if a feature is enabled
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/toggly/client.rb', line 65 def enabled?(feature_key, context: nil, default: nil) key = feature_key.to_s definition = @mutex.synchronize { @definitions[key] } # Check defaults if not found if definition.nil? return default unless default.nil? return @config.defaults[key] if @config.defaults.key?(key) return @config.enable_undefined_in_dev if development? return false end @engine.evaluate(definition, context) end |
#evaluate(feature_key, context: nil) ⇒ EvaluationResult
Get detailed evaluation result
97 98 99 100 101 102 |
# File 'lib/toggly/client.rb', line 97 def evaluate(feature_key, context: nil) key = feature_key.to_s definition = @mutex.synchronize { @definitions[key] } @engine.evaluate_with_details(definition, context) end |
#feature(feature_key) ⇒ FeatureDefinition?
Get a feature definition
108 109 110 |
# File 'lib/toggly/client.rb', line 108 def feature(feature_key) @mutex.synchronize { @definitions[feature_key.to_s] } end |
#feature_keys ⇒ Array<String>
Get all feature keys
115 116 117 |
# File 'lib/toggly/client.rb', line 115 def feature_keys @mutex.synchronize { @definitions.keys } end |
#features ⇒ Array<FeatureDefinition>
Get all features
122 123 124 |
# File 'lib/toggly/client.rb', line 122 def features @mutex.synchronize { @definitions.values } end |
#refresh(force: false) ⇒ Boolean
Manually refresh definitions
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/toggly/client.rb', line 130 def refresh(force: false) return false if @config.offline_mode? new_definitions = @provider.fetch(force: force) if new_definitions @mutex.synchronize do @definitions = new_definitions @ready = true end save_snapshot log_info("Definitions refreshed (#{new_definitions.size} features)") true else false end rescue StandardError => e log_error("Failed to refresh definitions: #{e.}") false end |
#wait_for_ready(timeout: 5) ⇒ Boolean
Wait for client to be ready
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/toggly/client.rb', line 171 def wait_for_ready(timeout: 5) start_time = Time.now until @ready return false if (Time.now - start_time) > timeout sleep(0.01) end true end |