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



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kameleoon/kameleoon_client_factory.rb', line 14

def self.create(site_code, config: nil, config_path: 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)
  @clients.compute_if_absent(key) do
    client = KameleoonClient.new(site_code, config)
    client.send(:log, "Client created with site code: #{site_code}")
    client.send(:fetch_configuration_initially)
    client
  end
end

.forget(site_code, environment = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/kameleoon/kameleoon_client_factory.rb', line 28

def self.forget(site_code, environment = nil)
  key = get_client_key(site_code, environment)
  @clients.compute_if_present(key) do |client|
    client.send(:dispose)
    nil
  end
end

.get_client_key(site_code, environment) ⇒ Object



38
39
40
# File 'lib/kameleoon/kameleoon_client_factory.rb', line 38

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