Кирчо обнови решението на 27.11.2012 17:25 (преди почти 12 години)
+module FilterEmail
+ def filter_email
+ if partially_preserve_email_username then partially_preserve_email
+ elsif preserve_email_hostname then preserve_hostname
+ else @text.gsub(/[a-zA-Z0-9][-\w\+\.]{,199}@([a-zA-Z0-9][-a-zA-Z0-9]{,61}(?<!-)\.)+[a-zA-z]{2,3}/, '[EMAIL]')
+ end
+ end
+
+ def preserve_hostname
+ @text.gsub(/[a-zA-Z0-9][-\w\+\.]{,199}(@([a-zA-Z0-9][-a-zA-Z0-9]{,61}(?<!-)\.)+[a-zA-z]{2,3})/) do |matched|
+ '[FILTERED]' + $1
+ end
+ end
+
+ def partially_preserve_email
+ @text.gsub(/([a-zA-Z0-9][-\w\+\.]{,199})(@([a-zA-Z0-9][-a-zA-Z0-9]{,61}(?<!-)\.)+[a-zA-z]{2,3})/) do |matched|
+ if $1.length < 6
+ '[FILTERED]' + $2
+ else
+ $1[0,3] + '[FILTERED]' + $2
+ end
+ end
+ end
+end
+
+module FilterPhone
+ def filter_phone
+ if preserve_phone_country_code
+ @text = @text.gsub(/((00|\+)[1-9]\d{0,2})([- \)\(])*\d(\g<3>{,2}\d){5,10}/) { |matched| $1 + " [FILTERED]"}
+ end
+ @text.gsub(/\b0([- \)\(])*\d(\g<1>{,2}\d){5,10}/, '[PHONE]')
+ end
+end
+
+class PrivacyFilter
+ include FilterEmail, FilterPhone
+ attr_accessor :preserve_phone_country_code,
+ :preserve_email_hostname,
+ :partially_preserve_email_username
+
+ def initialize(text)
+ @text = text
+ end
+
+ def filtered
+ @text = filter_email
+ @text = filter_phone
+
+ @text
+ end
+end
+
+class Validations
+ class << self
+ def email?(value)
+ value_parts = value.split('@')
+ name, hostname = value_parts
+ value_parts.length == 2 and hostname?(hostname) and
+ /\A[a-zA-Z0-9][-\w\+\.]{,199}\z/.match(name) != nil
+ end
+
+ def phone?(value)
+ /\A(0|(00|\+)[1-9]\d)([- \)\(])*\d(\g<3>{,2}\d){5,10}\z/.match(value) != nil
+ end
+
+ def hostname?(value)
+ /\A([a-zA-Z0-9][-a-zA-Z0-9]{,61}(?<!-)\.)+[a-zA-z]{2,3}\z/.match(value) != nil
+ end
+
+ def ip_address?(value)
+ /\A(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).\g<1>.\g<1>.\g<1>\z/.match(value) != nil
+ end
+
+ def number?(value)
+ /\A-?(0|[1-9]\d*|0\.\d+|[1-9]\d*\.\d*)\z/.match(value) != nil
+ end
+
+ def integer?(value)
+ /\A-?(0|[1-9]\d*)\z/.match(value) != nil
+ end
+
+ def date?(value)
+ /\A\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\z/.match(value) != nil
+ end
+
+ def time?(value)
+ /\A([01]\d|2[0-3]):([0-5]\d):\g<2>\z/.match(value) != nil
+ end
+
+ def date_time?(value)
+ value_parts = value.split(/ |T/)
+ date, time = value_parts
+ value_parts.length == 2 and date?(date) and time?(time)
+ end
+ end
+end