Class: Togul::Client
- Inherits:
-
Object
- Object
- Togul::Client
- Defined in:
- lib/togul/client.rb
Instance Method Summary collapse
-
#enabled?(key, context = {}) ⇒ Boolean
Evaluate a feature flag.
-
#evaluate_bool(key, context = {}, fallback: false) ⇒ Boolean
Evaluate a boolean flag.
-
#evaluate_json(key, context = {}, fallback: nil) ⇒ Object
Evaluate a JSON flag.
-
#evaluate_number(key, context = {}, fallback: 0.0) ⇒ Float
Evaluate a number flag.
-
#evaluate_result(key, context = {}) ⇒ Togul::EvaluateResult
Evaluate a feature flag and return the full result with typed value accessors.
-
#evaluate_string(key, context = {}, fallback: '') ⇒ String
Evaluate a string flag.
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
-
#invalidate_cache ⇒ Object
Clear all cached flag values.
-
#invalidate_flag(key) ⇒ Object
Clear a specific flag from cache.
-
#on_cache_invalidated(&block) ⇒ Object
Register a listener for cache invalidation events.
-
#stream ⇒ Object
Start SSE stream for real-time cache invalidation.
Constructor Details
Instance Method Details
#enabled?(key, context = {}) ⇒ Boolean
Evaluate a feature flag.
21 22 23 24 25 26 27 28 |
# File 'lib/togul/client.rb', line 21 def enabled?(key, context = {}) evaluate_result(key, context).enabled? rescue StandardError case @config.fallback_mode when :fail_open then true else false end end |
#evaluate_bool(key, context = {}, fallback: false) ⇒ Boolean
Evaluate a boolean flag.
52 53 54 55 56 |
# File 'lib/togul/client.rb', line 52 def evaluate_bool(key, context = {}, fallback: false) evaluate_result(key, context).bool_value(fallback) rescue StandardError fallback end |
#evaluate_json(key, context = {}, fallback: nil) ⇒ Object
Evaluate a JSON flag.
88 89 90 91 92 |
# File 'lib/togul/client.rb', line 88 def evaluate_json(key, context = {}, fallback: nil) evaluate_result(key, context).json_value(fallback) rescue StandardError fallback end |
#evaluate_number(key, context = {}, fallback: 0.0) ⇒ Float
Evaluate a number flag.
76 77 78 79 80 |
# File 'lib/togul/client.rb', line 76 def evaluate_number(key, context = {}, fallback: 0.0) evaluate_result(key, context).number_value(fallback) rescue StandardError fallback end |
#evaluate_result(key, context = {}) ⇒ Togul::EvaluateResult
Evaluate a feature flag and return the full result with typed value accessors.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/togul/client.rb', line 35 def evaluate_result(key, context = {}) cache_key = build_cache_key(key, context) cached = @cache.get(cache_key) return cached unless cached.nil? result = evaluate(key, context) @cache.set(cache_key, result) result end |
#evaluate_string(key, context = {}, fallback: '') ⇒ String
Evaluate a string flag.
64 65 66 67 68 |
# File 'lib/togul/client.rb', line 64 def evaluate_string(key, context = {}, fallback: '') evaluate_result(key, context).string_value(fallback) rescue StandardError fallback end |
#invalidate_cache ⇒ Object
Clear all cached flag values.
95 96 97 |
# File 'lib/togul/client.rb', line 95 def invalidate_cache @cache.flush end |
#invalidate_flag(key) ⇒ Object
Clear a specific flag from cache.
100 101 102 |
# File 'lib/togul/client.rb', line 100 def invalidate_flag(key) @cache.invalidate_flag(key) end |
#on_cache_invalidated(&block) ⇒ Object
Register a listener for cache invalidation events.
110 111 112 |
# File 'lib/togul/client.rb', line 110 def on_cache_invalidated(&block) stream.on_cache_invalidated(&block) end |
#stream ⇒ Object
Start SSE stream for real-time cache invalidation.
105 106 107 |
# File 'lib/togul/client.rb', line 105 def stream @stream_client ||= StreamClient.new(@config, @cache) end |