Sha256: 618985b3b0778ba49b137d61b9e8d250daf758b9d9641e2b72c7f680bc92d8be

Contents?: true

Size: 1.77 KB

Versions: 10

Compression:

Stored size: 1.77 KB

Contents

# typed: false

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

# TODO: sqreen/events

require 'json'
require 'sqreen/log'
require 'sqreen/event'
require 'sqreen/sensitive_data_redactor'

module Sqreen
  # When a request is deeemed worthy of being sent to the backend
  class RequestRecord < Sqreen::Event
    attr_reader :redactor

    # @param [Hash] payload
    # @param [Sqreen::SensitiveDataRedactor] redactor
    def initialize(payload, redactor = nil)
      @redactor = redactor
      super(payload)
    end

    def observed
      (payload && payload[:observed]) || {}
    end

    def last_identify_args
      return nil unless observed[:sdk]

      observed[:sdk].reverse_each do |meth, _time, *args|
        next unless meth == :identify
        return args
      end
      nil
    end

    def processed_sdk_calls
      return [] unless observed[:sdk]
      auth_keys = last_identify_id

      observed[:sdk].map do |meth, time, *args|
        {
          :name => meth,
          :time => time,
          :args => inject_identifiers(args, meth, auth_keys),
        }
      end
    end

    private

    def inject_identifiers(args, meth, auth_keys)
      return args unless meth == :track && auth_keys

      track_opts = args[1] || {}
      if track_opts[:user_identifiers].nil?
        args[1] = track_opts.dup
        args[1][:user_identifiers] = auth_keys
      elsif track_opts[:user_identifiers] != auth_keys
        Sqreen.log.warn 'Sqreen.identify and Sqreen.track have been called ' \
                          'with different user_identifiers values'
      end

      args
    end

    def last_identify_id
      args = last_identify_args
      args.first if args.respond_to? :first
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sqreen-1.22.0-java lib/sqreen/events/request_record.rb
sqreen-1.22.0 lib/sqreen/events/request_record.rb
sqreen-1.21.1-java lib/sqreen/events/request_record.rb
sqreen-1.21.1 lib/sqreen/events/request_record.rb
sqreen-1.21.0-java lib/sqreen/events/request_record.rb
sqreen-1.21.0 lib/sqreen/events/request_record.rb
sqreen-1.20.4-java lib/sqreen/events/request_record.rb
sqreen-1.20.4 lib/sqreen/events/request_record.rb
sqreen-1.21.0.beta3-java lib/sqreen/events/request_record.rb
sqreen-1.21.0.beta3 lib/sqreen/events/request_record.rb