pseudonymisation.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Pseudonymisation
  3. def self.of_hash(source)
  4. return if source.blank?
  5. source.transform_values do |value|
  6. of_value(value.to_s)
  7. end
  8. end
  9. def self.of_value(source)
  10. of_email_address(source)
  11. rescue
  12. of_string(source)
  13. end
  14. def self.of_email_address(source)
  15. email_address = Mail::AddressList.new(source).addresses.first
  16. "#{of_string(email_address.local)}@#{of_domain(email_address.domain)}"
  17. rescue
  18. raise ArgumentError
  19. end
  20. def self.of_domain(source)
  21. domain_parts = source.split('.')
  22. # e.g. localhost
  23. return of_string(source) if domain_parts.size == 1
  24. tld = domain_parts[-1]
  25. other = domain_parts[0..-2].join('.')
  26. "#{of_string(other)}.#{tld}"
  27. end
  28. def self.of_string(source)
  29. return '*' if source.to_s.length <= 1
  30. return "#{source.first}*#{source.last}" if source.exclude?(' ')
  31. source.split.map do |sub_string|
  32. of_string(sub_string)
  33. end.join(' ')
  34. end
  35. end