Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ldclient-rb.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "ldclient-rb/version"
require "ldclient-rb/settings"
require "ldclient-rb/ldclient"
require "ldclient-rb/store"
require "ldclient-rb/config"
Expand Down
1 change: 1 addition & 0 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module LaunchDarkly
#
#
class LDClient
include Settings
#
# Creates a new client instance that connects to LaunchDarkly. A custom
# configuration parameter can also supplied to specify advanced options,
Expand Down
40 changes: 40 additions & 0 deletions lib/ldclient-rb/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module LaunchDarkly

#
# Module to manage user flag settings
#
module Settings
#
# Specifically enable or disable a feature flag for a user based
# on their key.
#
# @param user_key [String] the key of the user
# @param flag_key [String] the unique feature key for the feature flag, as shown
# on the LaunchDarkly dashboard
# @param setting [Boolean] the new setting, one of:
# true: the feature is always on
# false: the feature is never on
# nil: remove the setting (assign user per defined rules)
def update_user_flag_setting(user_key, flag_key, setting=nil)
unless user_key
@config.logger.error("[LDClient] Must specify user")
return
end

res = log_timings('update_user_flag_setting') do
@client.put("#{@config.base_uri}/api/users/#{user_key}/features/#{flag_key}") do |req|
req.headers['Authorization'] = "api_key #{@api_key}"
req.headers['User-Agent'] = "RubyClient/#{LaunchDarkly::VERSION}"
req.headers['Content-Type'] = 'application/json'
req.body = {setting: setting}.to_json
req.options.timeout = @config.read_timeout
req.options.open_timeout = @config.connect_timeout
end
end

unless res.success?
@config.logger.error("[LDClient] Failed to change setting, status: #{res.status}")
end
end
end
end
15 changes: 15 additions & 0 deletions spec/ldclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
JSON.parse(data, symbolize_names: true)
end

context 'user flag settings' do
describe '#update_user_flag_setting' do
it 'requires user' do
expect(client.instance_variable_get(:@config).logger).to receive(:error)
client.update_user_flag_setting(nil, feature[:key], true)
end

it 'puts the new setting' do
result = double('result', success?: true, status: 204)
expect(client.instance_variable_get(:@client)).to receive(:put).and_return(result)
client.update_user_flag_setting(user[:key], feature[:key], true)
end
end
end

describe '#flush' do
it "will flush and post all events" do
client.instance_variable_get(:@queue).push "asdf"
Expand Down