Иван обнови решението на 25.11.2012 11:40 (преди почти 12 години)
+class PrivacyFilter
+ attr_accessor :text, :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+
+ def initialize(text)
+ @text = text
+ @preserve_phone_country_code = false
+ @preserve_email_hostname = false
+ @partially_preserve_email_username = false
+ end
+
+ def filtered
+ return filter_email(filter_phone(@text)) if Validations.email?(@text) and
+ /(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d\4{,2})\g<5>{4,9}\d\b/ =~ @text
+ return filter_email(@text) if Validations.email?(@text)
+ return filter_phone(@text) if /(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d\4{,2})\g<5>{4,9}\d\b/ =~ @text
+ end
+
+ def filter_email(text)
+ reg_exp = /\b(([[:alnum:]][\w\+\.\-]{,2})[\w\+\.\-]{,198})
+ (@([[:alnum:]]([a-zA-Z0-9-]{,60}[^-])?\.)*[[:alnum:]]\g<5>?(\.([a-z]|[A-Z]){2,3})\g<6>?)\b/x
+ if preserve_email_hostname or partially_preserve_email_username then return filter_email_1(text, reg_exp)
+ else return text.gsub(reg_exp, '[EMAIL]')
+ end
+ end
+
+ def filter_email_1(text, reg_exp)
+ if reg_exp.match(text) and preserve_email_hostname then return text.gsub(reg_exp, '[FILTERED]'+$3)
+ elsif reg_exp.match(text) and partially_preserve_email_username then return filter_email_2(text, reg_exp)
+ end
+ end
+
+ def filter_email_2(text, reg_exp)
+ if partially_preserve_email_username and reg_exp.match(text)
+ if $1.to_s.length >= 6 then return text.gsub(reg_exp, $2+'[FILTERED]'+$3)
+ else return text.gsub(reg_exp, '[FILTERED]'+$3)
+ end
+ end
+ end
+
+ def filter_phone(text)
+ if /((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d\3{,2})\g<4>{4,9}\d/.match(text) and preserve_phone_country_code
+ text1 = text.gsub(/((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d\3{,2})\g<4>{4,9}\d/, $1+' [FILTERED]')
+ return text1.gsub(/0(([- \(\)])?\2?\d\2{,2})\g<1>{4,9}\d/, '[PHONE]')
+ else return text.gsub(/(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d\4{,2})\g<5>{4,9}\d\b/, '[PHONE]')
+ end
+ end
+end
+
+module Validations
+ def self.email?(value)
+ return true if /\b[[:alnum:]][\w\+\.\-]{,200}@([[:alnum:]]([a-zA-Z0-9-]
+ {,60}[^-])?\.)*[[:alnum:]]\g<2>?(\.([a-z]|[A-Z]){2,3})\g<3>?\b/x =~ value
+ false
+ end
+
+ def self.phone?(value)
+ return true if /\A(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d\4{,2})\g<5>{4,9}\d\z/ =~ value
+ false
+ end
+
+ def self.hostname?(value)
+ return true if /\A([[:alnum:]]([a-zA-Z0-9-]{,60}[^-])?\.)*[[:alnum:]]\g<2>?(\.([a-z]|[A-Z]){2,3})\g<3>?\z/ =~ value
+ false
+ end
+
+ def self.ip_address?(value)
+ return true if /\A(\d|[1-9]\d|1\d\d|2[0-5][0-5])\.\g<1>\.\g<1>\.\g<1>\z/ =~ value
+ false
+ end
+
+ def self.number?(value)
+ return true if /\A(-?(0|[1-9]\d*)\.\d+|(0|-?[1-9]\d*))\z/ =~ value
+ false
+ end
+
+ def self.integer?(value)
+ return true if /\A(0|-?[1-9]\d*)\z/ =~ value
+ false
+ end
+
+ def self.date?(value)
+ return true if /\A[1-9]\d{3}-(0[1-9]|1[0-2])-([0-2][1-9]|3[0-1])\z/ =~ value
+ false
+ end
+
+ def self.time?(value)
+ return true if /\A([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d\z/ =~ value
+ false
+ end
+
+ def self.date_time?(value)
+ return true if /\A[1-9]\d{3}-(0[1-9]|1[0-2])-([0-2][1-9]|3[0-1])(\s|T)([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d\z/ =~ value
+ false
+ end
+end