Class: Featureflow::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/featureflow/client.rb', line 7

def initialize(configuration = nil)
  @configuration = configuration || Featureflow.configuration
  @configuration.validate!
  @features = {}

  Featureflow.logger.info 'initializing client'

  @failover_variants = {}
  @configuration.with_features.each do |feature|
    Featureflow.logger.info "Registering feature with key #{feature[:key]}"
    failover = feature[:failover_variant];
    @failover_variants[feature[:key]] = failover if failover.is_a?(String) && !failover.empty?
  end

  unless @configuration.disable_events
    @events_client = EventsClient.new @configuration.event_endpoint, @configuration.api_key
    @events_client.register_features @configuration.with_features
  end

  reload

  Featureflow.logger.info 'client initialized'
end

Instance Method Details

#build_polling_clientObject



36
37
38
39
40
41
42
43
# File 'lib/featureflow/client.rb', line 36

def build_polling_client
  PollingClient.new(
    @configuration.endpoint,
    @configuration.api_key,
    poll_interval: 10,
    timeout: 30
  )
end

#evaluate(key, user) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/featureflow/client.rb', line 53

def evaluate(key, user)
  raise ArgumentError, 'key must be a string' unless key.is_a?(String)
  raise ArgumentError, 'user is required' unless user
  unless user.is_a?(String) || user.is_a?(Hash)
    raise ArgumentError, 'user must be either a string user id,' + \
                         ' or a Hash built using Featureflow::UserBuilder)'
  end

  user = UserBuilder.new(user).build if user.is_a?(String)

  user = user.dup
  user[:attributes] = user[:attributes].merge('featureflow.user.id' => user[:id])

  Evaluate.new(
    feature_key: key,
    feature: feature(key),
    failover_variant: failover_variant(key),
    user: user,
    salt: '1',
    events_client: @events_client
  )
end

#failover_variant(key) ⇒ Object



49
50
51
# File 'lib/featureflow/client.rb', line 49

def failover_variant(key)
  @failover_variants[key]
end

#feature(key) ⇒ Object



45
46
47
# File 'lib/featureflow/client.rb', line 45

def feature(key)
  @polling_client.feature(key)
end

#reloadObject



31
32
33
34
# File 'lib/featureflow/client.rb', line 31

def reload
  @polling_client.finish if @polling_client
  @polling_client = build_polling_client
end