From 94b8307846ab5691034d0b1489fdc3063671cb80 Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Mon, 25 Jan 2016 17:14:14 -0800 Subject: [PATCH 1/3] New methods for fetching all user settings and all feature flags --- lib/ldclient-rb/ldclient.rb | 31 ++++++++++++++++++++++--------- lib/ldclient-rb/stream.rb | 9 ++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index 23159906..bd1be34b 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -203,25 +203,38 @@ def track(event_name, user, data) end # - # Returns the key of every feature + # Returns the key of every feature flag # - def feature_keys - get_features.map { |feature| feature[:key] } + def all_keys + all_flags.keys end # - # Returns all features + # Returns all feature flags # - def get_features - res = make_request "/api/features" + def all_flags + if @config.stream? && !@stream_processor.started? + @stream_processor.start + end - if res.status / 100 == 2 - return JSON.parse(res.body, symbolize_names: true)[:items] + if @config.stream? && @stream_processor.initialized? + @stream_processor.get_all_features else - @config.logger.error("[LDClient] Unexpected status code #{res.status}") + res = make_request "/api/eval/features" + + if res.status / 100 == 2 + JSON.parse(res.body, symbolize_names: true) + else + @config.logger.error("[LDClient] Unexpected status code #{res.status}") + Hash.new + end end end + def get_user_settings(user) + Hash[all_flags.map { |key, feature| [key, evaluate(feature, user)]}] + end + def get_streamed_flag(key) feature = get_flag_stream(key) if @config.debug_stream? diff --git a/lib/ldclient-rb/stream.rb b/lib/ldclient-rb/stream.rb index 07070f9f..4e508990 100644 --- a/lib/ldclient-rb/stream.rb +++ b/lib/ldclient-rb/stream.rb @@ -80,6 +80,13 @@ def started? @started.value end + def get_all_features + if not initialized? + throw :uninitialized + end + @store.all + end + def get_feature(key) if not initialized? throw :uninitialized @@ -125,7 +132,7 @@ def boot_event_manager source.on(PATCH) { |message| process_message(message, PATCH) } source.on(DELETE) { |message| process_message(message, DELETE) } source.error do |error| - @config.logger.info("[LDClient] Error subscribing to stream API: #{error}") + @config.logger.info("[LDClient] Stream connection: #{error}") set_disconnected end source.inactivity_timeout = 0 From 39cf927067d67a9ca3b6e6c2bee949b35eda82c9 Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Mon, 25 Jan 2016 18:03:54 -0800 Subject: [PATCH 2/3] Update tests --- spec/ldclient_spec.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/ldclient_spec.rb b/spec/ldclient_spec.rb index 03c82e59..52c7cd8e 100644 --- a/spec/ldclient_spec.rb +++ b/spec/ldclient_spec.rb @@ -101,18 +101,18 @@ end end - describe '#get_features' do + describe '#all_flags' do it "will parse and return the features list" do - result = double("Faraday::Response", status: 200, body: '{"items": ["asdf"]}') - expect(client).to receive(:make_request).with("/api/features").and_return(result) - data = client.send(:get_features) - expect(data).to eq ["asdf"] + result = double("Faraday::Response", status: 200, body: '{"foo": {"key": "foo"}}') + expect(client).to receive(:make_request).with("/api/eval/features").and_return(result) + data = client.send(:all_flags) + expect(data).to eq {foo : {key: "foo"}} end it "will log errors" do result = double("Faraday::Response", status: 418) - expect(client).to receive(:make_request).with("/api/features").and_return(result) + expect(client).to receive(:make_request).with("/api/eval/features").and_return(result) expect(client.instance_variable_get(:@config).logger).to receive(:error) - client.send(:get_features) + client.send(:all_flags) end end From d8804c8277eb06776910342c6450ae22829382df Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Tue, 26 Jan 2016 09:22:42 -0800 Subject: [PATCH 3/3] Update all flags test --- spec/ldclient_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/ldclient_spec.rb b/spec/ldclient_spec.rb index 52c7cd8e..730d3dc2 100644 --- a/spec/ldclient_spec.rb +++ b/spec/ldclient_spec.rb @@ -103,10 +103,10 @@ describe '#all_flags' do it "will parse and return the features list" do - result = double("Faraday::Response", status: 200, body: '{"foo": {"key": "foo"}}') + result = double("Faraday::Response", status: 200, body: '{"asdf":"qwer"}') expect(client).to receive(:make_request).with("/api/eval/features").and_return(result) data = client.send(:all_flags) - expect(data).to eq {foo : {key: "foo"}} + expect(data).to eq(asdf: "qwer") end it "will log errors" do result = double("Faraday::Response", status: 418)