Class: Worldline::Acquiring::SDK::Logging::Obfuscation::BodyObfuscator

Inherits:
Object
  • Object
show all
Defined in:
lib/worldline/acquiring/sdk/logging/obfuscation/body_obfuscator.rb

Overview

A class that can be used to obfuscate properties in JSON bodies.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(additional_rules = nil) ⇒ BodyObfuscator

Creates a new body obfuscator. This will contain some pre-defined obfuscation rules, as well as any provided custom rules

Parameters:

  • additional_rules (Hash) (defaults to: nil)

    An optional hash where the keys are property names and the values are functions that obfuscate a single value



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/worldline/acquiring/sdk/logging/obfuscation/body_obfuscator.rb', line 16

def initialize(additional_rules = nil)
  @obfuscation_rules = {
    "address" => Obfuscation.obfuscate_all,
    "authenticationValue" => Obfuscation.obfuscate_all_but_first(4),
    "bin" => Obfuscation.obfuscate_all_but_first(6),
    "cardholderAddress" => Obfuscation.obfuscate_all,
    "cardholderPostalCode" => Obfuscation.obfuscate_all,
    "cardNumber" => Obfuscation.obfuscate_all_but_last(4),
    "cardSecurityCode" => Obfuscation.obfuscate_all,
    "city" => Obfuscation.obfuscate_all,
    "cryptogram" => Obfuscation.obfuscate_all_but_first(4),
    "expiryDate" => Obfuscation.obfuscate_all_but_last(4),
    "name" => Obfuscation.obfuscate_all,
    "paymentAccountReference" => Obfuscation.obfuscate_all_but_first(6),
    "postalCode" => Obfuscation.obfuscate_all,
    "stateCode" => Obfuscation.obfuscate_all,
  }
  if additional_rules
    additional_rules.each do |name, rule|
      @obfuscation_rules[name] = rule
    end
  end

  @property_pattern = build_property_pattern(@obfuscation_rules.keys)
end

Class Method Details

.default_obfuscatorBodyObfuscator

Returns:



93
94
95
# File 'lib/worldline/acquiring/sdk/logging/obfuscation/body_obfuscator.rb', line 93

def self.default_obfuscator
  DEFAULT_OBFUSCATOR
end

Instance Method Details

#obfuscate_body(body) ⇒ String

Obfuscates the given body as necessary.

Returns:

  • (String)


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/worldline/acquiring/sdk/logging/obfuscation/body_obfuscator.rb', line 73

def obfuscate_body(body)
  return nil if body.nil?
  return '' if body.empty?

  body.gsub(@property_pattern) do
    m = Regexp.last_match
    property_name = m[2]
    value = m[4] || m[5]
    # copy value 'cause it's part of m[0]
    m[0].sub(value, obfuscate_value(property_name, value.dup))
  end
end