Class: SplitIoClient::Api::Events

Inherits:
Client
  • Object
show all
Defined in:
lib/splitclient-rb/engine/api/events.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, events) ⇒ Events

Returns a new instance of Events.



4
5
6
7
8
# File 'lib/splitclient-rb/engine/api/events.rb', line 4

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

Instance Method Details

#post ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/splitclient-rb/engine/api/events.rb', line 10

def post
  if @events.empty?
    @config.logger.debug('No events to report') if @config.debug_enabled
    return
  end

  @events.each_slice(@config.events_queue_size) do |event_slice|
    result = post_api(
      "#{@config.events_uri}/events/bulk",
      @config,
      @api_key,
      event_slice.map { |event| formatted_event(event[:e]) },
      'SplitSDKMachineIP' => event_slice[0][:m][:i],
      'SplitSDKMachineName' => event_slice[0][:m][:n],
      'SplitSDKVersion' => event_slice[0][:m][:s]
    )

    if (200..299).include? result.status
      @config.logger.debug("Events reported: #{event_slice.size}") if @config.debug_enabled
    else
      @config.logger.error("Unexpected status code while posting events: #{result.status}")
    end
  end
end