Йордан обнови решението на 27.11.2012 15:12 (преди почти 12 години)
+class PrivacyFilter
+ attr_reader :text
+ attr_accessor :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_phone_number(country_code)
+ if not country_code then '[PHONE]'
+ elsif preserve_phone_country_code then country_code + ' [FILTERED]'
+ else '[PHONE]'
+ end
+ end
+
+ def filtered_email(user, at_hostname)
+ if partially_preserve_email_username then (user[0..2] unless user.length < 6).to_s + '[FILTERED]' + at_hostname
+ elsif preserve_email_hostname then '[FILTERED]' + at_hostname
+ else '[EMAIL]'
+ end
+ end
+
+
+ def filtered
+ hostname = /((\w[\w\-]{,60}\w)\.)*(\w[\w\-]{,60}\w)\.([a-zA-Z]{2,3})(\.[a-zA-Z]{2}){,1}/
+ result = @text.gsub(/(\w[\w\_\+\.\-]{,200})(\@#{hostname})/) { filtered_email $1, $2 }
+ result.gsub(/(?<!\w|\+)(((00|\+)[1-9]\d{1,2})|(0))([\s\-\(\)]{,2}\d){6,11}(?!\w)/) { filtered_phone_number $2 }
+ end
+end
+
+class Validations
+ def self.email?(value)
+ hostname = /((\w[\w\-]{,60}\w)\.)*(\w[\w\-]{,60}\w)\.([a-zA-Z]{2,3})(\.[a-zA-Z]{2}){,1}/
+
+ if /(\w[\w\_\+\.\-]{,200})(\@)(#{hostname})/.match value then true
+ else false
+ end
+ end
+
+ def self.phone?(value)
+ if /\A(((00|\+)[1-9]\d{1,2})|0)([\s\-\(\)]{,2}\d){6,11}\z/.match value then true
+ else false
+ end
+ end
+
+ def self.hostname?(value)
+ if /\A((\w[\w\-]{,60}\w)\.)*(\w[\w\-]{,60}\w)\.([a-zA-Z]{2,3})(\.[a-zA-Z]{2}){,1}\z/.match value then true
+ else false
+ end
+ end
+
+ def self.ip_address?(value)
+ if /\A(0\.|(1{,1}[0-9]{,2}\.)|(2[0-5][0-5]\.)){3}(0|(1{,1}[0-9]{,2})|(2[0-5][0-5]))\z/.match value then true
+ else false
+ end
+ end
+
+ def self.number?(value)
+ if /\A\-{,1}(0{,1}|[1-9][0-9]*)(\.[0-9]+){,1}\z/.match value then true
+ else false
+ end
+ end
+
+ def self.integer?(value)
+ if /\A\-{,1}(0{,1}|[1-9][0-9]*)\z/.match value then true
+ else false
+ end
+ end
+
+ def self.date?(value)
+ if /\A[0-9]{4}\-(0[1-9]|1[12])\-(0[1-9]|[12][0-9]|3[01])\z/.match value then true
+ else false
+ end
+ end
+
+ def self.time?(value)
+ if /\A([01][0-9]|2[0-3])\:[0-5][0-9]\:[0-5][0-9]\z/.match value then true
+ else false
+ end
+ end
+
+ def self.date_time?(value)
+ date = /[0-9]{4}\-(0[1-9]|1[12])\-(0[1-9]|[12][0-9]|3[01])/
+ time = /([01][0-9]|2[0-3])\:[0-5][0-9]\:[0-5][0-9]/
+
+ if /\A#{date}(\-|\ |T)#{time}\z/.match value then true
+ else false
+ end
+ end
+end