Sha256: d2968a04b603d11439f30dd6abc26af6381bc5a1035765518152fa606790a766

Contents?: true

Size: 1.97 KB

Versions: 28

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module SplitIoClient
  module Api
    # Retrieves split definitions from the Split Backend
    class Splits < Client
      METRICS_PREFIX = 'splitChangeFetcher'

      def initialize(api_key, metrics)
        @api_key = api_key
        @metrics = metrics
      end

      def since(since)
        start = Time.now

        response = get_api("#{SplitIoClient.configuration.base_uri}/splitChanges", @api_key, since: since)

        if response.success?
          result = splits_with_segment_names(response.body)

          @metrics.count(METRICS_PREFIX + '.status.' + response.status.to_s, 1)
          unless result[:splits].empty?
            SplitLogger.log_if_debug("#{result[:splits].length} splits retrieved. since=#{since}")
          end
          SplitLogger.log_if_transport(result.to_s)

          latency = (Time.now - start) * 1000.0
          @metrics.time(METRICS_PREFIX + '.time', latency)

          result
        else
          @metrics.count(METRICS_PREFIX + '.status.' + response.status.to_s, 1)
          SplitLogger.log_error("Unexpected status code while fetching splits: #{response.status}. " \
          'Check your API key and base URI')
          raise 'Split SDK failed to connect to backend to fetch split definitions'
        end
      end

      private

      def splits_with_segment_names(splits_json)
        parsed_splits = JSON.parse(splits_json, symbolize_names: true)

        parsed_splits[:segment_names] =
          parsed_splits[:splits].each_with_object(Set.new) do |split, splits|
            splits << segment_names(split)
          end.flatten

        parsed_splits
      end

      def segment_names(split)
        split[:conditions].each_with_object(Set.new) do |condition, names|
          condition[:matcherGroup][:matchers].each do |matcher|
            next if matcher[:userDefinedSegmentMatcherData].nil?

            names << matcher[:userDefinedSegmentMatcherData][:segmentName]
          end
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
splitclient-rb-6.4.1-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.1 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.1.pre.rc3-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.1.pre.rc3 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.1.pre.rc2-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.1.pre.rc2 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.1.pre.rc1-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.1.pre.rc1 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.0-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.0 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.0.pre.rc1-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.4.0.pre.rc1 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.3.0-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.3.0 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.3.0.pre.rc1-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.3.0.pre.rc1 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.2.0-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.2.0 lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.2.0.pre.rc2-java lib/splitclient-rb/engine/api/splits.rb
splitclient-rb-6.2.0.pre.rc2 lib/splitclient-rb/engine/api/splits.rb