Skip to content

Symbols #2

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

Merged
merged 2 commits into from
Oct 23, 2014
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
43 changes: 22 additions & 21 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,51 +95,52 @@ def get_flag_int(key, user, default)
end


feature = JSON.parse(res.body)
feature = JSON.parse(res.body, :symbolize_names => true)

val = evaluate(feature, user)

val == nil ? default : val
end

def param_for_user(feature, user)
if user.has_key? 'key'
id_hash = user['key']
if user.has_key? :key
id_hash = user[:key]
else
return nil
end

if user.has_key? 'secondary'
id_hash += '.' + user['secondary']
if user.has_key? :secondary
id_hash += '.' + user[:secondary]
end

hash_key = "%s.%s.%s" % [feature['key'], feature['salt'], id_hash]
hash_key = "%s.%s.%s" % [feature[:key], feature[:salt], id_hash]

hash_val = (Digest::SHA1.hexdigest(hash_key))[0..14]
return hash_val.to_i(16) / LONG_SCALE
end

def match_target?(target, user)
attrib = target['attribute']
attrib = target[:attribute].to_sym

if attrib == 'key' or attrib == 'ip' or attrib == 'country'
if attrib == :key or attrib == :ip or attrib == :country
if user[attrib]
u_value = user[attrib]
return target['values'].include? u_value
return target[:values].include? u_value
else
return false
end
else # custom attribute
unless user.has_key? 'custom'
unless user.has_key? :custom
return false
end
unless user['custom'].include? attrib
unless user[:custom].include? attrib
return false
end
u_value = user['custom'][attrib]
u_value = user[:custom][attrib]
if u_value.is_a? String or u_value.is_a? Numeric
return target['values'].include? u_value
return target[:values].include? u_value
elsif u_value.is_a? Array
return ! ((target['values'] & u_value).empty?)
return ! ((target[:values] & u_value).empty?)
end

return false
Expand All @@ -148,7 +149,7 @@ def match_target?(target, user)
end

def match_variation?(variation, user)
variation['targets'].each do |target|
variation[:targets].each do |target|
if match_target?(target, user)
return true
end
Expand All @@ -157,7 +158,7 @@ def match_variation?(variation, user)
end

def evaluate(feature, user)
unless feature['on']
unless feature[:on]
return nil
end

Expand All @@ -167,18 +168,18 @@ def evaluate(feature, user)
return nil
end

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

total = 0.0
feature['variations'].each do |variation|
total += variation['weight'].to_f / 100.0
feature[:variations].each do |variation|
total += variation[:weight].to_f / 100.0

if param < total
return variation['value']
return variation[:value]
end
end

Expand Down