Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New methods for fetching all user settings and all feature flags #51

Merged
merged 3 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)]}]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should send feature events here.

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 @@ -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

Expand Down