Sha256: 5eddabeefc334de8c6ec2b466b05d89e6a2d60d41b7e3c6e1ce6b16087a2fb8b

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module SimpleSegment
  module Operations
    class Operation
      include SimpleSegment::Utils

      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

1 entries across 1 versions & 1 rubygems

Version Path
simple_segment-0.3.0 lib/simple_segment/operations/operation.rb