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.



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

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

  # Optional workload name sent as X-Featureflow-Application on every request so
  # usage and flag evaluations are attributable per application in the dashboard.
  @application = Application.sanitise(@configuration.application)

  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, @application
    @events_client.register_features @configuration.with_features
  end

  reload

  Featureflow.logger.info 'client initialized'
end

Instance Method Details

#build_polling_clientObject



42
43
44
45
46
47
48
49
50
# File 'lib/featureflow/client.rb', line 42

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

#evaluate(key, user) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/featureflow/client.rb', line 60

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
  # Implicit attributes injected at evaluation time (matching the Node SDK's
  # EvaluateHelpers): featureflow.date/hourofday let rules target the current
  # moment; sdk-server's partial evaluation defers these to the JS client, but
  # server SDKs evaluate them locally against "now".
  now = Time.now
  user[:attributes] = user[:attributes].merge(
    'featureflow.user.id' => user[:id],
    'featureflow.date' => now.utc.iso8601,
    'featureflow.hourofday' => now.hour
  )

  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



56
57
58
# File 'lib/featureflow/client.rb', line 56

def failover_variant(key)
  @failover_variants[key]
end

#feature(key) ⇒ Object



52
53
54
# File 'lib/featureflow/client.rb', line 52

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

#reloadObject



37
38
39
40
# File 'lib/featureflow/client.rb', line 37

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