Sha256: 17e46210a7deed867427458d7a948e7ada68e96a2f9d902c77e08a8a52a9c492

Contents?: true

Size: 1.93 KB

Versions: 42

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

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

      def post(impressions)
        if impressions.empty?
          @config.split_logger.log_if_debug('No impressions to report')
          return
        end

        response = post_api("#{@config.events_uri}/testImpressions/bulk", @api_key, impressions, impressions_headers)

        if response.success?
          @config.split_logger.log_if_debug("Impressions reported: #{total_impressions(impressions)}")
        else
          @config.logger.error("Unexpected status code while posting impressions: #{response.status}." \
          ' - Check your API key and base URI')
          raise 'Split SDK failed to connect to backend to post impressions'
        end
      end

      def post_count(impressions_count)
        if impressions_count.nil? || impressions_count[:pf].empty?
          @config.split_logger.log_if_debug('No impressions count to send')
          return
        end

        response = post_api("#{@config.events_uri}/testImpressions/count", @api_key, impressions_count)

        if response.success?
          @config.split_logger.log_if_debug("Impressions count sent: #{impressions_count[:pf].length}")
        else
          @config.logger.error("Unexpected status code while posting impressions count: #{response.status}." \
          ' - Check your API key and base URI')
          raise 'Split SDK failed to connect to backend to post impressions'
        end
      end

      def total_impressions(impressions)
        return 0 if impressions.nil?

        impressions.reduce(0) do |impressions_count, impression|
          impressions_count += impression[:i].length
        end
      end

      private

      def impressions_headers
        {
          'SplitSDKImpressionsMode' => @config.impressions_mode.to_s
        }
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
splitclient-rb-7.1.4.pre.rc18-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc18 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc17-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc17 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc16-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc16 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc15-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc15 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc14-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc14 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc13-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc13 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc12-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc12 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc11-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc11 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc10-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc10 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc9-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc9 lib/splitclient-rb/engine/api/impressions.rb