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
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) ⇒ 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: :info) ⇒ 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) ⇒ 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: :info) ⇒ SchematicClient
Returns a new instance of SchematicClient.
59 60 61 62 63 64 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 |
# File 'lib/schematic/schematic_client.rb', line 59 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: :info ) @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
365 366 367 |
# File 'lib/schematic/schematic_client.rb', line 365 def accesstokens @api_client&.accesstokens end |
#accounts ⇒ Object
— Fern API Access —
301 302 303 |
# File 'lib/schematic/schematic_client.rb', line 301 def accounts @api_client&.accounts end |
#billing ⇒ Object
305 306 307 |
# File 'lib/schematic/schematic_client.rb', line 305 def billing @api_client&.billing end |
#check_flag(flag_key, company: nil, user: nil) ⇒ Object
— Flag Checking —
121 122 123 |
# File 'lib/schematic/schematic_client.rb', line 121 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
125 126 127 128 129 130 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 |
# File 'lib/schematic/schematic_client.rb', line 125 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
162 163 164 165 166 167 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 |
# File 'lib/schematic/schematic_client.rb', line 162 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
317 318 319 |
# File 'lib/schematic/schematic_client.rb', line 317 def checkout @api_client&.checkout end |
#close ⇒ Object
— Lifecycle —
375 376 377 378 379 380 381 382 383 |
# File 'lib/schematic/schematic_client.rb', line 375 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
309 310 311 |
# File 'lib/schematic/schematic_client.rb', line 309 def companies @api_client&.companies end |
#components ⇒ Object
329 330 331 |
# File 'lib/schematic/schematic_client.rb', line 329 def components @api_client&.components end |
#componentspublic ⇒ Object
357 358 359 |
# File 'lib/schematic/schematic_client.rb', line 357 def componentspublic @api_client&.componentspublic end |
#credits ⇒ Object
313 314 315 |
# File 'lib/schematic/schematic_client.rb', line 313 def credits @api_client&.credits end |
#dataexports ⇒ Object
333 334 335 |
# File 'lib/schematic/schematic_client.rb', line 333 def dataexports @api_client&.dataexports end |
#entitlements ⇒ Object
321 322 323 |
# File 'lib/schematic/schematic_client.rb', line 321 def entitlements @api_client&.entitlements end |
#events ⇒ Object
337 338 339 |
# File 'lib/schematic/schematic_client.rb', line 337 def events @api_client&.events end |
#features ⇒ Object
341 342 343 |
# File 'lib/schematic/schematic_client.rb', line 341 def features @api_client&.features end |
#identify(body) ⇒ Object
— Event Submission —
254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/schematic/schematic_client.rb', line 254 def identify(body) return if @offline @event_buffer.push({ event_type: "identify", body: body, sent_at: Time.now.utc.iso8601 }) rescue StandardError => e @logger.error("Error sending identify event: #{e.}") end |
#planbundle ⇒ Object
345 346 347 |
# File 'lib/schematic/schematic_client.rb', line 345 def planbundle @api_client&.planbundle end |
#plangroups ⇒ Object
349 350 351 |
# File 'lib/schematic/schematic_client.rb', line 349 def plangroups @api_client&.plangroups end |
#planmigrations ⇒ Object
353 354 355 |
# File 'lib/schematic/schematic_client.rb', line 353 def planmigrations @api_client&.planmigrations end |
#plans ⇒ Object
325 326 327 |
# File 'lib/schematic/schematic_client.rb', line 325 def plans @api_client&.plans end |
#scheduledcheckout ⇒ Object
361 362 363 |
# File 'lib/schematic/schematic_client.rb', line 361 def scheduledcheckout @api_client&.scheduledcheckout end |
#set_flag_default(flag_key, value) ⇒ Object
— Flag Defaults —
287 288 289 290 291 |
# File 'lib/schematic/schematic_client.rb', line 287 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
293 294 295 296 297 |
# File 'lib/schematic/schematic_client.rb', line 293 def set_flag_defaults(defaults) # rubocop:disable Naming/AccessorMethodName @flag_defaults_mutex.synchronize do @flag_defaults.merge!(defaults) end end |
#track(body) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/schematic/schematic_client.rb', line 266 def track(body) return if @offline @event_buffer.push({ event_type: "track", body: body, sent_at: Time.now.utc.iso8601 }) # 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
369 370 371 |
# File 'lib/schematic/schematic_client.rb', line 369 def webhooks @api_client&.webhooks end |