-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from launchdarkly/eb/ch22308/all-flags-state
add new version of all_flags that captures more metadata
- Loading branch information
Showing
5 changed files
with
274 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'json' | ||
|
||
module LaunchDarkly | ||
# | ||
# A snapshot of the state of all feature flags with regard to a specific user, generated by | ||
# calling the client's all_flags_state method. 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. | ||
# | ||
class FeatureFlagsState | ||
def initialize(valid) | ||
@flag_values = {} | ||
@flag_metadata = {} | ||
@valid = valid | ||
end | ||
|
||
# Used internally to build the state map. | ||
def add_flag(flag, value, variation) | ||
key = flag[:key] | ||
@flag_values[key] = value | ||
meta = { version: flag[:version], trackEvents: flag[:trackEvents] } | ||
meta[:variation] = variation if !variation.nil? | ||
meta[:debugEventsUntilDate] = flag[:debugEventsUntilDate] if flag[:debugEventsUntilDate] | ||
@flag_metadata[key] = meta | ||
end | ||
|
||
# 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). | ||
def valid? | ||
@valid | ||
end | ||
|
||
# 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. | ||
def flag_value(key) | ||
@flag_values[key] | ||
end | ||
|
||
# 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. | ||
def values_map | ||
@flag_values | ||
end | ||
|
||
# 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. | ||
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 | ||
|
||
# Same as as_json, but converts the JSON structure into a string. | ||
def to_json(*a) | ||
as_json.to_json(a) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require "spec_helper" | ||
require "json" | ||
|
||
describe LaunchDarkly::FeatureFlagsState do | ||
subject { LaunchDarkly::FeatureFlagsState } | ||
|
||
it "can get flag value" do | ||
state = subject.new(true) | ||
flag = { key: 'key' } | ||
state.add_flag(flag, 'value', 1) | ||
|
||
expect(state.flag_value('key')).to eq 'value' | ||
end | ||
|
||
it "returns nil for unknown flag" do | ||
state = subject.new(true) | ||
|
||
expect(state.flag_value('key')).to be nil | ||
end | ||
|
||
it "can be converted to values map" do | ||
state = subject.new(true) | ||
flag1 = { key: 'key1' } | ||
flag2 = { key: 'key2' } | ||
state.add_flag(flag1, 'value1', 0) | ||
state.add_flag(flag2, 'value2', 1) | ||
|
||
expect(state.values_map).to eq({ 'key1' => 'value1', 'key2' => 'value2' }) | ||
end | ||
|
||
it "can be converted to JSON structure" do | ||
state = subject.new(true) | ||
flag1 = { key: "key1", version: 100, offVariation: 0, variations: [ 'value1' ], trackEvents: false } | ||
flag2 = { key: "key2", version: 200, offVariation: 1, variations: [ 'x', 'value2' ], trackEvents: true, debugEventsUntilDate: 1000 } | ||
state.add_flag(flag1, 'value1', 0) | ||
state.add_flag(flag2, 'value2', 1) | ||
|
||
result = state.as_json | ||
expect(result).to eq({ | ||
'key1' => 'value1', | ||
'key2' => 'value2', | ||
'$flagsState' => { | ||
'key1' => { | ||
:variation => 0, | ||
:version => 100, | ||
:trackEvents => false | ||
}, | ||
'key2' => { | ||
:variation => 1, | ||
:version => 200, | ||
:trackEvents => true, | ||
:debugEventsUntilDate => 1000 | ||
} | ||
}, | ||
'$valid' => true | ||
}) | ||
end | ||
|
||
it "can be converted to JSON string" do | ||
state = subject.new(true) | ||
flag1 = { key: "key1", version: 100, offVariation: 0, variations: [ 'value1' ], trackEvents: false } | ||
flag2 = { key: "key2", version: 200, offVariation: 1, variations: [ 'x', 'value2' ], trackEvents: true, debugEventsUntilDate: 1000 } | ||
state.add_flag(flag1, 'value1', 0) | ||
state.add_flag(flag2, 'value2', 1) | ||
|
||
object = state.as_json | ||
str = state.to_json | ||
expect(object.to_json).to eq(str) | ||
end | ||
|
||
it "uses our custom serializer with JSON.generate" do | ||
state = subject.new(true) | ||
flag1 = { key: "key1", version: 100, offVariation: 0, variations: [ 'value1' ], trackEvents: false } | ||
flag2 = { key: "key2", version: 200, offVariation: 1, variations: [ 'x', 'value2' ], trackEvents: true, debugEventsUntilDate: 1000 } | ||
state.add_flag(flag1, 'value1', 0) | ||
state.add_flag(flag2, 'value2', 1) | ||
|
||
stringFromToJson = state.to_json | ||
stringFromGenerate = JSON.generate(state) | ||
expect(stringFromGenerate).to eq(stringFromToJson) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters