Sha256: 575e6937abaacbb228adb2217cdc9242e5210d5301a78bd661f2647d9ac183b8

Contents?: true

Size: 1.72 KB

Versions: 8

Compression:

Stored size: 1.72 KB

Contents

module SplitIoClient
  module Api
    class Splits < Client
      def initialize(api_key, config, metrics)
        @api_key = api_key
        @config = config
        @metrics = metrics
      end

      def since(since)
        start = Time.now
        prefix = 'splitChangeFetcher'
        splits = call_api('/splitChanges', @config, @api_key, {:since => since})

        if splits.status / 100 == 2
          result = splits_with_segment_names(splits.body)

          @metrics.count(prefix + '.status.' + splits.status.to_s, 1)

          @config.logger.debug("#{result[:splits].length} splits retrieved. since=#{since}") if @config.debug_enabled and result[:splits].length > 0
          @config.logger.debug("#{result}") if @config.transport_debug_enabled
        else
          @metrics.count(prefix + '.status.' + splits.status.to_s, 1)

          @config.logger.error('Unexpected result from API call')
        end

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

        result
      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

8 entries across 8 versions & 1 rubygems

Version Path
splitclient-rb-3.1.0.pre.rc6 lib/engine/api/splits.rb
splitclient-rb-3.1.0.pre.rc5 lib/engine/api/splits.rb
splitclient-rb-3.1.0.pre.rc4 lib/engine/api/splits.rb
splitclient-rb-3.1.0.pre.rc2 lib/engine/api/splits.rb
splitclient-rb-3.0.3 lib/engine/api/splits.rb
splitclient-rb-3.0.3.pre.rc2 lib/engine/api/splits.rb
splitclient-rb-3.0.3.pre.rc1 lib/engine/api/splits.rb
splitclient-rb-3.0.2 lib/engine/api/splits.rb