Module: Kameleoon::KameleoonClientFactory

Defined in:
lib/kameleoon/kameleoon_client_factory.rb

Overview

A Factory class for creating kameleoon clients

Constant Summary collapse

CONFIG_PATH =
'/etc/kameleoon/client-ruby.yaml'

Class Method Summary collapse

Class Method Details

.create(site_code, config: nil, config_path: CONFIG_PATH) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kameleoon/kameleoon_client_factory.rb', line 15

def self.create(site_code, config: nil, config_path: CONFIG_PATH)
  Logging::KameleoonLogger.info(
    "CALL: KameleoonClientFactory.create(site_code: '%s', config: %s, config_path: '%s')",
    site_code, config, config_path
  )
  unless config.is_a?(KameleoonClientConfig)
    config_path = CONFIG_PATH unless config_path.is_a?(String)
    config = KameleoonClientConfig.read_from_yaml(config_path)
  end
  key = get_client_key(site_code, config.environment)
  client = @clients.compute_if_absent(key) do
    client = KameleoonClient.new(site_code, config)
    client.send(:fetch_configuration_initially)
    client
  end
  Logging::KameleoonLogger.info(
    "RETURN: KameleoonClientFactory.create(site_code: '%s', config: %s, config_path: '%s') -> (client)",
    site_code, config, config_path
  )
  client
end

.forget(site_code, environment = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kameleoon/kameleoon_client_factory.rb', line 37

def self.forget(site_code, environment = nil)
  Logging::KameleoonLogger.info("CALL: KameleoonClientFactory.forget(site_code: '%s', environment: '%s')",
                                site_code, environment)
  key = get_client_key(site_code, environment)
  @clients.compute_if_present(key) do |client|
    client.send(:dispose)
    nil
  end
  Logging::KameleoonLogger.info("RETURN: KameleoonClientFactory.forget(site_code: '%s', environment: '%s')",
                       site_code, environment)
end

.get_client_key(site_code, environment) ⇒ Object



51
52
53
# File 'lib/kameleoon/kameleoon_client_factory.rb', line 51

def self.get_client_key(site_code, environment)
  environment.nil? ? site_code : "#{site_code}/#{environment}"
end