Sha256: 8d6b0960520f051aca62791b6fca42c74aa461002f232db06ce70b926e6ca10f

Contents?: true

Size: 1.87 KB

Versions: 8

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

8 entries across 8 versions & 1 rubygems

Version Path
contrast-agent-7.6.1 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.6.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.5.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.4.1 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.4.0 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.3.2 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.3.1 lib/contrast/utils/metrics_hash.rb
contrast-agent-7.3.0 lib/contrast/utils/metrics_hash.rb