@@ -6,36 +6,30 @@ class Processor::SanitizeData < Processor
6
6
VALUES_RE = /^\d {16}$/
7
7
8
8
def process ( value )
9
- fields_re = /(#{ ( DEFAULT_FIELDS + @sanitize_fields ) . join ( "|" ) } )/i
9
+ value . inject ( value ) { |memo , ( k , v ) | memo [ k ] = sanitize ( k , v ) ; memo }
10
+ end
10
11
11
- value . inject ( value ) do | value , ( k , v ) |
12
- v = k if v . nil?
13
- if v . is_a? ( Hash ) || v . is_a? ( Array )
14
- process ( v )
15
- elsif v . is_a? ( String ) && ( json = parse_json_or_nil ( v ) )
16
- #if this string is actually a json obj, convert and sanitize
17
- value = modify_in_place ( value , [ k , v ] , process ( json ) . to_json )
18
- elsif v . is_a? ( Integer ) && ( VALUES_RE . match ( v . to_s ) || fields_re . match ( k . to_s ) )
19
- value = modify_in_place ( value , [ k , v ] , INT_MASK )
20
- elsif VALUES_RE . match ( v . to_s ) || fields_re . match ( k . to_s )
21
- value = modify_in_place ( value , [ k , v ] , STRING_MASK )
22
- else
23
- value
24
- end
12
+ def sanitize ( k , v )
13
+ if v . is_a? ( Hash )
14
+ process ( v )
15
+ elsif v . is_a? ( Array )
16
+ v . map { | a | sanitize ( nil , a ) }
17
+ elsif v . is_a? ( String ) && ( json = parse_json_or_nil ( v ) )
18
+ #if this string is actually a json obj, convert and sanitize
19
+ json . is_a? ( Hash ) ? process ( json ) . to_json : v
20
+ elsif v . is_a? ( Integer ) && ( VALUES_RE . match ( v . to_s ) || fields_re . match ( k . to_s ) )
21
+ INT_MASK
22
+ elsif v . is_a? ( String ) && ( VALUES_RE . match ( v . to_s ) || fields_re . match ( k . to_s ) )
23
+ STRING_MASK
24
+ else
25
+ v
25
26
end
26
- value
27
27
end
28
28
29
29
private
30
30
31
- def modify_in_place ( original_parent , original_child , new_child )
32
- if original_parent . is_a? ( Array )
33
- index = original_parent . index ( original_child [ 0 ] )
34
- original_parent [ index ] = new_child
35
- elsif original_parent . is_a? ( Hash )
36
- original_parent [ original_child [ 0 ] ] = new_child
37
- end
38
- original_parent
31
+ def fields_re
32
+ @fields_re ||= /(#{ ( DEFAULT_FIELDS + @sanitize_fields ) . join ( "|" ) } )/i
39
33
end
40
34
end
41
35
end
0 commit comments