class SamsaraSDK::Client

A client for ingesting events into Samsara. It is the main interface to communicate with Samsara API.

Public Class Methods

new(config) click to toggle source

initialize

@param config [Hash] Configuration overrides. @raise [SamsaraSDK::ConfigValidationError] if any config option is incorrect.

# File lib/samsara_sdk/client.rb, line 15
def initialize(config)
  Config.setup! config
  @publisher = Publisher.new
  @queue = RingBuffer.new(Config.get[:max_buffer_size])
  @activity = Thread.new { publishing_activity } if Config.get[:start_publishing_thread]
end

Public Instance Methods

publish_events(events) click to toggle source

Publishes given events list to Ingestion API immediately.

@param events [Array<Hash>] List of events. @return [Boolean] The result of publish operation. @raise [SamsaraSDK::EventValidationError] if any option of the given event is invalid. @see samsara-analytics.io/docs/design/events-spec Event specification

# File lib/samsara_sdk/client.rb, line 28
def publish_events(events)
  events = events.map { |event| Event.validate(Event.enrich(event)) }
  @publisher.post events
end
record_event(event) click to toggle source

Pushes event to internal events' queue.

@param event [Hash] Event data. @option data [String] :eventName Name of the event. @option data [String] :sourceId Source ID of the event. @option data [Integer] :timestamp Timestamp in milliseconds. @see samsara-analytics.io/docs/design/events-spec Event specification

# File lib/samsara_sdk/client.rb, line 40
def record_event(event)
  event = Event.validate(Event.enrich(event))
  @queue << event
end

Private Instance Methods

publishing_activity() click to toggle source

Publishing activity. Represents an infinite loop that periodically posts queued events to Ingestion API. Used in a background thread.

# File lib/samsara_sdk/client.rb, line 50
def publishing_activity
  loop do
    @queue.flush { |data| @publisher.post data } if @queue.count >= Config.get[:min_buffer_size]
    sleep Config.get[:publish_interval_ms] / 1000
  end
end