forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudonymisation.rb
45 lines (35 loc) · 1.01 KB
/
pseudonymisation.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
# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
class Pseudonymisation
def self.of_hash(source)
return if source.blank?
source.transform_values do |value|
of_value(value.to_s)
end
end
def self.of_value(source)
of_email_address(source)
rescue
of_string(source)
end
def self.of_email_address(source)
email_address = Mail::AddressList.new(source).addresses.first
"#{of_string(email_address.local)}@#{of_domain(email_address.domain)}"
rescue
raise ArgumentError
end
def self.of_domain(source)
domain_parts = source.split('.')
# e.g. localhost
return of_string(source) if domain_parts.size == 1
tld = domain_parts[-1]
other = domain_parts[0..-2].join('.')
"#{of_string(other)}.#{tld}"
end
def self.of_string(source)
return '*' if source.to_s.length <= 1
return "#{source.first}*#{source.last}" if source.exclude?(' ')
source.split.map do |sub_string|
of_string(sub_string)
end.join(' ')
end
end