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) ⇒ Events

Returns a new instance of Events.



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

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#post(events) ⇒ 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
34
# File 'lib/splitclient-rb/engine/api/events.rb', line 10

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

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

    if response.success?
      SplitLogger.log_if_debug("Events reported: #{events_slice.size}")
    else
      SplitLogger.log_error("Unexpected status code while posting events: #{response.status}." \
      ' - Check your API key and base URI')
      raise 'Split SDK failed to connect to backend to post events'
    end
  end
end