class SamsaraSDK::Publisher

Physical connector that Publishes messages to Samsara Ingestion API.

Public Instance Methods

post(data) click to toggle source

Sends message to Ingestion API.

@param data [Array<Hash>] List of events. @return [Boolean] Success or failure of HTTP POST call.

# File lib/samsara_sdk/publisher.rb, line 15
def post(data)
  url = URI.parse(Config.get[:url].chomp('/') + Config::API_PATH)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = url.scheme == 'https'
  http.read_timeout = Config.get[:send_timeout_ms] / 1000
  request = Net::HTTP::Post.new(url, headers)
  request.body = prepare data
  http.request(request).instance_of? Net::HTTPAccepted
rescue RuntimeError
  FALSE
end

Private Instance Methods

gzip(data) click to toggle source

Gzip wrapper for data.

@param data [String] Data to wrap. @return [String] Gzipped data.

# File lib/samsara_sdk/publisher.rb, line 42
def gzip(data)
  wio = StringIO.new('w')
  w_gz = Zlib::GzipWriter.new(wio)
  w_gz.write(data)
  w_gz.close
  wio.string
end
headers() click to toggle source

Helper method to generate HTTP request headers for Ingestion API.

@return [Hash] headers

# File lib/samsara_sdk/publisher.rb, line 61
def headers
  {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'Content-Encoding' => Config.get[:compression] == :gzip ? 'gzip' : 'identity',
    Config::PUBLISHED_TIMESTAMP_HEADER => Config.timestamp.to_s
  }
end
none(data) click to toggle source

None wrapper for data.

@param data [String] Data to wrap. @return [String] Original data.

# File lib/samsara_sdk/publisher.rb, line 54
def none(data)
  data
end
prepare(data) click to toggle source

Wraps data in JSON and optionally compresses it.

@param data [Array<Hash>] Data to prepare. @return [String] Prepared data.

# File lib/samsara_sdk/publisher.rb, line 33
def prepare(data)
  data = JSON.generate data
  send(Config.get[:compression], data)
end