From 8fc5f8f09a044ef4f1656187332cb948fe89e1be Mon Sep 17 00:00:00 2001 From: Steven Lumos Date: Tue, 23 Jun 2015 14:56:42 -0700 Subject: [PATCH 1/6] Add methods for interacting with user overrides --- lib/ldclient-rb/ldclient.rb | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index aa8aae66..f593deee 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -208,6 +208,40 @@ def get_features end end + def get_user_toggles(user) + res = @client.get("#{@config.base_uri}/api/users/#{user[:key]}/features") do |req| + req.headers['Authorization'] = "api_key #{@api_key}" + req.headers['User-Agent'] = "RubyClient/#{LaunchDarkly::VERSION}" + req.options.timeout = @config.read_timeout + req.options.open_timeout = @config.connect_timeout + end + + if res.status == 200 then + return JSON.parse(res.body, symbolize_names: true) + else + @config.logger.error("[LDClient] Unexpected status code #{res.status}") + end + end + + def add_user_override(key, user, value) + res = @client.put("#{@config.base_uri}/api/users/#{user[:key]}/features/#{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: value}.to_json + req.options.timeout = @config.read_timeout + req.options.open_timeout = @config.connect_timeout + end + + if res.status == 401 + @config.logger.error("[LDClient] Invalid API key") + end + + if res.status != 200 + @config.logger.error("[LDClient] Unexpected status code #{res.status}") + end + end + def get_flag_int(key, user, default) unless user @@ -372,4 +406,4 @@ def log_timings(label, &block) private :add_event, :get_flag_int, :param_for_user, :match_target?, :match_user?, :match_variation?, :evaluate, :create_worker, :log_timings end -end \ No newline at end of file +end From 37b636168e8917fe3d029fad8a0989630cfca9c7 Mon Sep 17 00:00:00 2001 From: Steven Lumos Date: Tue, 30 Jun 2015 12:50:02 -0700 Subject: [PATCH 2/6] Add explicit return value --- lib/ldclient-rb/ldclient.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index f593deee..e3f74b23 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -239,7 +239,10 @@ def add_user_override(key, user, value) if res.status != 200 @config.logger.error("[LDClient] Unexpected status code #{res.status}") + return false end + + return true end def get_flag_int(key, user, default) From 6bceb1893a09dcdb36d81d912e6d942b19e6b334 Mon Sep 17 00:00:00 2001 From: Nan Ma Date: Wed, 6 Jan 2016 12:02:08 -0800 Subject: [PATCH 3/6] Return empty toggles if request fails. Add aggressive logging for debugging --- lib/ldclient-rb/ldclient.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index 243a3844..d5bd95c2 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -223,17 +223,13 @@ def get_features end def get_user_toggles(user) - res = @client.get("#{@config.base_uri}/api/users/#{user[:key]}/features") do |req| - req.headers['Authorization'] = "api_key #{@api_key}" - req.headers['User-Agent'] = "RubyClient/#{LaunchDarkly::VERSION}" - req.options.timeout = @config.read_timeout - req.options.open_timeout = @config.connect_timeout - end + res = make_request "#{@config.base_uri}/api/users/#{user[:key]}/features" - if res.status == 200 then + if res.status / 100 == 2 return JSON.parse(res.body, symbolize_names: true) else - @config.logger.error("[LDClient] Unexpected status code #{res.status}") + @config.logger.error("[LDClient] Unexpected status code #{res.status}. Response body #{res.body}") + return { :items => [] } end end From c0885ad0e66dcf117fac61ff29c6be9d06d18718 Mon Sep 17 00:00:00 2001 From: Nan Ma Date: Wed, 6 Jan 2016 12:26:04 -0800 Subject: [PATCH 4/6] Removed duplicated base_url --- lib/ldclient-rb/ldclient.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index d5bd95c2..7dcebf68 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -223,7 +223,7 @@ def get_features end def get_user_toggles(user) - res = make_request "#{@config.base_uri}/api/users/#{user[:key]}/features" + res = make_request "api/users/#{user[:key]}/features" if res.status / 100 == 2 return JSON.parse(res.body, symbolize_names: true) From 179aa3ac7e21413dc25142c5a2194fa50d519f57 Mon Sep 17 00:00:00 2001 From: Nan Ma Date: Wed, 6 Jan 2016 12:37:54 -0800 Subject: [PATCH 5/6] Fix delimiter --- lib/ldclient-rb/ldclient.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index 7dcebf68..6e256834 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -223,7 +223,7 @@ def get_features end def get_user_toggles(user) - res = make_request "api/users/#{user[:key]}/features" + res = make_request "/api/users/#{user[:key]}/features" if res.status / 100 == 2 return JSON.parse(res.body, symbolize_names: true) From 9171158a145ce1679a08fb37e17cc01fe1675ac1 Mon Sep 17 00:00:00 2001 From: Nan Ma Date: Wed, 6 Jan 2016 13:45:15 -0800 Subject: [PATCH 6/6] Treat 204 as expected in add_user_override --- lib/ldclient-rb/ldclient.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index 6e256834..ac848b94 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -247,7 +247,7 @@ def add_user_override(key, user, value) @config.logger.error("[LDClient] Invalid API key") end - if res.status != 200 + if res.status / 100 != 2 @config.logger.error("[LDClient] Unexpected status code #{res.status}") return false end