Class: Bosh::Cpi::Redactor

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/cpi/redactor.rb

Constant Summary collapse

REDACTED =
'<redacted>'

Class Method Summary collapse

Class Method Details

.clone_and_redact(hash, *paths) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/bosh/cpi/redactor.rb', line 6

def self.clone_and_redact(hash, *paths)
  begin
    hash = JSON.parse(hash.to_json)
  rescue
    return nil
  end

  redact!(hash, *paths)
end

.fetch_propertyObject



28
29
30
# File 'lib/bosh/cpi/redactor.rb', line 28

def self.fetch_property
  -> (hash, property) { hash.fetch(property, {})}
end

.redact!(hash, *json_paths) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bosh/cpi/redactor.rb', line 16

def self.redact!(hash, *json_paths)
  json_paths.each do |json_path|
    properties = json_path.split('.')
    property_to_redact = properties.pop

    target_hash = properties.reduce(hash, &fetch_property)
    target_hash[property_to_redact] = REDACTED if target_hash.has_key? property_to_redact
  end

  hash
end