Class: AmplitudeExperiment::RemoteEvaluationClient

Inherits:
Object
  • Object
show all
Defined in:
lib/experiment/remote/client.rb

Overview

Main client for fetching variant data.

Instance Method Summary collapse

Constructor Details

#initialize(api_key, config = nil) ⇒ RemoteEvaluationClient

Creates a new Experiment Client instance.

Parameters:

  • api_key (String)

    The environment API Key

  • config (Config) (defaults to: nil)

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/experiment/remote/client.rb', line 13

def initialize(api_key, config = nil)
  @api_key = api_key
  @config = config || RemoteEvaluationConfig.new
  @logger = Logger.new($stdout)
  @logger.level = if @config.debug
                    Logger::DEBUG
                  else
                    Logger::INFO
                  end
  endpoint = "#{@config.server_url}/sdk/vardata"
  @uri = URI(endpoint)
  raise ArgumentError, 'Experiment API key is empty' if @api_key.nil? || @api_key.empty?
end

Instance Method Details

#fetch(user) ⇒ Hash

Fetch all variants for a user synchronous.

This method will automatically retry if configured (default).

Parameters:

Returns:

  • (Hash)

    Variants Hash



32
33
34
35
36
37
# File 'lib/experiment/remote/client.rb', line 32

def fetch(user)
  fetch_internal(user)
rescue StandardError => e
  @logger.error("[Experiment] Failed to fetch variants: #{e.message}")
  {}
end

#fetch_async(user) {|User, Hash| ... } ⇒ Object

Fetch all variants for a user asynchronous.

This method will automatically retry if configured (default).

Parameters:

Yields:

  • (User, Hash)

    callback block takes user object and variants hash



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/experiment/remote/client.rb', line 44

def fetch_async(user, &callback)
  Thread.new do
    variants = fetch_internal(user)
    yield(user, variants) unless callback.nil?
    variants
  rescue StandardError => e
    @logger.error("[Experiment] Failed to fetch variants: #{e.message}")
    yield(user, {}) unless callback.nil?
    {}
  end
end