Class: SplitIoClient::Api::Impressions

Inherits:
Client
  • Object
show all
Defined in:
lib/splitclient-rb/engine/api/impressions.rb

Constant Summary

Constants inherited from Client

Client::RUBY_ENCODING

Instance Method Summary collapse

Methods inherited from Client

#get_api, #post_api

Constructor Details

#initialize(api_key, config) ⇒ Impressions

Returns a new instance of Impressions.



6
7
8
9
# File 'lib/splitclient-rb/engine/api/impressions.rb', line 6

def initialize(api_key, config)
  @api_key = api_key
  @config = config
end

Instance Method Details

#post(impressions) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/splitclient-rb/engine/api/impressions.rb', line 11

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

#post_count(impressions_count) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/splitclient-rb/engine/api/impressions.rb', line 28

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

#total_impressions(impressions) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/splitclient-rb/engine/api/impressions.rb', line 45

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

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