Class: Featureflow::Client
- Inherits:
-
Object
- Object
- Featureflow::Client
- Defined in:
- lib/featureflow/client.rb
Instance Method Summary collapse
- #evaluate(key, context) ⇒ Object
- #feature(key) ⇒ Object
-
#initialize(config = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(config = {}) ⇒ Client
Returns a new instance of Client.
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 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/featureflow/client.rb', line 10 def initialize(config = {}) api_key = config[:api_key] || ENV['FEATUREFLOW_SERVER_KEY'] raise ArgumentError, "You have not defined either config[:api_key] or ENV[:FEATUREFLOW_SERVER_KEY]" unless api_key Featureflow.logger.info 'initializing client' @features = {} @config = { api_key: api_key, url: 'https://app.featureflow.io', path: '/api/sdk/v1/features', with_features: [], disable_events: false }.merge(config) unless with_features_valid? @config[:with_features] raise ArgumentError, 'config[:with_features] must be an array of Feature hashes. Use Featureflow::Feature.create(key, failover_variant)' end @failover_variants = {} @config[: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 @config[:disable_events] @events_client = EventsClient.new @config[:url], @config[:api_key] @events_client.register_features @config[:with_features] end PollingClient.new(@config[:url] + @config[:path], @config[:api_key], delay: 30, timeout: 30) {|features| update_features(features)} Featureflow.logger.info 'client initialized' end |
Instance Method Details
#evaluate(key, context) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/featureflow/client.rb', line 51 def evaluate(key, context) raise ArgumentError, 'key must be a string' unless key.is_a?(String) raise ArgumentError, 'context is required' unless context unless context.is_a?(String) || context.is_a?(Hash) raise ArgumentError, 'context must be either a string context key,' + \ ' or a Hash built using Featureflow::ContextBuilder)' end context = ContextBuilder.new(context).build if context.is_a?(String) context = context.dup context[:values] = context[:values].merge('featureflow.key' => context[:key], 'featureflow.date' => Time.now.iso8601) Evaluate.new key, feature(key), failover_variant(key), context, '1', @events_client end |
#feature(key) ⇒ Object
47 48 49 |
# File 'lib/featureflow/client.rb', line 47 def feature(key) @features[key] end |