Skip to content

Commit

Permalink
Merge pull request #51 from launchdarkly/jko/all-features
Browse files Browse the repository at this point in the history
New methods for fetching all user settings and all feature flags
  • Loading branch information
jkodumal committed Feb 16, 2016
2 parents 10921cc + d8804c8 commit 1ac3051
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
31 changes: 22 additions & 9 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,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?
Expand Down
9 changes: 8 additions & 1 deletion lib/ldclient-rb/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions spec/ldclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,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

Expand Down

0 comments on commit 1ac3051

Please sign in to comment.