Skip to content

Commit

Permalink
Merge pull request #8 from launchdarkly/jko/user-rules
Browse files Browse the repository at this point in the history
Jko/user rules
  • Loading branch information
jkodumal committed Feb 4, 2015
2 parents c7a1542 + d94b120 commit ea57c00
Showing 1 changed file with 22 additions and 2 deletions.
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)
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

0 comments on commit ea57c00

Please sign in to comment.