Sha256: 18baf39a61001e1aaebe0a9d876ff0aa7ac75d77a763225dcb52cdbd59b65e7e

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module SimpleSegment
  module Operations
    class Operation
      DEFAULT_CONTEXT = {
        library: {
          name: 'simple_segment',
          version: SimpleSegment::VERSION
        }
      }.freeze

      attr_reader :options, :request

      def initialize(client, options = {})
        @options = options
        @request = Request.new(client)
      end

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

      private

      def base_payload
        check_identity!
        current_time = Time.now

        {
          userId: options[:user_id],
          anonymousId: options[:anonymous_id],
          context: DEFAULT_CONTEXT.merge(options[:context].to_h),
          integrations: options[:integrations],
          timestamp: timestamp(options.fetch(:timestamp, current_time)),
          sentAt: current_time.iso8601
        }
      end

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

      def timestamp(timestamp)
        if timestamp.respond_to?(:iso8601)
          timestamp.iso8601
        else
          Time.iso8601(timestamp).iso8601
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_segment-0.2.1 lib/simple_segment/operations/operation.rb
simple_segment-0.2.0 lib/simple_segment/operations/operation.rb