Class: Quonfig::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/quonfig/config_loader.rb

Overview

Fetches config envelopes from the Quonfig delivery API and installs them into a ConfigStore.

Wire format matches sdk-node’s Transport + ConfigStore:

GET /api/v2/configs
  -> 200 { "configs": [...], "meta": { "version": "...", "environment": "..." } }
  -> 304 Not Modified (ETag honored via If-None-Match)

The fetch is synchronous; Client is responsible for timing out the initial fetch per ‘initialization_timeout_sec`.

Constant Summary collapse

LOG =
Quonfig::InternalLogger.new(self)
CONFIGS_PATH =
'/api/v2/configs'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store_or_base_client, options = nil, logger: nil) ⇒ ConfigLoader

store: the Quonfig::ConfigStore to populate on successful fetch. options: a Quonfig::Options instance (supplies sdk_key + config_api_urls). logger: optional logger override (defaults to module LOG).

Backward compat: callers that pass a single base_client (mock client used by tests that expects ‘.options`) are still supported.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/quonfig/config_loader.rb', line 29

def initialize(store_or_base_client, options = nil, logger: nil)
  if options.nil? && store_or_base_client.respond_to?(:options)
    # Legacy shape: ConfigLoader.new(base_client)
    @options = store_or_base_client.options
    @store = nil
  else
    @store = store_or_base_client
    @options = options
  end

  @api_config = Concurrent::Map.new
  @etag = nil
  @version = nil
  @environment_id = nil
  @logger = logger || LOG
end

Instance Attribute Details

#environment_idObject (readonly)

Returns the value of attribute environment_id.



21
22
23
# File 'lib/quonfig/config_loader.rb', line 21

def environment_id
  @environment_id
end

#etagObject (readonly)

Returns the value of attribute etag.



21
22
23
# File 'lib/quonfig/config_loader.rb', line 21

def etag
  @etag
end

#versionObject (readonly)

Returns the value of attribute version.



21
22
23
# File 'lib/quonfig/config_loader.rb', line 21

def version
  @version
end

Instance Method Details

#apply_envelope(envelope) ⇒ Object

Apply a ConfigEnvelope (from SSE) to the store. Called by the SSE client when a new event arrives.



64
65
66
# File 'lib/quonfig/config_loader.rb', line 64

def apply_envelope(envelope)
  install_envelope(envelope, source: :sse)
end

#calc_configObject



68
69
70
71
72
73
74
# File 'lib/quonfig/config_loader.rb', line 68

def calc_config
  rtn = {}
  @api_config.each_key do |k|
    rtn[k] = @api_config[k]
  end
  rtn
end

#fetch!Object

Fetch configs from /api/v2/configs with ETag / If-None-Match caching. On 200 responses, installs the envelope into the attached ConfigStore (if one was provided).

Returns one of:

:updated       -- 200 response; store replaced
:not_modified  -- 304 response; store untouched
:failed        -- every configured source failed


54
55
56
57
58
59
60
# File 'lib/quonfig/config_loader.rb', line 54

def fetch!
  Array(@options.config_api_urls).each do |api_url|
    result = fetch_from(api_url)
    return result if result != :failed
  end
  :failed
end

#rm(key) ⇒ Object



80
81
82
# File 'lib/quonfig/config_loader.rb', line 80

def rm(key)
  @api_config.delete(key)
end

#set(config, source) ⇒ Object



76
77
78
# File 'lib/quonfig/config_loader.rb', line 76

def set(config, source)
  @api_config[config.key] = { source: source, config: config }
end