zenmanage 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +27 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/CONTRIBUTING.md +22 -0
- data/LICENSE +21 -0
- data/README.md +382 -0
- data/SECURITY.md +9 -0
- data/examples/caching.rb +58 -0
- data/examples/context_based_flags.rb +45 -0
- data/examples/defaults.rb +62 -0
- data/examples/percentage_rollouts.rb +37 -0
- data/examples/rails_integration.rb +74 -0
- data/examples/simple_flags.rb +41 -0
- data/lib/generators/zenmanage/install/install_generator.rb +18 -0
- data/lib/generators/zenmanage/install/templates/initializer.rb +23 -0
- data/lib/zenmanage/api_client.rb +264 -0
- data/lib/zenmanage/attribute.rb +55 -0
- data/lib/zenmanage/cache/base.rb +46 -0
- data/lib/zenmanage/cache/filesystem_cache.rb +88 -0
- data/lib/zenmanage/cache/memory_cache.rb +63 -0
- data/lib/zenmanage/cache/null_cache.rb +22 -0
- data/lib/zenmanage/config_builder.rb +215 -0
- data/lib/zenmanage/configuration.rb +118 -0
- data/lib/zenmanage/context.rb +125 -0
- data/lib/zenmanage/defaults_collection.rb +68 -0
- data/lib/zenmanage/errors.rb +21 -0
- data/lib/zenmanage/flag.rb +200 -0
- data/lib/zenmanage/flag_manager.rb +197 -0
- data/lib/zenmanage/hash_access.rb +72 -0
- data/lib/zenmanage/rails/controller_helpers.rb +85 -0
- data/lib/zenmanage/rails/middleware.rb +34 -0
- data/lib/zenmanage/rails/railtie.rb +60 -0
- data/lib/zenmanage/rollout.rb +45 -0
- data/lib/zenmanage/rollout_bucket.rb +40 -0
- data/lib/zenmanage/rule.rb +142 -0
- data/lib/zenmanage/rule_engine.rb +156 -0
- data/lib/zenmanage/rule_store.rb +164 -0
- data/lib/zenmanage/target.rb +37 -0
- data/lib/zenmanage/version.rb +6 -0
- data/lib/zenmanage/webhook.rb +89 -0
- data/lib/zenmanage.rb +183 -0
- metadata +216 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "json"
|
|
5
|
+
require "digest"
|
|
6
|
+
|
|
7
|
+
module Zenmanage
|
|
8
|
+
module Cache
|
|
9
|
+
# Filesystem-backed cache. Stores entries as JSON files on disk so they
|
|
10
|
+
# survive process restarts and can be shared between workers.
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# cache = Zenmanage::Cache::FilesystemCache.new("/tmp/zenmanage")
|
|
14
|
+
class FilesystemCache < Base
|
|
15
|
+
# @param directory [String] path to the cache directory
|
|
16
|
+
# @raise [CacheError] if the directory cannot be created
|
|
17
|
+
def initialize(directory)
|
|
18
|
+
super()
|
|
19
|
+
@directory = directory
|
|
20
|
+
FileUtils.mkdir_p(@directory)
|
|
21
|
+
rescue SystemCallError => e
|
|
22
|
+
raise CacheError, "Failed to create cache directory '#{directory}': #{e.message}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @param key [String]
|
|
26
|
+
# @return [String, nil]
|
|
27
|
+
def get(key)
|
|
28
|
+
path = cache_path(key)
|
|
29
|
+
return nil unless File.exist?(path)
|
|
30
|
+
|
|
31
|
+
# Read as UTF-8 whatever the process locale says; rules can hold any text.
|
|
32
|
+
raw = File.read(path, encoding: "UTF-8")
|
|
33
|
+
data = JSON.parse(raw)
|
|
34
|
+
|
|
35
|
+
if data["expires_at"] && data["expires_at"] < Time.now.to_f
|
|
36
|
+
begin
|
|
37
|
+
File.delete(path)
|
|
38
|
+
rescue StandardError
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
return nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
data["value"]
|
|
45
|
+
rescue JSON::ParserError, SystemCallError
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @param key [String]
|
|
50
|
+
# @param value [String]
|
|
51
|
+
# @param ttl [Integer, nil] seconds until expiry
|
|
52
|
+
def set(key, value, ttl = nil)
|
|
53
|
+
path = cache_path(key)
|
|
54
|
+
expires = ttl ? Time.now.to_f + ttl : nil
|
|
55
|
+
payload = JSON.generate("value" => value, "expires_at" => expires)
|
|
56
|
+
File.write(path, payload, encoding: "UTF-8")
|
|
57
|
+
rescue SystemCallError => e
|
|
58
|
+
raise CacheError, "Failed to write cache entry '#{key}': #{e.message}"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# @param key [String]
|
|
62
|
+
def delete(key)
|
|
63
|
+
File.delete(cache_path(key))
|
|
64
|
+
rescue Errno::ENOENT
|
|
65
|
+
nil
|
|
66
|
+
rescue SystemCallError => e
|
|
67
|
+
raise CacheError, "Failed to delete cache entry '#{key}': #{e.message}"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Remove all cached entries in the cache directory.
|
|
71
|
+
def clear
|
|
72
|
+
Dir.glob(File.join(@directory, "*.zen_cache")).each do |f|
|
|
73
|
+
File.delete(f)
|
|
74
|
+
rescue StandardError
|
|
75
|
+
nil
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
# Map a cache key to a safe filename.
|
|
82
|
+
def cache_path(key)
|
|
83
|
+
safe = Digest::SHA256.hexdigest(key)[0, 32]
|
|
84
|
+
File.join(@directory, "#{safe}.zen_cache")
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zenmanage
|
|
4
|
+
module Cache
|
|
5
|
+
# In-process memory cache. Data is scoped to the current Ruby process and
|
|
6
|
+
# does not persist across requests or restarts.
|
|
7
|
+
#
|
|
8
|
+
# This is the default cache backend.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# cache = Zenmanage::Cache::MemoryCache.new
|
|
12
|
+
# cache.set("key", "value", 60)
|
|
13
|
+
# cache.get("key") # => "value"
|
|
14
|
+
class MemoryCache < Base
|
|
15
|
+
# @api private
|
|
16
|
+
Entry = Struct.new(:value, :expires_at)
|
|
17
|
+
|
|
18
|
+
def initialize
|
|
19
|
+
super
|
|
20
|
+
@store = {}
|
|
21
|
+
@mutex = Mutex.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @param key [String]
|
|
25
|
+
# @return [String, nil]
|
|
26
|
+
def get(key)
|
|
27
|
+
@mutex.synchronize do
|
|
28
|
+
entry = @store[key]
|
|
29
|
+
return nil unless entry
|
|
30
|
+
return nil if entry.expires_at && entry.expires_at < Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
31
|
+
|
|
32
|
+
entry.value
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @param key [String]
|
|
37
|
+
# @param value [String]
|
|
38
|
+
# @param ttl [Integer, nil] seconds until expiry
|
|
39
|
+
def set(key, value, ttl = nil)
|
|
40
|
+
@mutex.synchronize do
|
|
41
|
+
expires_at = ttl ? Process.clock_gettime(Process::CLOCK_MONOTONIC) + ttl : nil
|
|
42
|
+
@store[key] = Entry.new(value, expires_at)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @param key [String]
|
|
47
|
+
# @return [Boolean]
|
|
48
|
+
def exist?(key)
|
|
49
|
+
!get(key).nil?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# @param key [String]
|
|
53
|
+
def delete(key)
|
|
54
|
+
@mutex.synchronize { @store.delete(key) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Remove all cached entries.
|
|
58
|
+
def clear
|
|
59
|
+
@mutex.synchronize { @store.clear }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zenmanage
|
|
4
|
+
module Cache
|
|
5
|
+
# Null (no-op) cache. Every `get` returns `nil`; `set`/`delete`/`clear` are no-ops.
|
|
6
|
+
#
|
|
7
|
+
# Useful for testing or when you want to force fresh API calls on every request.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# config = Zenmanage::ConfigBuilder.create
|
|
11
|
+
# .with_environment_token("srv_token")
|
|
12
|
+
# .with_cache_backend("null")
|
|
13
|
+
# .build
|
|
14
|
+
class NullCache < Base
|
|
15
|
+
def get(_key) = nil
|
|
16
|
+
def set(*) = nil
|
|
17
|
+
def delete(_key) = nil
|
|
18
|
+
def clear = nil
|
|
19
|
+
def exist?(_key) = false
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zenmanage
|
|
4
|
+
# Fluent builder for creating {Configuration} objects.
|
|
5
|
+
#
|
|
6
|
+
# @example Basic usage
|
|
7
|
+
# config = Zenmanage::ConfigBuilder.create
|
|
8
|
+
# .with_environment_token("srv_my_token")
|
|
9
|
+
# .build
|
|
10
|
+
#
|
|
11
|
+
# @example From environment variables
|
|
12
|
+
# config = Zenmanage::ConfigBuilder.from_environment.build
|
|
13
|
+
#
|
|
14
|
+
# @example Full configuration
|
|
15
|
+
# config = Zenmanage::ConfigBuilder.create
|
|
16
|
+
# .with_environment_token("srv_my_token")
|
|
17
|
+
# .with_cache_ttl(7200)
|
|
18
|
+
# .with_cache_backend("filesystem")
|
|
19
|
+
# .with_cache_directory("/tmp/zenmanage")
|
|
20
|
+
# .with_usage_reporting(true)
|
|
21
|
+
# .with_api_endpoint("https://api.zenmanage.com")
|
|
22
|
+
# .build
|
|
23
|
+
class ConfigBuilder
|
|
24
|
+
def initialize
|
|
25
|
+
@environment_token = nil
|
|
26
|
+
@cache_ttl = Configuration::DEFAULT_CACHE_TTL
|
|
27
|
+
@cache_backend = Configuration::DEFAULT_CACHE_BACKEND
|
|
28
|
+
@cache_directory = nil
|
|
29
|
+
@enable_usage_reporting = true
|
|
30
|
+
@api_endpoint = Configuration::DEFAULT_API_ENDPOINT
|
|
31
|
+
@sdk_version = nil
|
|
32
|
+
@client_agent = nil
|
|
33
|
+
@logger = nil
|
|
34
|
+
@cache = nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Create a new ConfigBuilder instance.
|
|
38
|
+
#
|
|
39
|
+
# @return [ConfigBuilder]
|
|
40
|
+
def self.create
|
|
41
|
+
new
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Create a ConfigBuilder pre-populated from environment variables.
|
|
45
|
+
#
|
|
46
|
+
# Reads the following environment variables:
|
|
47
|
+
# - `ZENMANAGE_ENVIRONMENT_TOKEN`
|
|
48
|
+
# - `ZENMANAGE_CACHE_TTL`
|
|
49
|
+
# - `ZENMANAGE_CACHE_BACKEND`
|
|
50
|
+
# - `ZENMANAGE_CACHE_DIR`
|
|
51
|
+
# - `ZENMANAGE_ENABLE_USAGE_REPORTING`
|
|
52
|
+
# - `ZENMANAGE_API_ENDPOINT`
|
|
53
|
+
#
|
|
54
|
+
# @return [ConfigBuilder]
|
|
55
|
+
def self.from_environment
|
|
56
|
+
builder = new
|
|
57
|
+
|
|
58
|
+
with_env("ZENMANAGE_ENVIRONMENT_TOKEN") { |v| builder.with_environment_token(v) }
|
|
59
|
+
with_env("ZENMANAGE_CACHE_TTL") { |v| builder.with_cache_ttl(v.to_i) if v.match?(/\A\d+\z/) }
|
|
60
|
+
with_env("ZENMANAGE_CACHE_BACKEND") { |v| builder.with_cache_backend(v) }
|
|
61
|
+
with_env("ZENMANAGE_CACHE_DIR") { |v| builder.with_cache_directory(v) }
|
|
62
|
+
with_env("ZENMANAGE_API_ENDPOINT") { |v| builder.with_api_endpoint(v) }
|
|
63
|
+
with_env("ZENMANAGE_ENABLE_USAGE_REPORTING") do |v|
|
|
64
|
+
enabled = env_boolean(v)
|
|
65
|
+
builder.with_usage_reporting(enabled) unless enabled.nil?
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
builder
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Yield the named environment variable, but only when it is set and non-empty.
|
|
72
|
+
def self.with_env(name)
|
|
73
|
+
value = ENV.fetch(name, nil)
|
|
74
|
+
yield value if value && !value.empty?
|
|
75
|
+
end
|
|
76
|
+
private_class_method :with_env
|
|
77
|
+
|
|
78
|
+
# @return [Boolean, nil] nil when the value isn't a recognised boolean
|
|
79
|
+
def self.env_boolean(value)
|
|
80
|
+
case value.downcase
|
|
81
|
+
when "true", "1" then true
|
|
82
|
+
when "false", "0" then false
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
private_class_method :env_boolean
|
|
86
|
+
|
|
87
|
+
# Set the environment token (required).
|
|
88
|
+
#
|
|
89
|
+
# @param token [String] server key starting with `srv_`
|
|
90
|
+
# @return [self]
|
|
91
|
+
def with_environment_token(token)
|
|
92
|
+
@environment_token = token
|
|
93
|
+
self
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Set the cache time-to-live in seconds.
|
|
97
|
+
#
|
|
98
|
+
# @param ttl [Integer]
|
|
99
|
+
# @return [self]
|
|
100
|
+
def with_cache_ttl(ttl)
|
|
101
|
+
@cache_ttl = ttl
|
|
102
|
+
self
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Set the cache backend (`"memory"`, `"filesystem"`, or `"null"`).
|
|
106
|
+
#
|
|
107
|
+
# @param backend [String]
|
|
108
|
+
# @return [self]
|
|
109
|
+
def with_cache_backend(backend)
|
|
110
|
+
@cache_backend = backend
|
|
111
|
+
self
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Set the directory for filesystem caching.
|
|
115
|
+
#
|
|
116
|
+
# @param directory [String]
|
|
117
|
+
# @return [self]
|
|
118
|
+
def with_cache_directory(directory)
|
|
119
|
+
@cache_directory = directory
|
|
120
|
+
self
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Enable or disable usage reporting.
|
|
124
|
+
#
|
|
125
|
+
# @param enabled [Boolean]
|
|
126
|
+
# @return [self]
|
|
127
|
+
def with_usage_reporting(enabled)
|
|
128
|
+
@enable_usage_reporting = enabled
|
|
129
|
+
self
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# @deprecated Use {#with_usage_reporting} instead.
|
|
133
|
+
def enable_usage_reporting
|
|
134
|
+
with_usage_reporting(true)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# @deprecated Use {#with_usage_reporting} instead.
|
|
138
|
+
def disable_usage_reporting
|
|
139
|
+
with_usage_reporting(false)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Set a custom API endpoint.
|
|
143
|
+
#
|
|
144
|
+
# @param endpoint [String]
|
|
145
|
+
# @return [self]
|
|
146
|
+
def with_api_endpoint(endpoint)
|
|
147
|
+
@api_endpoint = endpoint
|
|
148
|
+
self
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Override the SDK version reported in request headers.
|
|
152
|
+
#
|
|
153
|
+
# @param version [String]
|
|
154
|
+
# @return [self]
|
|
155
|
+
def with_sdk_version(version)
|
|
156
|
+
@sdk_version = version
|
|
157
|
+
self
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Override the client agent string reported in request headers.
|
|
161
|
+
#
|
|
162
|
+
# @param agent [String]
|
|
163
|
+
# @return [self]
|
|
164
|
+
def with_client_agent(agent)
|
|
165
|
+
@client_agent = agent
|
|
166
|
+
self
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Set a custom logger.
|
|
170
|
+
#
|
|
171
|
+
# @param logger [#debug, #info, #warn, #error]
|
|
172
|
+
# @return [self]
|
|
173
|
+
def with_logger(logger)
|
|
174
|
+
@logger = logger
|
|
175
|
+
self
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Use your own cache backend instead of one of the built-in ones.
|
|
179
|
+
#
|
|
180
|
+
# @param cache [Cache::Base] anything with `get(key)` and `set(key, value, ttl)`
|
|
181
|
+
# @return [self]
|
|
182
|
+
def with_cache(cache)
|
|
183
|
+
@cache = cache
|
|
184
|
+
self
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# The settings gathered so far, keyed as {Configuration#initialize} takes them.
|
|
188
|
+
#
|
|
189
|
+
# @return [Hash{Symbol => Object}]
|
|
190
|
+
def to_h
|
|
191
|
+
{
|
|
192
|
+
environment_token: @environment_token,
|
|
193
|
+
cache_ttl: @cache_ttl,
|
|
194
|
+
cache_backend: @cache_backend,
|
|
195
|
+
cache_directory: @cache_directory,
|
|
196
|
+
enable_usage_reporting: @enable_usage_reporting,
|
|
197
|
+
api_endpoint: @api_endpoint,
|
|
198
|
+
sdk_version: @sdk_version,
|
|
199
|
+
client_agent: @client_agent,
|
|
200
|
+
logger: @logger,
|
|
201
|
+
cache: @cache,
|
|
202
|
+
}
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Build and return the {Configuration} object.
|
|
206
|
+
#
|
|
207
|
+
# @return [Configuration]
|
|
208
|
+
# @raise [ConfigurationError] if validation fails
|
|
209
|
+
def build
|
|
210
|
+
options = to_h
|
|
211
|
+
options[:environment_token] ||= ""
|
|
212
|
+
Configuration.new(**options)
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zenmanage
|
|
4
|
+
# Immutable configuration object for the Zenmanage SDK.
|
|
5
|
+
#
|
|
6
|
+
# Use {ConfigBuilder} to construct instances via a fluent interface.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# config = Zenmanage::ConfigBuilder.create
|
|
10
|
+
# .with_environment_token("srv_my_token")
|
|
11
|
+
# .with_cache_ttl(3600)
|
|
12
|
+
# .build
|
|
13
|
+
class Configuration
|
|
14
|
+
DEFAULT_CACHE_TTL = 3600
|
|
15
|
+
DEFAULT_CACHE_BACKEND = "memory"
|
|
16
|
+
DEFAULT_API_ENDPOINT = "https://api.zenmanage.com"
|
|
17
|
+
|
|
18
|
+
SERVER_KEY_PREFIX = "srv_"
|
|
19
|
+
CLIENT_KEY_PREFIX = "cli_"
|
|
20
|
+
MOBILE_KEY_PREFIX = "mob_"
|
|
21
|
+
|
|
22
|
+
attr_reader :environment_token, :cache_ttl, :cache_backend, :cache_directory,
|
|
23
|
+
:enable_usage_reporting, :api_endpoint, :sdk_version, :client_agent, :logger, :cache
|
|
24
|
+
|
|
25
|
+
# @param environment_token [String] server environment token (must start with `srv_`)
|
|
26
|
+
# @param cache_ttl [Integer] seconds rules are cached (default: 3600)
|
|
27
|
+
# @param cache_backend [String] one of `"memory"`, `"filesystem"`, `"null"`
|
|
28
|
+
# @param cache_directory [String, nil] required when cache_backend is `"filesystem"`
|
|
29
|
+
# @param enable_usage_reporting [Boolean] send usage events to the API (default: true)
|
|
30
|
+
# @param api_endpoint [String] override the Zenmanage API endpoint
|
|
31
|
+
# @param sdk_version [String, nil] override the SDK version reported in `X-ZEN-CLIENT-AGENT`
|
|
32
|
+
# @param client_agent [String, nil] override the client agent string
|
|
33
|
+
# @param logger [Logger, nil] custom logger; defaults to a null logger
|
|
34
|
+
# @param cache [Cache::Base, nil] your own cache backend; used instead of `cache_backend`
|
|
35
|
+
# @raise [ConfigurationError] if any validation fails
|
|
36
|
+
def initialize(
|
|
37
|
+
environment_token:,
|
|
38
|
+
cache_ttl: DEFAULT_CACHE_TTL,
|
|
39
|
+
cache_backend: DEFAULT_CACHE_BACKEND,
|
|
40
|
+
cache_directory: nil,
|
|
41
|
+
enable_usage_reporting: true,
|
|
42
|
+
api_endpoint: DEFAULT_API_ENDPOINT,
|
|
43
|
+
sdk_version: nil,
|
|
44
|
+
client_agent: nil,
|
|
45
|
+
logger: nil,
|
|
46
|
+
cache: nil
|
|
47
|
+
)
|
|
48
|
+
@environment_token = environment_token
|
|
49
|
+
@cache_ttl = cache_ttl
|
|
50
|
+
@cache_backend = cache_backend
|
|
51
|
+
@cache_directory = cache_directory
|
|
52
|
+
@enable_usage_reporting = enable_usage_reporting
|
|
53
|
+
@api_endpoint = api_endpoint
|
|
54
|
+
@sdk_version = sdk_version
|
|
55
|
+
@client_agent = client_agent
|
|
56
|
+
@logger = logger || NullLogger.new
|
|
57
|
+
@cache = cache
|
|
58
|
+
|
|
59
|
+
validate!
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# @return [Boolean]
|
|
63
|
+
def usage_reporting_enabled?
|
|
64
|
+
@enable_usage_reporting
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def validate!
|
|
70
|
+
validate_environment_token!
|
|
71
|
+
|
|
72
|
+
raise ConfigurationError, "cache_ttl must be non-negative" unless cache_ttl >= 0
|
|
73
|
+
|
|
74
|
+
validate_cache!
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def validate_cache!
|
|
78
|
+
if cache
|
|
79
|
+
return if cache.respond_to?(:get) && cache.respond_to?(:set)
|
|
80
|
+
|
|
81
|
+
raise ConfigurationError, "cache must respond to get and set"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
unless %w[memory filesystem null].include?(cache_backend)
|
|
85
|
+
raise ConfigurationError, "cache_backend is invalid: #{cache_backend}"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
return unless cache_backend == "filesystem" && (cache_directory.nil? || cache_directory.empty?)
|
|
89
|
+
|
|
90
|
+
raise ConfigurationError, "cache_directory is required for filesystem cache backend"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def validate_environment_token!
|
|
94
|
+
raise ConfigurationError, "environment_token is required" if environment_token.nil? || environment_token.empty?
|
|
95
|
+
|
|
96
|
+
[CLIENT_KEY_PREFIX, MOBILE_KEY_PREFIX].each do |prefix|
|
|
97
|
+
next unless environment_token.start_with?(prefix)
|
|
98
|
+
|
|
99
|
+
raise ConfigurationError,
|
|
100
|
+
"client-side tokens (#{prefix}) are not supported for server-side SDKs. Use a server token (srv_)."
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
return if environment_token.start_with?(SERVER_KEY_PREFIX)
|
|
104
|
+
|
|
105
|
+
raise ConfigurationError,
|
|
106
|
+
"Invalid environment token for Ruby SDK. Expected a case-sensitive server key prefixed with srv_."
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Minimal logger that discards all output.
|
|
111
|
+
# @api private
|
|
112
|
+
class NullLogger
|
|
113
|
+
def debug(*); end
|
|
114
|
+
def info(*); end
|
|
115
|
+
def warn(*); end
|
|
116
|
+
def error(*); end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zenmanage
|
|
4
|
+
# Evaluation context carrying identity and attributes for rule matching.
|
|
5
|
+
#
|
|
6
|
+
# @example Anonymous context (default)
|
|
7
|
+
# context = Zenmanage::Context.new("anonymous")
|
|
8
|
+
#
|
|
9
|
+
# @example User context
|
|
10
|
+
# context = Zenmanage::Context.new("user", name: "Jane Doe", identifier: "user-123")
|
|
11
|
+
#
|
|
12
|
+
# @example Context with attributes
|
|
13
|
+
# context = Zenmanage::Context.new("user",
|
|
14
|
+
# identifier: "user-123",
|
|
15
|
+
# attributes: [Zenmanage::Attribute.new("plan", ["pro"])]
|
|
16
|
+
# )
|
|
17
|
+
#
|
|
18
|
+
# @example Convenience factory
|
|
19
|
+
# context = Zenmanage::Context.single("user", "user-123", name: "Jane Doe")
|
|
20
|
+
class Context
|
|
21
|
+
attr_reader :type, :name, :identifier
|
|
22
|
+
|
|
23
|
+
# @param type [String] context type (e.g., `"user"`, `"organization"`)
|
|
24
|
+
# @param name [String, nil] display name
|
|
25
|
+
# @param identifier [String, nil] unique identifier used for bucketing and rule matching
|
|
26
|
+
# @param attributes [Array<Attribute>] additional attributes for rule matching
|
|
27
|
+
def initialize(type, name: nil, identifier: nil, attributes: [])
|
|
28
|
+
@type = type
|
|
29
|
+
@name = name
|
|
30
|
+
@identifier = identifier
|
|
31
|
+
@attributes = {}
|
|
32
|
+
Array(attributes).each { |attr| add_attribute(attr) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @deprecated Use {#identifier} instead.
|
|
36
|
+
alias id identifier
|
|
37
|
+
|
|
38
|
+
# Create a simple context with an identifier and optional name.
|
|
39
|
+
#
|
|
40
|
+
# @param type [String]
|
|
41
|
+
# @param identifier [String]
|
|
42
|
+
# @param name [String, nil]
|
|
43
|
+
# @return [Context]
|
|
44
|
+
def self.single(type, identifier, name: nil)
|
|
45
|
+
new(type, identifier: identifier, name: name)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Build a Context from a plain Hash (e.g., from cached JSON).
|
|
49
|
+
#
|
|
50
|
+
# @param data [Hash]
|
|
51
|
+
# @return [Context]
|
|
52
|
+
def self.from_hash(data)
|
|
53
|
+
new(
|
|
54
|
+
HashAccess.string(data, :type, "user"),
|
|
55
|
+
name: HashAccess.string(data, :name),
|
|
56
|
+
identifier: HashAccess.string(data, :identifier),
|
|
57
|
+
attributes: HashAccess.array(data, :attributes).filter_map { |attr| attribute_from(attr) },
|
|
58
|
+
)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Build one {Attribute} from its payload hash, skipping entries that aren't
|
|
62
|
+
# hashes, carry no key, or resolve to no usable values.
|
|
63
|
+
#
|
|
64
|
+
# @return [Attribute, nil]
|
|
65
|
+
def self.attribute_from(data)
|
|
66
|
+
return unless data.is_a?(Hash)
|
|
67
|
+
|
|
68
|
+
key = HashAccess.value(data, :key)
|
|
69
|
+
return unless key
|
|
70
|
+
|
|
71
|
+
values = HashAccess.attribute_values(data)
|
|
72
|
+
Attribute.new(key.to_s, values) unless values.empty?
|
|
73
|
+
end
|
|
74
|
+
private_class_method :attribute_from
|
|
75
|
+
|
|
76
|
+
# Add an attribute to the context.
|
|
77
|
+
#
|
|
78
|
+
# @param attribute [Attribute]
|
|
79
|
+
# @return [self]
|
|
80
|
+
def add_attribute(attribute)
|
|
81
|
+
@attributes[attribute.key] = attribute
|
|
82
|
+
self
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Retrieve an attribute by key.
|
|
86
|
+
#
|
|
87
|
+
# @param key [String]
|
|
88
|
+
# @return [Attribute, nil]
|
|
89
|
+
def attribute(key)
|
|
90
|
+
@attributes[key]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# @param key [String]
|
|
94
|
+
# @return [Boolean]
|
|
95
|
+
def attribute?(key)
|
|
96
|
+
@attributes.key?(key)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @return [Array<Attribute>]
|
|
100
|
+
def attributes
|
|
101
|
+
@attributes.values
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# @return [Hash]
|
|
105
|
+
def to_h
|
|
106
|
+
result = { "type" => @type }
|
|
107
|
+
result["name"] = @name if @name
|
|
108
|
+
result["identifier"] = @identifier if @identifier
|
|
109
|
+
result["attributes"] = @attributes.values.map(&:to_h) unless @attributes.empty?
|
|
110
|
+
result
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Rails serializers pass an options argument.
|
|
114
|
+
#
|
|
115
|
+
# @return [Hash]
|
|
116
|
+
def as_json(*)
|
|
117
|
+
to_h
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# @return [String]
|
|
121
|
+
def to_s
|
|
122
|
+
to_h.to_json
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zenmanage
|
|
4
|
+
# Collection of default flag values used when a flag is not found in the API response.
|
|
5
|
+
#
|
|
6
|
+
# @example
|
|
7
|
+
# defaults = Zenmanage::DefaultsCollection.new
|
|
8
|
+
# .set("dark-mode", false)
|
|
9
|
+
# .set("checkout-layout", "classic")
|
|
10
|
+
# .set("rate-limit", 100)
|
|
11
|
+
#
|
|
12
|
+
# flags = client.flags.with_defaults(defaults)
|
|
13
|
+
class DefaultsCollection
|
|
14
|
+
# @param defaults [Hash{String => Object}]
|
|
15
|
+
def initialize(defaults = {})
|
|
16
|
+
@defaults = defaults.transform_keys(&:to_s)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Build a DefaultsCollection from a Hash.
|
|
20
|
+
#
|
|
21
|
+
# @param hash [Hash{String => Object}]
|
|
22
|
+
# @return [DefaultsCollection]
|
|
23
|
+
def self.from_hash(hash)
|
|
24
|
+
new(hash)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Set a default value for a flag key.
|
|
28
|
+
#
|
|
29
|
+
# @param key [String]
|
|
30
|
+
# @param value [Boolean, String, Numeric, Hash, Array]
|
|
31
|
+
# @return [self]
|
|
32
|
+
def set(key, value)
|
|
33
|
+
@defaults[key.to_s] = value
|
|
34
|
+
self
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Get the default value for a flag key.
|
|
38
|
+
#
|
|
39
|
+
# @param key [String]
|
|
40
|
+
# @return [Object, nil]
|
|
41
|
+
def get(key)
|
|
42
|
+
@defaults[key.to_s]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @param key [String]
|
|
46
|
+
# @return [Boolean]
|
|
47
|
+
def has?(key)
|
|
48
|
+
@defaults.key?(key.to_s)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# @return [Hash{String => Object}]
|
|
52
|
+
def all
|
|
53
|
+
@defaults.dup
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @return [Hash{String => Object}]
|
|
57
|
+
def to_h
|
|
58
|
+
@defaults.dup
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Rails serializers pass an options argument.
|
|
62
|
+
#
|
|
63
|
+
# @return [Hash]
|
|
64
|
+
def as_json(*)
|
|
65
|
+
to_h
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|