Sha256: 81840f861445dbf1a63709692d8601d578877b96d9644d29ed2c30a0134c195c

Contents?: true

Size: 1.87 KB

Versions: 10

Compression:

Stored size: 1.87 KB

Contents

# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'contrast/components/logger'

module Contrast
  module Utils
    # This is the MetricsHash, which will take data type, so we now what is introduced/included
    # in the TelemetryEvent
    class MetricsHash < Hash
      include Contrast::Components::Logger::InstanceMethods

      attr_reader :data_type

      ERROR_MESSAGES = [
        'The key is not string or does not meet the requirements.',
        'The key extends the allowed length.',
        'The provided value is not the right data type'
      ].cs__freeze
      KEY_REGEXP = /[a-zA-Z0-9_-]{1,63}/.cs__freeze

      def initialize data_type, *several_variants
        super
        @data_type = data_type
      end

      def []= key, value
        key_val = key.dup
        value_val = value.dup
        key_val.strip! if key_val.cs__is_a?(String)
        value_val.strip! if value_val.cs__is_a?(String)
        return unless valid_pair?(key_val, value_val)

        key_val.downcase!
        key_val.strip!
        super(key_val, value_val)
      end

      def valid_pair? key, value
        if !key.cs__is_a?(String) || (KEY_REGEXP =~ key).nil?
          logger.warn('The following key will be omitted', key: key, error: ERROR_MESSAGES[0])
          return false
        end
        unless key.length <= 28
          logger.warn('The following key will be omitted', key: key, error: ERROR_MESSAGES[1])
          return false
        end
        unless value.cs__is_a?(data_type)
          logger.warn('The following key will be omitted', value: value, error: ERROR_MESSAGES[2])
          return false
        end
        return false if value.cs__is_a?(String) && value.empty?
        return false if value.cs__is_a?(String) && value.length > 200

        true
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
contrast-agent-7.2.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.1.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.0.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-6.15.3 lib/contrast/utils/metrics_hash.rb
contrast-agent-6.15.2 lib/contrast/utils/metrics_hash.rb
contrast-agent-6.15.1 lib/contrast/utils/metrics_hash.rb
contrast-agent-6.15.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-6.14.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-6.13.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-6.12.0 lib/contrast/utils/metrics_hash.rb