Skip to content

Commit 1ac3051

Browse files
committed
Merge pull request #51 from launchdarkly/jko/all-features
New methods for fetching all user settings and all feature flags
2 parents 10921cc + d8804c8 commit 1ac3051

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

lib/ldclient-rb/ldclient.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,38 @@ def track(event_name, user, data)
204204
end
205205

206206
#
207-
# Returns the key of every feature
207+
# Returns the key of every feature flag
208208
#
209-
def feature_keys
210-
get_features.map { |feature| feature[:key] }
209+
def all_keys
210+
all_flags.keys
211211
end
212212

213213
#
214-
# Returns all features
214+
# Returns all feature flags
215215
#
216-
def get_features
217-
res = make_request "/api/features"
216+
def all_flags
217+
if @config.stream? && !@stream_processor.started?
218+
@stream_processor.start
219+
end
218220

219-
if res.status / 100 == 2
220-
return JSON.parse(res.body, symbolize_names: true)[:items]
221+
if @config.stream? && @stream_processor.initialized?
222+
@stream_processor.get_all_features
221223
else
222-
@config.logger.error("[LDClient] Unexpected status code #{res.status}")
224+
res = make_request "/api/eval/features"
225+
226+
if res.status / 100 == 2
227+
JSON.parse(res.body, symbolize_names: true)
228+
else
229+
@config.logger.error("[LDClient] Unexpected status code #{res.status}")
230+
Hash.new
231+
end
223232
end
224233
end
225234

235+
def get_user_settings(user)
236+
Hash[all_flags.map { |key, feature| [key, evaluate(feature, user)]}]
237+
end
238+
226239
def get_streamed_flag(key)
227240
feature = get_flag_stream(key)
228241
if @config.debug_stream?

lib/ldclient-rb/stream.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ def started?
8080
@started.value
8181
end
8282

83+
def get_all_features
84+
if not initialized?
85+
throw :uninitialized
86+
end
87+
@store.all
88+
end
89+
8390
def get_feature(key)
8491
if not initialized?
8592
throw :uninitialized
@@ -125,7 +132,7 @@ def boot_event_manager
125132
source.on(PATCH) { |message| process_message(message, PATCH) }
126133
source.on(DELETE) { |message| process_message(message, DELETE) }
127134
source.error do |error|
128-
@config.logger.info("[LDClient] Error subscribing to stream API: #{error}")
135+
@config.logger.info("[LDClient] Stream connection: #{error}")
129136
set_disconnected
130137
end
131138
source.inactivity_timeout = 0

spec/ldclient_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,18 @@
120120
end
121121
end
122122

123-
describe '#get_features' do
123+
describe '#all_flags' do
124124
it "will parse and return the features list" do
125-
result = double("Faraday::Response", status: 200, body: '{"items": ["asdf"]}')
126-
expect(client).to receive(:make_request).with("/api/features").and_return(result)
127-
data = client.send(:get_features)
128-
expect(data).to eq ["asdf"]
125+
result = double("Faraday::Response", status: 200, body: '{"asdf":"qwer"}')
126+
expect(client).to receive(:make_request).with("/api/eval/features").and_return(result)
127+
data = client.send(:all_flags)
128+
expect(data).to eq(asdf: "qwer")
129129
end
130130
it "will log errors" do
131131
result = double("Faraday::Response", status: 418)
132-
expect(client).to receive(:make_request).with("/api/features").and_return(result)
132+
expect(client).to receive(:make_request).with("/api/eval/features").and_return(result)
133133
expect(client.instance_variable_get(:@config).logger).to receive(:error)
134-
client.send(:get_features)
134+
client.send(:all_flags)
135135
end
136136
end
137137

0 commit comments

Comments
 (0)