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.

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/v2/vardata?v=0"
  @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 synchronously.

This method will automatically retry if configured (default).



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

def fetch(user)
  AmplitudeExperiment.filter_default_variants(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 asynchronously.

This method will automatically retry if configured (default).

Yields:

  • (User, Hash)

    callback block takes user object and variants hash



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/experiment/remote/client.rb', line 57

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

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

Fetch all variants for a user asynchronously. This function differs from fetch as it will return a default variant object if the flag was evaluated but the user was not assigned (i.e. off).

This method will automatically retry if configured (default).

Yields:

  • (User, Hash)

    callback block takes user object and variants hash



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/experiment/remote/client.rb', line 75

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

#fetch_v2(user) ⇒ Hash

Fetch all variants for a user synchronously.

This method will automatically retry if configured (default). This function differs from fetch as it will return a default variant object if the flag was evaluated but the user was not assigned (i.e. off).



45
46
47
48
49
50
# File 'lib/experiment/remote/client.rb', line 45

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