Sha256: f7b6481e2cc1f25da48be84c65a59b15117faec9f26f0f15ff495517eb5b58e4

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

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

      def post
        if @impressions.empty?
          SplitIoClient.configuration.logger.debug('No impressions to report') if SplitIoClient.configuration.debug_enabled
          return
        end

        impressions_by_ip.each do |ip, impressions|
          response = post_api("#{SplitIoClient.configuration.events_uri}/testImpressions/bulk", @api_key, impressions, '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.group_by { |impression| impression[:ip] }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
splitclient-rb-5.1.3.pre.rc3-java lib/splitclient-rb/engine/api/impressions.rb
splitclient-rb-5.1.3.pre.rc3 lib/splitclient-rb/engine/api/impressions.rb