Class: LaunchDarkly::LDClient
- Inherits:
-
Object
- Object
- LaunchDarkly::LDClient
- Includes:
- Evaluation
- Defined in:
- lib/ldclient-rb/ldclient.rb
Overview
A client for LaunchDarkly. Client instances are thread-safe. Users should create a single client instance for the lifetime of the application.
Constant Summary
Constants included from Evaluation
Evaluation::BUILTINS, Evaluation::OPERATORS
Instance Method Summary collapse
-
#all_flags(user) ⇒ Object
Returns all feature flag values for the given user.
- #flush ⇒ Object
-
#identify(user) ⇒ void
Registers the user.
-
#initialize(sdk_key, config = Config.default, wait_for_sec = 5) ⇒ LDClient
constructor
Creates a new client instance that connects to LaunchDarkly.
-
#initialized? ⇒ Boolean
Returns whether the client has been initialized and is ready to serve feature flag requests.
- #secure_mode_hash(user) ⇒ Object
- #toggle?(key, user, default = False) ⇒ Boolean
-
#track(event_name, user, data) ⇒ void
Tracks that a user performed an event.
-
#variation(key, user, default) ⇒ Object
Determines the variation of a feature flag to present to a user.
Methods included from Evaluation
#bucket_user, #clause_match_user, #eval_internal, #eval_rules, #evaluate, #get_variation, #match_any, #maybe_negate, #rule_match_user, #user_value, #variation_for_user
Constructor Details
#initialize(sdk_key, config = Config.default, wait_for_sec = 5) ⇒ LDClient
Creates a new client instance that connects to LaunchDarkly. A custom configuration parameter can also supplied to specify advanced options, but for most use cases, the default configuration is appropriate.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ldclient-rb/ldclient.rb', line 26 def initialize(sdk_key, config = Config.default, wait_for_sec = 5) @sdk_key = sdk_key @config = config @store = config.feature_store requestor = Requestor.new(sdk_key, config) if !@config.offline? if @config.stream? @update_processor = StreamProcessor.new(sdk_key, config, requestor) else @update_processor = PollingProcessor.new(config, requestor) end @update_processor.start end @event_processor = EventProcessor.new(sdk_key, config) if !@config.offline? && wait_for_sec > 0 begin WaitUtil.wait_for_condition("LaunchDarkly client initialization", :timeout_sec => wait_for_sec, :delay_sec => 0.1) do @update_processor.initialized? end rescue WaitUtil::TimeoutError @config.logger.error("[LDClient] Timeout encountered waiting for LaunchDarkly client initialization") end end end |
Instance Method Details
#all_flags(user) ⇒ Object
Returns all feature flag values for the given user
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ldclient-rb/ldclient.rb', line 178 def all_flags(user) sanitize_user(user) return Hash.new if @config.offline? unless user @config.logger.error("[LDClient] Must specify user in all_flags") return Hash.new end begin features = @store.all # TODO rescue if necessary Hash[features.map{|k,f| [k, evaluate(f, user, @store)[:value]] }] rescue => exn @config.logger.warn("[LDClient] Error evaluating all flags: #{exn.inspect}. \nTrace: #{exn.backtrace}") return Hash.new end end |
#flush ⇒ Object
54 55 56 |
# File 'lib/ldclient-rb/ldclient.rb', line 54 def flush @event_processor.flush end |
#identify(user) ⇒ void
This method returns an undefined value.
Registers the user
157 158 159 160 |
# File 'lib/ldclient-rb/ldclient.rb', line 157 def identify(user) sanitize_user(user) @event_processor.add_event(kind: "identify", key: user[:key], user: user) end |
#initialized? ⇒ Boolean
Returns whether the client has been initialized and is ready to serve feature flag requests
69 70 71 |
# File 'lib/ldclient-rb/ldclient.rb', line 69 def initialized? @update_processor.initialized? end |
#secure_mode_hash(user) ⇒ Object
63 64 65 |
# File 'lib/ldclient-rb/ldclient.rb', line 63 def secure_mode_hash(user) OpenSSL::HMAC.hexdigest('sha256', @sdk_key, user[:key].to_s) end |
#toggle?(key, user, default = False) ⇒ Boolean
58 59 60 61 |
# File 'lib/ldclient-rb/ldclient.rb', line 58 def toggle?(key, user, default = False) @config.logger.warn("[LDClient] toggle? is deprecated. Use variation instead") variation(key, user, default) end |
#track(event_name, user, data) ⇒ void
This method returns an undefined value.
Tracks that a user performed an event
170 171 172 173 |
# File 'lib/ldclient-rb/ldclient.rb', line 170 def track(event_name, user, data) sanitize_user(user) @event_processor.add_event(kind: "custom", key: event_name, user: user, data: data) end |
#variation(key, user, default) ⇒ Object
Determines the variation of a feature flag to present to a user. At a minimum, the user hash should contain a :key
.
For authenticated users, the :key
should be the unique identifier for your user. For anonymous users, the :key
should be a session identifier or cookie. In either case, the only requirement is that the key is unique to a user.
You can also pass IP addresses and country codes in the user hash.
The user hash can contain arbitrary custom attributes stored in a :custom
sub-hash:
Attribute values in the custom hash can be integers, booleans, strings, or
lists of integers, booleans, or strings.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ldclient-rb/ldclient.rb', line 105 def variation(key, user, default) return default if @config.offline? unless user @config.logger.error("[LDClient] Must specify user") @event_processor.add_event(kind: "feature", key: key, value: default, default: default, user: user) return default end if !@update_processor.initialized? @config.logger.error("[LDClient] Client has not finished initializing. Returning default value") @event_processor.add_event(kind: "feature", key: key, value: default, default: default, user: user) return default end sanitize_user(user) feature = @store.get(key) if feature.nil? @config.logger.error("[LDClient] Unknown feature flag #{key}. Returning default value") @event_processor.add_event(kind: "feature", key: key, value: default, default: default, user: user) return default end begin res = evaluate(feature, user, @store) if !res[:events].nil? res[:events].each do |event| @event_processor.add_event(event) end end if !res[:value].nil? @event_processor.add_event(kind: "feature", key: key, user: user, value: res[:value], default: default, version: feature[:version]) return res[:value] else @config.logger.debug("[LDClient] Result value is null in toggle") @event_processor.add_event(kind: "feature", key: key, user: user, value: default, default: default, version: feature[:version]) return default end rescue => exn @config.logger.warn("[LDClient] Error evaluating feature flag: #{exn.inspect}. \nTrace: #{exn.backtrace}") @event_processor.add_event(kind: "feature", key: key, user: user, value: default, default: default, version: feature[:version]) return default end end |