Sha256: ea52e1bf3285932de59ada591aeb084a27db1af083f204158c86124425535727

Contents?: true

Size: 2 KB

Versions: 2

Compression:

Stored size: 2 KB

Contents

module SplitIoClient
  class SplitManager
    #
    # Creates a new split manager instance that connects to split.io API.
    #
    # @param api_key [String] the API key for your split account
    #
    # @return [SplitIoManager] split.io client instance
    def initialize(api_key, config = {}, adapter = nil, splits_repository = nil)
      @localhost_mode_features = []
      @config = config
      @splits_repository = splits_repository
      @adapter = adapter
    end

    #
    # method to get the split list from the client
    #
    # @returns [object] array of splits
    def splits
      return if @splits_repository.nil?

      @splits_repository.splits.each_with_object([]) do |(name, split), memo|
        split_model = Engine::Models::Split.new(split)

        memo << build_split_view(name, split) unless split_model.archived?
      end
    end

    #
    # method to get the list of just split names. Ideal for ietrating and calling client.get_treatment
    #
    # @returns [object] array of split names (String)
    def split_names
      return if @splits_repository.nil?

      @splits_repository.split_names
    end

    #
    # method to get a split view
    #
    # @returns a split view
    def split(split_name)
      if @splits_repository
        split = @splits_repository.get_split(split_name)

        build_split_view(split_name, split) unless split.nil? or split_model(split).archived?
      end
    end

    def build_split_view(name, split)
      return {} unless split

      treatments =
        if split[:conditions] && split[:conditions][0][:partitions]
          split[:conditions][0][:partitions].map { |partition| partition[:treatment] }
        else
          []
        end

        {
          name: name,
          traffic_type_name: split[:trafficTypeName],
          killed: split[:killed],
          treatments: treatments,
          change_number: split[:changeNumber]
        }
    end

    private

    def split_model(split)
      split_model = Engine::Models::Split.new(split)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
splitclient-rb-3.1.3.pre.rc2 lib/splitclient-rb/managers/split_manager.rb
splitclient-rb-3.1.3.pre.rc1 lib/splitclient-rb/managers/split_manager.rb