Class: Schematic::SchematicClient
- Inherits:
-
Object
- Object
- Schematic::SchematicClient
- Defined in:
- lib/schematic/schematic_client.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.schematichq.com"- DEFAULT_CACHE_TTL =
seconds
5.0- DEFAULT_CACHE_MAX_SIZE =
1000- DEFAULT_EVENT_BUFFER_PERIOD =
seconds (canonical Go value)
5.0- TRACK_OPTION_KEYS =
Optional event metadata accepted via the ‘options:` keyword on track/identify. identify only honors :idempotency_key; track also honors :sent_at, :trusted_client_clock, and :backfill. Fields are only sent when set.
%i[idempotency_key sent_at trusted_client_clock backfill].freeze
- IDENTIFY_OPTION_KEYS =
%i[idempotency_key].freeze
Instance Attribute Summary collapse
-
#api_client ⇒ Object
readonly
Returns the value of attribute api_client.
Instance Method Summary collapse
- #accesstokens ⇒ Object
-
#accounts ⇒ Object
— Fern API Access —.
- #billing ⇒ Object
-
#check_flag(flag_key, company: nil, user: nil) ⇒ Object
— Flag Checking —.
- #check_flag_with_entitlement(flag_key, company: nil, user: nil) ⇒ Object
- #check_flags(company: nil, user: nil, keys: nil) ⇒ Object
- #checkout ⇒ Object
-
#close ⇒ Object
— Lifecycle —.
- #companies ⇒ Object
- #components ⇒ Object
- #componentspublic ⇒ Object
- #credits ⇒ Object
- #dataexports ⇒ Object
- #entitlements ⇒ Object
- #events ⇒ Object
- #features ⇒ Object
-
#identify(body, options: nil) ⇒ Object
— Event Submission —.
-
#initialize(api_key: nil, base_url: nil, offline: false, flag_defaults: {}, cache_providers: nil, event_buffer_period: DEFAULT_EVENT_BUFFER_PERIOD, event_capture_base_url: nil, use_data_stream: false, datastream_options: {}, logger: nil, log_level: :warn) ⇒ SchematicClient
constructor
A new instance of SchematicClient.
- #planbundle ⇒ Object
- #plangroups ⇒ Object
- #planmigrations ⇒ Object
- #plans ⇒ Object
- #scheduledcheckout ⇒ Object
-
#set_flag_default(flag_key, value) ⇒ Object
— Flag Defaults —.
-
#set_flag_defaults(defaults) ⇒ Object
rubocop:disable Naming/AccessorMethodName.
- #track(body, options: nil) ⇒ Object
- #webhooks ⇒ Object
Constructor Details
#initialize(api_key: nil, base_url: nil, offline: false, flag_defaults: {}, cache_providers: nil, event_buffer_period: DEFAULT_EVENT_BUFFER_PERIOD, event_capture_base_url: nil, use_data_stream: false, datastream_options: {}, logger: nil, log_level: :warn) ⇒ SchematicClient
Returns a new instance of SchematicClient.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/schematic/schematic_client.rb', line 65 def initialize( api_key: nil, base_url: nil, offline: false, flag_defaults: {}, cache_providers: nil, event_buffer_period: DEFAULT_EVENT_BUFFER_PERIOD, event_capture_base_url: nil, use_data_stream: false, datastream_options: {}, logger: nil, log_level: :warn ) @api_key = api_key @base_url = base_url || DEFAULT_BASE_URL @flag_defaults = flag_defaults.dup @flag_defaults_mutex = Mutex.new @logger = logger || ConsoleLogger.new(level: log_level) @closed = false # Auto-enable offline mode if no API key if (api_key.nil? || api_key.empty?) && !offline @logger.warn("No API key provided, enabling offline mode") offline = true end @offline = offline # Initialize Fern-generated API client @api_client = if @offline nil else Schematic::Client.new( api_key: @api_key, base_url: @base_url ) end # Cache setup @flag_check_cache_providers = cache_providers || [ LocalCache.new(max_size: DEFAULT_CACHE_MAX_SIZE, ttl: DEFAULT_CACHE_TTL) ] # Event buffer setup — sends to capture service (not the Fern API) @event_buffer = EventBuffer.new( api_key: @api_key || "", logger: @logger, interval: event_buffer_period, offline: @offline, capture_base_url: event_capture_base_url || EventBuffer::DEFAULT_CAPTURE_BASE_URL ) # DataStream setup @datastream_client = nil @rules_engine = nil setup_datastream() if use_data_stream && !@offline # Register shutdown hook to ensure graceful cleanup on process exit at_exit { close } end |
Instance Attribute Details
#api_client ⇒ Object (readonly)
Returns the value of attribute api_client.
52 53 54 |
# File 'lib/schematic/schematic_client.rb', line 52 def api_client @api_client end |
Instance Method Details
#accesstokens ⇒ Object
363 364 365 |
# File 'lib/schematic/schematic_client.rb', line 363 def accesstokens @api_client&.accesstokens end |
#accounts ⇒ Object
— Fern API Access —
299 300 301 |
# File 'lib/schematic/schematic_client.rb', line 299 def accounts @api_client&.accounts end |
#billing ⇒ Object
303 304 305 |
# File 'lib/schematic/schematic_client.rb', line 303 def billing @api_client&.billing end |
#check_flag(flag_key, company: nil, user: nil) ⇒ Object
— Flag Checking —
127 128 129 |
# File 'lib/schematic/schematic_client.rb', line 127 def check_flag(flag_key, company: nil, user: nil) check_flag_with_entitlement(flag_key, company: company, user: user).value end |
#check_flag_with_entitlement(flag_key, company: nil, user: nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/schematic/schematic_client.rb', line 131 def check_flag_with_entitlement(flag_key, company: nil, user: nil) # Offline mode if @offline return CheckFlagResponse.new( value: get_flag_default(flag_key), flag_key: flag_key, reason: "offline mode" ) end # DataStream path if @datastream_client&.connected? begin eval_ctx = build_eval_context(company, user) result = @datastream_client.check_flag(eval_ctx, flag_key) response = CheckFlagResponse.new(result) enqueue_flag_check_event(flag_key, response, company, user) return response rescue DataStream::EvaluationError => e @logger.debug("DataStream flag check unavailable, falling back to API: #{e.}") rescue StandardError => e @logger.warn("DataStream flag check failed, falling back to API: #{e.}") end end # API path with caching check_flag_via_api(flag_key, company, user) rescue StandardError => e @logger.error("check_flag_with_entitlement error for '#{flag_key}': #{e.}") CheckFlagResponse.new( value: get_flag_default(flag_key), flag_key: flag_key, reason: "error: #{e.}" ) end |
#check_flags(company: nil, user: nil, keys: nil) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/schematic/schematic_client.rb', line 168 def check_flags(company: nil, user: nil, keys: nil) if @offline @logger.debug("Offline mode enabled, returning default flag values") flag_keys = if keys.nil? || keys.empty? @flag_defaults_mutex.synchronize { @flag_defaults.keys } else keys end return flag_keys.map { |k| { flag: k, value: get_flag_default(k), reason: "Offline mode - using default value" } } end begin # DataStream path — try evaluating all requested keys locally if @datastream_client&.connected? && keys&.any? ds_results = check_flags_via_datastream(keys, company, user) return ds_results if ds_results end eval_body = {} eval_body[:company] = company if company&.any? eval_body[:user] = user if user&.any? # If no keys, call API directly if keys.nil? || keys.empty? @logger.debug("No specific flag keys provided, calling checkFlags API") api_response = @api_client.features.check_flags(**eval_body) return api_response.data.flags.map { |f| { flag: f.flag, value: f.value, reason: f.reason } } end # Check cache for all requested keys all_cached = true cached_results = {} keys.each do |key| cache_key = build_cache_key(key, company, user) found = false @flag_check_cache_providers.each do |provider| cached = provider.get(cache_key) next unless cached cached_results[key] = { flag: key, value: cached.value, reason: cached.reason } found = true break end all_cached = false unless found end # All cached — return without API call if all_cached @logger.debug("All #{keys.length} flags found in cache") return keys.map { |k| cached_results[k] } end # Any cache miss — fetch fresh values for ALL keys from API to ensure consistency @logger.debug("Cache miss for some flags, calling API for all #{keys.length} keys") api_response = @api_client.features.check_flags(**eval_body) api_results = {} api_response.data.flags.each do |f| api_results[f.flag] = f # Cache the fresh result cache_key = build_cache_key(f.flag, company, user) response = CheckFlagResponse.new( value: f.value, flag_key: f.flag, flag_id: f.respond_to?(:flag_id) ? f.flag_id : nil, reason: f.reason, rule_id: f.respond_to?(:rule_id) ? f.rule_id : nil, rule_type: f.respond_to?(:rule_type) ? f.rule_type : nil, company_id: f.respond_to?(:company_id) ? f.company_id : nil, user_id: f.respond_to?(:user_id) ? f.user_id : nil ) @flag_check_cache_providers.each { |p| p.set(cache_key, response) } end # Build results in requested key order, preferring fresh API values keys.map do |key| if api_results[key] f = api_results[key] { flag: f.flag, value: f.value, reason: f.reason } else { flag: key, value: get_flag_default(key), reason: "Flag not found - using default value" } end end rescue StandardError => e @logger.error("Error checking flags: #{e.}") (keys || []).map { |k| { flag: k, value: get_flag_default(k), reason: "Error occurred - using default value: #{e.}" } } end end |
#checkout ⇒ Object
315 316 317 |
# File 'lib/schematic/schematic_client.rb', line 315 def checkout @api_client&.checkout end |
#close ⇒ Object
— Lifecycle —
373 374 375 376 377 378 379 380 381 |
# File 'lib/schematic/schematic_client.rb', line 373 def close return if @closed @closed = true @event_buffer.stop @datastream_client&.close @flag_check_cache_providers.each { |c| c.stop if c.respond_to?(:stop) } @logger.debug("SchematicClient closed") end |
#companies ⇒ Object
307 308 309 |
# File 'lib/schematic/schematic_client.rb', line 307 def companies @api_client&.companies end |
#components ⇒ Object
327 328 329 |
# File 'lib/schematic/schematic_client.rb', line 327 def components @api_client&.components end |
#componentspublic ⇒ Object
355 356 357 |
# File 'lib/schematic/schematic_client.rb', line 355 def componentspublic @api_client&.componentspublic end |
#credits ⇒ Object
311 312 313 |
# File 'lib/schematic/schematic_client.rb', line 311 def credits @api_client&.credits end |
#dataexports ⇒ Object
331 332 333 |
# File 'lib/schematic/schematic_client.rb', line 331 def dataexports @api_client&.dataexports end |
#entitlements ⇒ Object
319 320 321 |
# File 'lib/schematic/schematic_client.rb', line 319 def entitlements @api_client&.entitlements end |
#events ⇒ Object
335 336 337 |
# File 'lib/schematic/schematic_client.rb', line 335 def events @api_client&.events end |
#features ⇒ Object
339 340 341 |
# File 'lib/schematic/schematic_client.rb', line 339 def features @api_client&.features end |
#identify(body, options: nil) ⇒ Object
— Event Submission —
260 261 262 263 264 265 266 |
# File 'lib/schematic/schematic_client.rb', line 260 def identify(body, options: nil) return if @offline @event_buffer.push(build_event("identify", body, , IDENTIFY_OPTION_KEYS)) rescue StandardError => e @logger.error("Error sending identify event: #{e.}") end |
#planbundle ⇒ Object
343 344 345 |
# File 'lib/schematic/schematic_client.rb', line 343 def planbundle @api_client&.planbundle end |
#plangroups ⇒ Object
347 348 349 |
# File 'lib/schematic/schematic_client.rb', line 347 def plangroups @api_client&.plangroups end |
#planmigrations ⇒ Object
351 352 353 |
# File 'lib/schematic/schematic_client.rb', line 351 def planmigrations @api_client&.planmigrations end |
#plans ⇒ Object
323 324 325 |
# File 'lib/schematic/schematic_client.rb', line 323 def plans @api_client&.plans end |
#scheduledcheckout ⇒ Object
359 360 361 |
# File 'lib/schematic/schematic_client.rb', line 359 def scheduledcheckout @api_client&.scheduledcheckout end |
#set_flag_default(flag_key, value) ⇒ Object
— Flag Defaults —
285 286 287 288 289 |
# File 'lib/schematic/schematic_client.rb', line 285 def set_flag_default(flag_key, value) @flag_defaults_mutex.synchronize do @flag_defaults[flag_key] = value end end |
#set_flag_defaults(defaults) ⇒ Object
rubocop:disable Naming/AccessorMethodName
291 292 293 294 295 |
# File 'lib/schematic/schematic_client.rb', line 291 def set_flag_defaults(defaults) # rubocop:disable Naming/AccessorMethodName @flag_defaults_mutex.synchronize do @flag_defaults.merge!(defaults) end end |
#track(body, options: nil) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/schematic/schematic_client.rb', line 268 def track(body, options: nil) return if @offline @event_buffer.push(build_event("track", body, , TRACK_OPTION_KEYS)) # Update company metrics locally if DataStream is active and connected if @datastream_client&.connected? && body[:company] event_name = body[:event] || body["event"] quantity = body[:quantity] || body["quantity"] || 1 @datastream_client.update_company_metrics(body[:company], event_name, quantity) end rescue StandardError => e @logger.error("Error sending track event: #{e.}") end |
#webhooks ⇒ Object
367 368 369 |
# File 'lib/schematic/schematic_client.rb', line 367 def webhooks @api_client&.webhooks end |