Module: Contrast::Agent::Reporting::Settings::Helpers

Defined in:
lib/contrast/agent/reporting/settings/helpers.rb

Overview

Helper methods used across the settings.

Constant Summary collapse

NO_MORE_UNDERSCORE_EXCLUSIONS =

Known exclusion, we need different result, or just memo the output, saving computing time. Reason to exist: different endpoints require different attributes.

{
    protect_rules: :rules,
    assess_rules: :assessmentRules,
    match_strategy: :matchStrategy
}.cs__freeze

Class Method Summary collapse

Class Method Details

.array_to_iv(instance_type, instance_variable, array, ng_endpoint: false) ⇒ Object

Fills instance variable [Array] with new settings as new instance type for each entry.

return an array to fill. fill the iv with the new type containing data from the provided array param.

Parameters:

  • instance_type (Class)

    a class to create new instance from to

  • instance_variable (Array)

    instance variable accessor method

  • array (Array)

    array to be used to fill the iv.

  • ng_endpoint (Boolean) (defaults to: false)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/contrast/agent/reporting/settings/helpers.rb', line 31

def array_to_iv instance_type, instance_variable, array, ng_endpoint: false
  return unless array.is_a?(Array)

  # If we have a ng endpoint we have passed a boolean flag as param to the setter for the
  # array, and we need to retrieve the original array.
  payload = if ng_endpoint
              array[0]
            else
              array
            end

  payload.each_with_index do |entry, index|
    new_instance = instance_type.new
    instance_type::ATTRIBUTES.each do |attr|
      key = ng_attribute_filter(attr, ng_endpoint: ng_endpoint)
      new_instance.send("#{ attr }=".to_sym, entry[key])
    end
    instance_variable[index] = new_instance
  end

  instance_variable
end

.ng_attribute_filter(attr_name, ng_endpoint: false) ⇒ Symbol?

Ng endpoints payload is different for exclusions. This method will filter the attributes needed for each each endpoint.

Parameters:

  • attr_name (Symbol)
  • ng_endpoint (Boolean) (defaults to: false)

Returns:

  • (Symbol, nil)


78
79
80
81
82
83
84
85
86
# File 'lib/contrast/agent/reporting/settings/helpers.rb', line 78

def ng_attribute_filter attr_name, ng_endpoint: false
  if NO_MORE_UNDERSCORE_EXCLUSIONS.key?(attr_name)
    return NO_MORE_UNDERSCORE_EXCLUSIONS[attr_name] if ng_endpoint

    return attr_name
  end

  no_more_underscore(attr_name)
end

.no_more_underscore(attr_name) ⇒ Object

:attr_name => :attrName return original if no ‘_’

Parameters:

  • attr_name (Symbol)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/contrast/agent/reporting/settings/helpers.rb', line 59

def no_more_underscore attr_name
  name = attr_name.to_s
  idx = name.index('_')
  return attr_name if idx.nil? || (idx.zero? && idx >= name.length)

  result = name
  result.slice!(idx)
  result.insert(idx, name[idx]&.upcase)
  result.slice!(idx + 1)
  result.index('_') ? no_more_underscore(result).to_sym : result.to_sym
end

.to_rule_id(id) ⇒ Object

rule_id => rule-id

Parameters:



91
92
93
94
95
# File 'lib/contrast/agent/reporting/settings/helpers.rb', line 91

def to_rule_id id
  return Contrast::Utils::ObjectShare::EMPTY_STRING unless id.cs__is_a?(String) || id.cs__is_a?(Symbol)

  id.to_s.downcase.tr('_', '-')
end