Class: Featureflow::Client
- Inherits:
-
Object
- Object
- Featureflow::Client
- Defined in:
- lib/featureflow/client.rb
Instance Method Summary collapse
- #build_polling_client ⇒ Object
- #evaluate(key, user) ⇒ Object
- #failover_variant(key) ⇒ Object
- #feature(key) ⇒ Object
-
#initialize(configuration = nil) ⇒ Client
constructor
A new instance of Client.
- #reload ⇒ Object
Constructor Details
#initialize(configuration = nil) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/featureflow/client.rb', line 8 def initialize(configuration = nil) @configuration = build_configuration(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_client ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/featureflow/client.rb', line 37 def build_polling_client PollingClient.new( @configuration.endpoint, @configuration.api_key, poll_interval: 10, timeout: 30 ) end |
#evaluate(key, user) ⇒ Object
54 55 56 57 58 59 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 |
# File 'lib/featureflow/client.rb', line 54 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
50 51 52 |
# File 'lib/featureflow/client.rb', line 50 def failover_variant(key) @failover_variants[key] end |
#feature(key) ⇒ Object
46 47 48 |
# File 'lib/featureflow/client.rb', line 46 def feature(key) @polling_client.feature(key) end |
#reload ⇒ Object
32 33 34 35 |
# File 'lib/featureflow/client.rb', line 32 def reload @polling_client.finish if @polling_client @polling_client = build_polling_client end |