Class: Togul::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/togul/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.

Parameters:



10
11
12
13
14
# File 'lib/togul/client.rb', line 10

def initialize(config)
  @config = config
  @cache = Cache.new(ttl: config.cache_ttl)
  @stream_client = nil
end

Instance Method Details

#enabled?(key, context = {}) ⇒ Boolean

Evaluate a feature flag.

Parameters:

  • key (String)

    Flag key

  • context (Hash<String, String>) (defaults to: {})

    User/request context

Returns:

  • (Boolean)

    Whether the flag is enabled



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.

Parameters:

  • key (String)

    Flag key

  • context (Hash<String, String>) (defaults to: {})

    User/request context

  • fallback (Boolean) (defaults to: false)

    Value to return on error or type mismatch

Returns:

  • (Boolean)


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.

Parameters:

  • key (String)

    Flag key

  • context (Hash<String, String>) (defaults to: {})

    User/request context

  • fallback (Object) (defaults to: nil)

    Value to return on error or type mismatch

Returns:

  • (Object)


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.

Parameters:

  • key (String)

    Flag key

  • context (Hash<String, String>) (defaults to: {})

    User/request context

  • fallback (Float) (defaults to: 0.0)

    Value to return on error or type mismatch

Returns:

  • (Float)


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.

Parameters:

  • key (String)

    Flag key

  • context (Hash<String, String>) (defaults to: {})

    User/request context

Returns:



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.

Parameters:

  • key (String)

    Flag key

  • context (Hash<String, String>) (defaults to: {})

    User/request context

  • fallback (String) (defaults to: '')

    Value to return on error or type mismatch

Returns:

  • (String)


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_cacheObject

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

#streamObject

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