From d95f704fd73d34b23c60d9447db29a050551e3cc Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Wed, 22 Oct 2014 12:09:56 -0700 Subject: [PATCH 1/2] Switch from strings to symbols --- lib/ldclient-rb/ldclient.rb | 42 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index bc967088..953fb34c 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -95,7 +95,7 @@ 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) @@ -103,43 +103,43 @@ def get_flag_int(key, user, default) 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] - 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 @@ -148,7 +148,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 @@ -157,7 +157,7 @@ def match_variation?(variation, user) end def evaluate(feature, user) - unless feature['on'] + unless feature[:on] return nil end @@ -167,18 +167,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 From 884bca99e23366752ade9641cdc81b8760a5c378 Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Wed, 22 Oct 2014 16:40:35 -0700 Subject: [PATCH 2/2] Call to_sym on attribute values --- lib/ldclient-rb/ldclient.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index 953fb34c..1bb216f0 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -114,12 +114,13 @@ def param_for_user(feature, user) end 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 user[attrib]