Sha256: 82d097b8f4a85717977302aa505a1e4dfc69dd3e2335830377487ae723999390

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require 'rudder_analytics_sync/utils'

module RudderAnalyticsSync
  module Operations
    class Operation
      include RudderAnalyticsSync::Utils

      DEFAULT_CONTEXT = {
        library: {
          name: 'rudder-sdk-ruby-sync',
          version: RudderAnalyticsSync::VERSION
        },
        traits: {

        }
      }.freeze

      def initialize(client, options = {})
        @options = options
        @context = DEFAULT_CONTEXT.merge(options[:context].to_h)
        @request = Request.new(client)
      end

      def call
        raise 'Must be implemented in a subclass'
      end

      private

      attr_reader :options, :request, :context

      def base_payload
        check_identity!
        current_time = Time.now.utc

        payload = {
          context: context,
          integrations: options[:integrations] || { All: true },
          timestamp: maybe_datetime_in_iso8601(options[:timestamp] || Time.now.utc),
          sentAt: maybe_datetime_in_iso8601(current_time),
          messageId: uid(),
          properties: options[:properties] || {}
        }

        # add the userId if present
        if (options[:user_id])
            payload = payload.merge({userId: options[:user_id]})
        end

        # add the anonymousId if present
        if (options[:anonymous_id])
            payload = payload.merge({anonymousId: options[:anonymous_id]})
        end
        payload
      end

      def check_identity!
        raise ArgumentError, 'user_id or anonymous_id must be present' \
          unless options[:user_id] || options[:anonymous_id]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rudder_analytics_sync-1.0.7 lib/rudder_analytics_sync/operations/operation.rb