Module: Kameleoon::ClientFactory

Defined in:
lib/kameleoon/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_path = CONFIG_PATH, config: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kameleoon/factory.rb', line 13

def self.create(site_code, config_path = CONFIG_PATH, config: nil)
  if config.nil?
    config_yaml = YAML.load_file(config_path) if File.exist?(config_path)
    if config_yaml.nil?
      warn "Kameleoon SDK: Configuration file with path #{config_path} does not exist" if config_yaml.nil?
      config_yaml = {}
    end
  end
  environment = config&.environment || (config_yaml || {})['environment']
  client = @clients[get_client_key(site_code, environment)]
  if client.nil?
    config = ClientConfig.make_from_yaml(config_yaml) if config.nil?
    client = Client.new(site_code, config)
    client.send(:log, "Client created with site code: #{site_code}")
    client.send(:fetch_configuration)
    @clients.store(site_code, client)
  end
  client
end

.forget(site_code, environment = '') ⇒ Object



33
34
35
# File 'lib/kameleoon/factory.rb', line 33

def self.forget(site_code, environment = '')
  @clients.delete(get_client_key(site_code, environment))
end

.get_client_key(site_code, environment) ⇒ Object



39
40
41
# File 'lib/kameleoon/factory.rb', line 39

def self.get_client_key(site_code, environment)
  site_code + (environment || '')
end