Sha256: e14a65be0e39139410fb922839764ca329eb2d0f9af75a24fdbecb0cadda7957

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

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

      def post(impressions)
        if impressions.empty?
          SplitIoClient.configuration.logger.debug('No impressions to report') if SplitIoClient.configuration.debug_enabled
          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

6 entries across 6 versions & 1 rubygems

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