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 diff --git a/spec/ldclient_spec.rb b/spec/ldclient_spec.rb index 03c82e59..730d3dc2 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: '{"asdf":"qwer"}') + expect(client).to receive(:make_request).with("/api/eval/features").and_return(result) + data = client.send(:all_flags) + expect(data).to eq(asdf: "qwer") 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