Sha256: 7d5e933a23f188e8e94e796522a078f0fd6913561e069818c01d7730b2572622

Contents?: true

Size: 1.3 KB

Versions: 104

Compression:

Stored size: 1.3 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

        impressions_by_ip(impressions).each do |ip, impressions_ip|
          response = post_api("#{@config.events_uri}/testImpressions/bulk", @api_key, impressions_ip)

          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
      end

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

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

      private

      def impressions_by_ip(impressions)
        impressions.group_by { |impression| impression[:ip] }
      end
    end
  end
end

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
splitclient-rb-7.1.4.pre.rc7-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc7 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc6-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc6 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc5-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc5 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc4-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc4 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc3-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc3 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc2-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc2 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc1-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.4.pre.rc1 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.3-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.3 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.3.pre.rc1-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.3.pre.rc1 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.2-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-7.1.2 lib/splitclient-rb/engine/api/impressions.rb