Skip to content

Commit

Permalink
Merge pull request #77 from launchdarkly/eb/ch12124/client-side-filter
Browse files Browse the repository at this point in the history
add ability to filter for only client-side flags
  • Loading branch information
eli-darkly authored Aug 21, 2018
2 parents 748b59b + bdac27e commit 50b3aa5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,12 @@ def track(event_name, user, data)

#
# Returns all feature flag values for the given user. This method is deprecated - please use
# all_flags_state instead. Current versions of the client-side SDK will not generate analytics
# {#all_flags_state} instead. Current versions of the client-side SDK will not generate analytics
# events correctly if you pass the result of all_flags.
#
# @param user [Hash] The end user requesting the feature flags
# @return [Hash] a hash of feature flag keys to values
#
def all_flags(user)
all_flags_state(user).values_map
end
Expand All @@ -206,7 +209,13 @@ def all_flags(user)
# including the flag values and also metadata that can be used on the front end. This method does not
# send analytics events back to LaunchDarkly.
#
def all_flags_state(user)
# @param user [Hash] The end user requesting the feature flags
# @param options={} [Hash] Optional parameters to control how the state is generated
# @option options [Boolean] :client_side_only (false) True if only flags marked for use with the
# client-side SDK should be included in the state. By default, all flags are included.
# @return [FeatureFlagsState] a FeatureFlagsState object which can be serialized to JSON
#
def all_flags_state(user, options={})
return FeatureFlagsState.new(false) if @config.offline?

unless user && !user[:key].nil?
Expand All @@ -224,7 +233,11 @@ def all_flags_state(user)
end

state = FeatureFlagsState.new(true)
client_only = options[:client_side_only] || false
features.each do |k, f|
if client_only && !f[:clientSide]
next
end
begin
result = evaluate(f, user, @store, @config.logger)
state.add_flag(f, result[:value], result[:variation])
Expand Down
16 changes: 16 additions & 0 deletions spec/ldclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ def event_processor
})
end

it "can be filtered for only client-side flags" do
flag1 = { key: "server-side-1", offVariation: 0, variations: [ 'a' ], clientSide: false }
flag2 = { key: "server-side-2", offVariation: 0, variations: [ 'b' ], clientSide: false }
flag3 = { key: "client-side-1", offVariation: 0, variations: [ 'value1' ], clientSide: true }
flag4 = { key: "client-side-2", offVariation: 0, variations: [ 'value2' ], clientSide: true }
config.feature_store.init({ LaunchDarkly::FEATURES => {
flag1[:key] => flag1, flag2[:key] => flag2, flag3[:key] => flag3, flag4[:key] => flag4
}})

state = client.all_flags_state({ key: 'userkey' }, client_side_only: true)
expect(state.valid?).to be true

values = state.values_map
expect(values).to eq({ 'client-side-1' => 'value1', 'client-side-2' => 'value2' })
end

it "returns empty state for nil user" do
config.feature_store.init({ LaunchDarkly::FEATURES => { 'key1' => flag1, 'key2' => flag2 } })

Expand Down

0 comments on commit 50b3aa5

Please sign in to comment.