Sha256: e50856a1d3d96f099fa81642a6a6c8e9d4a8d329cf2cb44474b897b4aa458c3b

Contents?: true

Size: 1.3 KB

Versions: 28

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

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

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

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

          if response.success?
            SplitLogger.log_if_debug("Impressions reported: #{total_impressions(impressions)}")
          else
            SplitLogger.log_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

28 entries across 28 versions & 1 rubygems

Version Path
splitclient-rb-6.2.0.pre.rc1-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-6.2.0.pre.rc1 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-6.1.0-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-6.1.0 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-6.1.0.pre.rc2-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-6.1.0.pre.rc2 lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-6.1.0.pre.rc1-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-6.1.0.pre.rc1 lib/splitclient-rb/engine/api/impressions.rb