Sha256: 5c6c4af5f5ef8f00ca09552a88aea80d8f2b45d234a8978f9ab2f45d02bc9769

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module RudderAnalyticsSync
  class Batch
    include RudderAnalyticsSync::Utils

    attr_reader :client, :payload

    def self.deserialize(client, payload)
      new(client, symbolize_keys(payload))
    end

    def initialize(client, payload = { batch: [] })
      @client = client
      @payload = payload
    end

    def identify(options)
      add(Operations::Identify, options, __method__)
    end

    def track(options)
      add(Operations::Track, options, __method__)
    end

    def page(options)
      add(Operations::Page, options, __method__)
    end

    def screen(options)
      add(Operations::Screen, options, __method__)
    end

    def group(options)
      add(Operations::Group, options, __method__)
    end

    def alias(options)
      add(Operations::Alias, options, __method__)
    end

    def context=(context)
      payload[:context] = context
    end

    def integrations=(integrations)
      payload[:integrations] = integrations
    end

    def serialize
      payload
    end

    def commit
      if payload[:batch].length.zero?
        raise ArgumentError, 'A batch must contain at least one action'
      end

      Request.new(client).post('/v1/batch', payload)
    end

    private

    def add(operation_class, options, action)
      operation = operation_class.new(client, symbolize_keys(options))
      operation_payload = operation.build_payload
      operation_payload[:context] = operation_payload[:context].merge(payload[:context] || {})
      operation_payload[:integrations] = payload[:integrations] || operation_payload[:integrations]
      operation_payload[:type] = action
      payload[:batch] << operation_payload
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rudder_analytics_sync-1.0.7 lib/rudder_analytics_sync/batch.rb
rudder_analytics_sync-1.0.6 lib/rudder_analytics_sync/batch.rb
rudder_analytics_sync-1.0.5 lib/rudder_analytics_sync/batch.rb
rudder_analytics_sync-1.0.4 lib/rudder_analytics_sync/batch.rb
rudder_analytics_sync-1.0.3 lib/rudder_analytics_sync/batch.rb