Sha256: 094afcaa43c952b045899161ab9fb5c9a70e89a77ae1b0f58b228e9096897ebb

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module Prefab
  class FeatureFlagClient
    MAX_32_FLOAT = 4294967294.0

    def initialize(base_client)
      @base_client = base_client
    end

    def upsert(feature_obj)
      delta = Prefab::ConfigDelta.new(account_id: @base_client.account_id,
                                      key: feature_config_name(feature_obj.feature),
                                      value: Prefab::ConfigValue.new(feature_flag: feature_obj))
      @base_client.config_client.set(delta)
    end

    def feature_is_on?(feature_name)
      feature_is_on_for?(feature_name, nil)
    end

    def feature_is_on_for?(feature_name, lookup_key, attributes: [])
      @base_client.stats.increment("prefab.featureflag.on", tags: ["feature:#{feature_name}"])

      feature_obj = @base_client.config_client.get(feature_config_name(feature_name))
      if feature_obj.nil?
        return false
      end

      attributes << lookup_key if lookup_key
      if (attributes & feature_obj.whitelisted).size > 0
        return true
      end

      if lookup_key
        return get_user_pct(feature_name, lookup_key) < feature_obj.pct
      end

      return feature_obj.pct > rand()
    end

    private


    def get_user_pct(feature, lookup_key)
      int_value = Murmur3.murmur3_32("#{@account_id}#{feature}#{lookup_key}")
      int_value / MAX_32_FLOAT
    end

    def feature_config_name(feature)
      "Feature.#{feature}"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prefab-cloud-ruby-0.0.9 lib/prefab/feature_flag_client.rb