pseudonymisation.rb 956 B

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