Sha256: 65bf174c5d1a06611329896ff0867baef7a04b642ee4da4c0abe82e625c7a10b

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module SimpleSegment
  class Batch
    include SimpleSegment::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 group(options)
      add(Operations::Group, 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/import', payload)
    end

    private

    def add(operation_class, options, action)
      operation = operation_class.new(client, symbolize_keys(options))
      operation_payload = operation.build_payload
      operation_payload[:action] = action
      payload[:batch] << operation_payload
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_segment-0.3.0 lib/simple_segment/batch.rb
simple_segment-0.2.1 lib/simple_segment/batch.rb
simple_segment-0.2.0 lib/simple_segment/batch.rb