class SimpleSegment::Operations::Operation

Constants

DEFAULT_CONTEXT

Attributes

context[R]
options[R]
request[R]

Public Class Methods

new(client, options = {}) click to toggle source
# File lib/simple_segment/operations/operation.rb, line 15
def initialize(client, options = {})
  @options = options
  @context = DEFAULT_CONTEXT.merge(options[:context].to_h)
  @request = Request.new(client)
end

Public Instance Methods

call() click to toggle source
# File lib/simple_segment/operations/operation.rb, line 21
def call
  raise 'Must be implemented in a subclass'
end

Private Instance Methods

base_payload() click to toggle source
# File lib/simple_segment/operations/operation.rb, line 29
def base_payload
  check_identity!
  current_time = Time.now

  {
    userId: options[:user_id],
    anonymousId: options[:anonymous_id],
    context: context,
    integrations: options[:integrations],
    timestamp: timestamp(options.fetch(:timestamp, current_time)),
    sentAt: current_time.iso8601,
    messageId: options[:message_id]
  }
end
check_identity!() click to toggle source
# File lib/simple_segment/operations/operation.rb, line 44
def check_identity!
  raise ArgumentError, 'user_id or anonymous_id must be present' \
    unless options[:user_id] || options[:anonymous_id]
end
timestamp(timestamp) click to toggle source
# File lib/simple_segment/operations/operation.rb, line 49
def timestamp(timestamp)
  if timestamp.respond_to?(:iso8601)
    timestamp.iso8601
  else
    Time.iso8601(timestamp).iso8601
  end
end