-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrubber.rb
70 lines (58 loc) · 1.38 KB
/
scrubber.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'json'
class Scrubber
def initialize(sensitive_fields_file, input_file)
check_file(sensitive_fields_file)
check_file(input_file)
@sensitive_fields = File.readlines(sensitive_fields_file).map(&:strip)
@input_data = JSON.parse(File.read(input_file))
end
def scrub
scrub_obj(@input_data).compact
end
private
def check_file(filename)
unless File.file?(filename)
raise ArgumentError.new("file not found: #{filename}")
end
end
def scrub_obj(obj)
new_obj = {}
obj.each do |key, value|
if @sensitive_fields.include?(key)
new_obj[key] = scrub_sensitive_field(value)
elsif value.is_a?(Hash)
new_obj[key] = scrub_obj(value)
elsif value.is_a?(Array)
new_obj[key] = scrub_array(value)
else
new_obj[key] = value
end
end
new_obj
end
def scrub_array(arr)
new_arr = []
arr.each do |field|
if field.is_a?(Hash)
new_arr << scrub_obj(field)
else
new_arr << field
end
end
new_arr
end
def scrub_sensitive_field(field)
case field
when String, Numeric
field.to_s.gsub(/\w/, "*")
when TrueClass, FalseClass
"-"
when Array
field.map { |value| scrub_sensitive_field(value)}
when Hash
field.transform_values { |value| scrub_sensitive_field(value) }
else
field
end
end
end