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

Jko/user rules #8

Merged
merged 3 commits into from
Feb 4, 2015
Merged
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
24 changes: 22 additions & 2 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
require 'logger'

module LaunchDarkly

BUILTINS = [:key, :ip, :country, :email, :firstName, :lastName, :avatar, :name]

#
# A client for the LaunchDarkly API. Client instances are thread-safe. Users
# should create a single client instance for the lifetime of the application.
Expand Down Expand Up @@ -221,7 +224,7 @@ def param_for_user(feature, user)
def match_target?(target, user)
attrib = target[:attribute].to_sym

if attrib == :key or attrib == :ip or attrib == :country
if BUILTINS.include?(attrib)
if user[attrib]
u_value = user[attrib]
return target[:values].include? u_value
Expand All @@ -247,8 +250,19 @@ def match_target?(target, user)

end

def match_user?(variation, user)
if !!variation[:userTarget]
return match_target?(variation[:userTarget], user)
end
return false
end

def match_variation?(variation, user)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the userTarget is a part of the variation, this function should probably be named matchTargets or something, since it a user may match a given variation, but this function would return false.

I guess it doesn't matter that much, since this function is private anyway...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, some of the names are a bit unfortunate, but I want to keep the SDKs as consistent as I can. It makes it easier to port code between them. So I'd rather not have to re-name this everywhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, fair enough.

variation[:targets].each do |target|
if !!variation[:userTarget] and target[:attribute].to_sym == :key
next
end

if match_target?(target, user)
return true
end
Expand All @@ -267,6 +281,12 @@ def evaluate(feature, user)
return nil
end

feature[:variations].each do |variation|
if match_user?(variation, user)
return variation[:value]
end
end

feature[:variations].each do |variation|
if match_variation?(variation, user)
return variation[:value]
Expand All @@ -286,7 +306,7 @@ def evaluate(feature, user)

end

private :add_event, :get_flag_int, :param_for_user, :match_target?, :match_variation?, :evaluate, :create_worker
private :add_event, :get_flag_int, :param_for_user, :match_target?, :match_user?, :match_variation?, :evaluate, :create_worker


end
Expand Down