diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index bc967088..1bb216f0 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,44 @@ 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].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 @@ -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 @@ -157,7 +158,7 @@ def match_variation?(variation, user) end def evaluate(feature, user) - unless feature['on'] + unless feature[:on] return nil end @@ -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