Александър обнови решението на 28.11.2012 15:55 (преди почти 12 години)
+class PrivacyFilter
+ 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 phone_filtered(text)
+ text.gsub(/(((00)|\+([1-9]\d{0,2}))|0)((\ |\(\)){0,2}\d){6,11}/) do |match|
+ ($1 != '0' and @preserve_phone_country_code) ? "#{$1} [FILTERED]" : "[PHONE]"
+ end
+ end
+
+ def email_filtered(text)
+ text.gsub(
+ /([a-zA-Z0-9](\w|\-|\_|\+|\.){,200})\@(((\w)(\w|\-){0,61}(\w)\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)/) do |match|
+ case
+ when @partially_preserve_email_username and $1.length < 6 then "[FILTERED]@#{$3}"
+ when @partially_preserve_email_username then "#{$1[0..2]}[FILTERED]@#{$3}"
+ when @preserve_email_hostname ? then "[FILTERED]@#{$3}"
+ else "[EMAIL]"
+ end
+ end
+ end
+
+ def filtered
+ phone_filtered(email_filtered(@text))
+ end
+end
+
+module Validations
+ def self.email?(value)
+ email_beginning = value.match /^[a-zA-Z0-9](\w|\-|\_|\+|\.){,200}\@/
+ !!(email_beginning and hostname? value[value.index('@')+1..-1]) # value.match(/[[:alnum]]\w+\@/) {
+ end
+
+ def self.hostname?(value)
+ !!value.match(/^((\w)(\w|\-){0,61}(\w)\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$/)
+ end
+
+ def self.phone?(value)
+ !!value.match(/^(0|(00|\+[1-9]\d{0,2}))((\ |\(\)){0,2}\d){6,11}$/)
+ end
+
+ def self.ip_address?(value)
+ ip_match = value.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)
+ ip_match ? ip_match.captures.all? { |capture| capture.to_i >= 0 and capture.to_i <= 255} : false
+ end
+
+ def self.number?(value)
+ !!value.match(/^\-?(0|([1-9]\d*))(\.\d+)?$/)
+ end
+
+ def self.integer?(value)
+ !!value.match(/^\-?(0|([1-9]\d*))$/)
+ end
+
+ def self.date?(value)
+ !!value.match(/^\d{4}\-(0[1-9])|(1|[0-2])\-([0-2][0-9])|(3|([0-1]))$/)
+ end
+
+ def self.time?(value)
+ !!value.match(/^([0-1][0-9])|(2[0-3])\:[0-5][0-9]\:[0-5][0-9]$/)
+ end
+
+ def self.date_time?(value)
+ !!value.match(
+ /^\d{4}\-(0[1-9])|(1|[0-2])\-([0-2][0-9])|(3|([0-1]))(\ |\T)([0-1][0-9])|(2[0-3])\:[0-5][0-9]\:[0-5][0-9]$/)
+ end
+end