Class: LaunchDarkly::FeatureFlagsState
- Inherits:
-
Object
- Object
- LaunchDarkly::FeatureFlagsState
- Defined in:
- lib/ldclient-rb/flags_state.rb
Overview
A snapshot of the state of all feature flags with regard to a specific user, generated by calling the LDClient#all_flags_state. Serializing this object to JSON using ‘JSON.generate` (or the `to_json` method) will produce the appropriate data structure for bootstrapping the LaunchDarkly JavaScript client.
Instance Method Summary collapse
-
#add_flag(flag, value, variation, reason = nil, details_only_if_tracked = false) ⇒ Object
Used internally to build the state map.
-
#as_json ⇒ Object
Returns a hash that can be used as a JSON representation of the entire state map, in the format used by the LaunchDarkly JavaScript SDK.
-
#flag_value(key) ⇒ Object
Returns the value of an individual feature flag at the time the state was recorded.
-
#initialize(valid) ⇒ FeatureFlagsState
constructor
A new instance of FeatureFlagsState.
-
#to_json(*a) ⇒ Object
Same as as_json, but converts the JSON structure into a string.
-
#valid? ⇒ Boolean
Returns true if this object contains a valid snapshot of feature flag state, or false if the state could not be computed (for instance, because the client was offline or there was no user).
-
#values_map ⇒ Object
Returns a map of flag keys to flag values.
Constructor Details
#initialize(valid) ⇒ FeatureFlagsState
Returns a new instance of FeatureFlagsState.
11 12 13 14 15 |
# File 'lib/ldclient-rb/flags_state.rb', line 11 def initialize(valid) @flag_values = {} @flag_metadata = {} @valid = valid end |
Instance Method Details
#add_flag(flag, value, variation, reason = nil, details_only_if_tracked = false) ⇒ Object
Used internally to build the state map.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ldclient-rb/flags_state.rb', line 19 def add_flag(flag, value, variation, reason = nil, details_only_if_tracked = false) key = flag[:key] @flag_values[key] = value = {} with_details = !details_only_if_tracked || flag[:trackEvents] if !with_details && flag[:debugEventsUntilDate] with_details = flag[:debugEventsUntilDate] > Impl::Util::current_time_millis end if with_details [:version] = flag[:version] [:reason] = reason if !reason.nil? end [:variation] = variation if !variation.nil? [:trackEvents] = true if flag[:trackEvents] [:debugEventsUntilDate] = flag[:debugEventsUntilDate] if flag[:debugEventsUntilDate] @flag_metadata[key] = end |
#as_json ⇒ Object
Returns a hash that can be used as a JSON representation of the entire state map, in the format used by the LaunchDarkly JavaScript SDK. Use this method if you are passing data to the front end in order to “bootstrap” the JavaScript client.
Do not rely on the exact shape of this data, as it may change in future to support the needs of the JavaScript client.
64 65 66 67 68 69 |
# File 'lib/ldclient-rb/flags_state.rb', line 64 def as_json(*) # parameter is unused, but may be passed if we're using the json gem ret = @flag_values.clone ret['$flagsState'] = @flag_metadata ret['$valid'] = @valid ret end |
#flag_value(key) ⇒ Object
Returns the value of an individual feature flag at the time the state was recorded. Returns nil if the flag returned the default value, or if there was no such flag.
45 46 47 |
# File 'lib/ldclient-rb/flags_state.rb', line 45 def flag_value(key) @flag_values[key] end |
#to_json(*a) ⇒ Object
Same as as_json, but converts the JSON structure into a string.
72 73 74 |
# File 'lib/ldclient-rb/flags_state.rb', line 72 def to_json(*a) as_json.to_json(a) end |
#valid? ⇒ Boolean
Returns true if this object contains a valid snapshot of feature flag state, or false if the state could not be computed (for instance, because the client was offline or there was no user).
39 40 41 |
# File 'lib/ldclient-rb/flags_state.rb', line 39 def valid? @valid end |
#values_map ⇒ Object
Returns a map of flag keys to flag values. If a flag would have evaluated to the default value, its value will be nil.
Do not use this method if you are passing data to the front end to “bootstrap” the JavaScript client. Instead, use as_json.
54 55 56 |
# File 'lib/ldclient-rb/flags_state.rb', line 54 def values_map @flag_values end |