Решение на Четвърта задача от Йордан Петров

Обратно към всички решения

Към профила на Йордан Петров

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 37 успешни тест(а)
  • 4 неуспешни тест(а)

Код

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 filtered
filtered_phone_numbers filtered_emails @text
end
private
def filtered_phone_numbers(text)
text.gsub(/(?<!\w|\+)(((00|\+)[1-9]\d{,2})|0(?!0))([\s\-\(\)]{,2}\d){6,11}/) do
if preserve_phone_country_code and $2 then $2 + ' [FILTERED]'
else '[PHONE]'
end
end
end
def filtered_emails(text)
text.gsub(/([A-Za-z0-9][\w\+\.\-]{,200})(\@([A-Za-z0-9][A-Za-z0-9\-]{,62}(?<!\-)\.)+[a-zA-Z]{2,3})/) do
if partially_preserve_email_username and $1.length >= 6 then $1[0, 3] + '[FILTERED]' + $2
elsif partially_preserve_email_username or preserve_email_hostname then '[FILTERED]' + $2
else '[EMAIL]'
end
end
end
end
class Validations
@@date = /[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/
@@time = /([01][0-9]|2[0-3])\:[0-5][0-9]\:[0-5][0-9]/
@@hostname = /([A-Za-z0-9][A-Za-z0-9\-]{,62}(?<!\-)\.)+[a-zA-Z]{2,3}/
@@integer = /\-{,1}(0{,1}|[1-9][0-9]*)/
def self.email?(value)
not not value =~ /\A[A-Za-z0-9][\w\+\.\-]{,200}\@#{@@hostname}\z/
end
def self.phone?(value)
not not value =~ /\A(((00|\+)[1-9]\d{,2})|0(?!0))([\s\-\(\)]{,2}\d){6,11}\z/
end
def self.hostname?(value)
not not value =~ /\A#{@@hostname}\z/
end
def self.ip_address?(value)
not not value =~ /\A((?<ip_number>0|(1{,1}[0-9]{1,2})|(2[0-5]{2}))\.){3}\g<ip_number>\z/
end
def self.number?(value)
not not value =~ /\A#{@@integer}(\.[0-9]+){,1}\z/
end
def self.integer?(value)
not not value =~ /\A#{@@integer}\z/
end
def self.date?(value)
not not value =~ /\A#{@@date}\z/
end
def self.time?(value)
not not value =~ /\A#{@@time}\z/
end
def self.date_time?(value)
not not value =~ /\A#{@@date}(\ |T)#{@@time}\z/
end
end

Лог от изпълнението

...F.....F.........F......F..............

Failures:

  1) PrivacyFilter does not filter invalid emails
     Failure/Error: filter(text_with_invalid_emails).should eq text_with_invalid_emails
       
       expected: "Contact me here: _invalid@email.com"
            got: "Contact me here: _[EMAIL]"
       
       (compared using ==)
     # /tmp/d20130203-23049-yeiy81/spec.rb:52:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-yeiy81/spec.rb:46:in `each'
     # /tmp/d20130203-23049-yeiy81/spec.rb:46:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) PrivacyFilter does not filter invalid phone numbers
     Failure/Error: filter(text).should eq filtered
       
       expected: "Reach me at: 0885123"
            got: "Reach me at: [PHONE]"
       
       (compared using ==)
     # /tmp/d20130203-23049-yeiy81/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-yeiy81/spec.rb:95:in `each'
     # /tmp/d20130203-23049-yeiy81/spec.rb:95:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Validations can validate more complex phone numbers
     Failure/Error: Validations.phone?(phone).should be(valid)
       
       expected #<FalseClass:0> => false
            got #<TrueClass:2> => true
       
       Compared using equal?, which compares object identity,
       but expected and actual are not the same object. Use
       'actual.should eq(expected)' if you don't care about
       object identity in this example.
     # /tmp/d20130203-23049-yeiy81/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-yeiy81/spec.rb:209:in `each'
     # /tmp/d20130203-23049-yeiy81/spec.rb:209:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Validations validates more complex numbers
     Failure/Error: Validations.number?('.42').should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-yeiy81/spec.rb:264:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.06464 seconds
41 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-yeiy81/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-yeiy81/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-yeiy81/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-yeiy81/spec.rb:257 # Validations validates more complex numbers

История (5 версии и 0 коментара)

Йордан обнови решението на 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

Йордан обнови решението на 28.11.2012 00:46 (преди почти 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]'
+ if preserve_phone_country_code and 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 = @text.gsub(/(\w[\w\_\+\.\-]{,200})(\@(\w[\w\-]{,60}\w\.)+[a-zA-Z]{2,3})/) { 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}/
+ @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 /(\w[\w\_\+\.\-]{,200})(\@)(#{hostname})/.match value then true
+ def self.email?(value)
+ if /(\w[\w\_\+\.\-]{,200})(\@)((\w[\w\-]{,60}\w)\.)+([a-zA-Z]{2,3})(\.[a-zA-Z]{2}){,1}/.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
+ if /\A(\w[\w\-]{,60}\w\.)+[a-zA-Z]{2,3}\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
+ if /\A#{@date}\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
+ if /\A#{@time}\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
+ if /\A#{@date}(\ |T)#{@time}\z/.match value then true
else false
end
end
end

Йордан обнови решението на 28.11.2012 03:23 (преди почти 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 preserve_phone_country_code and 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
- result = @text.gsub(/(\w[\w\_\+\.\-]{,200})(\@(\w[\w\-]{,60}\w\.)+[a-zA-Z]{2,3})/) { filtered_email $1, $2 }
- result.gsub(/(?<!\w|\+)(((00|\+)[1-9]\d{1,2})|(0))([\s\-\(\)]{,2}\d){6,11}(?!\w)/) { filtered_phone_number $2 }
+ email = /([A-Za-z0-9][\w\+\.\-]{,200})(\@([A-Za-z0-9][A-Za-z0-9\-]{,61}[A-Za-z0-9]\.)+[a-zA-Z]{2,3})/
+ phone_number = /(?<!\w|\+)(((00|\+)[1-9]\d{1,2})|(0))([\s\-\(\)]{,2}\d){6,11}/
+
+ @text.gsub(email) { filtered_email $1, $2 }.
+ gsub(phone_number) { filtered_phone_number $2 }
end
end
class Validations
- @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]/
+ @date = /[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/
+ @time = /([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/
+ @hostname = /([A-Za-z0-9][A-Za-z0-9\-]{,61}[A-Za-z0-9]\.)+[a-zA-Z]{2,3}/
+ @integer = /\-{,1}(0{,1}|[1-9][0-9]*)/
- def self.email?(value)
- if /(\w[\w\_\+\.\-]{,200})(\@)((\w[\w\-]{,60}\w)\.)+([a-zA-Z]{2,3})(\.[a-zA-Z]{2}){,1}/.match value then true
- else false
- end
+ def self.email?(text)
+ not text !~ /\A[A-Za-z0-9][\w\+\.\-]{,200}\@#{@hostname}\z/
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
+ def self.phone?(text)
+ not text !~ /\A(((00|\+)[1-9]\d{1,2})|0)([\s\-\(\)]{,2}\d){6,11}\z/
end
- def self.hostname?(value)
- if /\A(\w[\w\-]{,60}\w\.)+[a-zA-Z]{2,3}\z/.match value then true
- else false
- end
+ def self.hostname?(text)
+ not text !~ /\A#{@hostname}\z/
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
+ def self.ip_address?(text)
+ not text !~ /\A((?<ip_number>0|(1{,1}[0-9]{,2})|(2[0-5]{2}))\.){3}\g<ip_number>\z/
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
+ def self.number?(text)
+ not text !~ /\A#{@integer}(\.[0-9]+){,1}\z/
end
- def self.integer?(value)
- if /\A\-{,1}(0{,1}|[1-9][0-9]*)\z/.match value then true
- else false
- end
+ def self.integer?(text)
+ not text !~ /\A#{@integer}\z/
end
- def self.date?(value)
- if /\A#{@date}\z/.match value then true
- else false
- end
+ def self.date?(text)
+ not text !~ /\A#{@date}\z/
end
- def self.time?(value)
- if /\A#{@time}\z/.match value then true
- else false
- end
+ def self.time?(text)
+ not text !~ /\A#{@time}\z/
end
- def self.date_time?(value)
- if /\A#{@date}(\ |T)#{@time}\z/.match value then true
- else false
- end
+ def self.date_time?(text)
+ not text !~ /\A#{@date}(\ |T)#{@time}\z/
end
end

Йордан обнови решението на 28.11.2012 15:14 (преди почти 12 години)

class PrivacyFilter
- attr_reader :text
- attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+ 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 preserve_phone_country_code and country_code then country_code + ' [FILTERED]'
- else '[PHONE]'
- end
+ def filtered
+ filtered_phone_numbers filtered_emails @text
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]'
+ private
+
+ def filtered_phone_numbers(text)
+ text.gsub(/(?<!\w|\+)(((00|\+)[1-9]\d{,2})|(0(?!0)))([\s\-\(\)]{,2}\d){6,11}/) do
+ if preserve_phone_country_code and $2 then $2 + ' [FILTERED]'
+ else '[PHONE]'
+ end
end
end
-
- def filtered
- email = /([A-Za-z0-9][\w\+\.\-]{,200})(\@([A-Za-z0-9][A-Za-z0-9\-]{,61}[A-Za-z0-9]\.)+[a-zA-Z]{2,3})/
- phone_number = /(?<!\w|\+)(((00|\+)[1-9]\d{1,2})|(0))([\s\-\(\)]{,2}\d){6,11}/
-
- @text.gsub(email) { filtered_email $1, $2 }.
- gsub(phone_number) { filtered_phone_number $2 }
+ def filtered_emails(text)
+ text.gsub(/([A-Za-z0-9][\w\+\.\-]{,200})(\@([A-Za-z0-9][A-Za-z0-9\-]{,62}(?<!\-)\.)+[a-zA-Z]{2,3})/) do
+ if partially_preserve_email_username and $1.length >= 6 then $1[0, 3] + '[FILTERED]' + $2
+ elsif partially_preserve_email_username or preserve_email_hostname then '[FILTERED]' + $2
+ else '[EMAIL]'
+ end
+ end
end
end
class Validations
- @date = /[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/
- @time = /([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/
- @hostname = /([A-Za-z0-9][A-Za-z0-9\-]{,61}[A-Za-z0-9]\.)+[a-zA-Z]{2,3}/
- @integer = /\-{,1}(0{,1}|[1-9][0-9]*)/
+ @@date = /[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/
+ @@time = /([01][0-9]|2[0-3])\:[0-5][0-9]\:[0-5][0-9]/
+ @@hostname = /([A-Za-z0-9][A-Za-z0-9\-]{,62}(?<!\-)\.)+[a-zA-Z]{2,3}/
+ @@integer = /\-{,1}(0{,1}|[1-9][0-9]*)/
- def self.email?(text)
- not text !~ /\A[A-Za-z0-9][\w\+\.\-]{,200}\@#{@hostname}\z/
+ def self.email?(value)
+ not not value =~ /\A[A-Za-z0-9][\w\+\.\-]{,200}\@#{@@hostname}\z/
end
- def self.phone?(text)
- not text !~ /\A(((00|\+)[1-9]\d{1,2})|0)([\s\-\(\)]{,2}\d){6,11}\z/
+ def self.phone?(value)
+ not not value =~ /\A(((00|\+)[1-9]\d{,2})|0)([\s\-\(\)]{,2}\d){6,11}\z/
end
- def self.hostname?(text)
- not text !~ /\A#{@hostname}\z/
+ def self.hostname?(value)
+ not not value =~ /\A#{@@hostname}\z/
end
- def self.ip_address?(text)
- not text !~ /\A((?<ip_number>0|(1{,1}[0-9]{,2})|(2[0-5]{2}))\.){3}\g<ip_number>\z/
+ def self.ip_address?(value)
+ not not value =~ /\A((?<ip_number>0|(1{,1}[0-9]{1,2})|(2[0-5]{2}))\.){3}\g<ip_number>\z/
end
- def self.number?(text)
- not text !~ /\A#{@integer}(\.[0-9]+){,1}\z/
+ def self.number?(value)
+ not not value =~ /\A#{@@integer}(\.[0-9]+){,1}\z/
end
- def self.integer?(text)
- not text !~ /\A#{@integer}\z/
+ def self.integer?(value)
+ not not value =~ /\A#{@@integer}\z/
end
- def self.date?(text)
- not text !~ /\A#{@date}\z/
+ def self.date?(value)
+ not not value =~ /\A#{@@date}\z/
end
- def self.time?(text)
- not text !~ /\A#{@time}\z/
+ def self.time?(value)
+ not not value =~ /\A#{@@time}\z/
end
- def self.date_time?(text)
- not text !~ /\A#{@date}(\ |T)#{@time}\z/
+ def self.date_time?(value)
+ not not value =~ /\A#{@@date}(\ |T)#{@@time}\z/
end
end

Йордан обнови решението на 28.11.2012 15:30 (преди почти 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 filtered
filtered_phone_numbers filtered_emails @text
end
private
def filtered_phone_numbers(text)
- text.gsub(/(?<!\w|\+)(((00|\+)[1-9]\d{,2})|(0(?!0)))([\s\-\(\)]{,2}\d){6,11}/) do
+ text.gsub(/(?<!\w|\+)(((00|\+)[1-9]\d{,2})|0(?!0))([\s\-\(\)]{,2}\d){6,11}/) do
if preserve_phone_country_code and $2 then $2 + ' [FILTERED]'
else '[PHONE]'
end
end
end
def filtered_emails(text)
text.gsub(/([A-Za-z0-9][\w\+\.\-]{,200})(\@([A-Za-z0-9][A-Za-z0-9\-]{,62}(?<!\-)\.)+[a-zA-Z]{2,3})/) do
if partially_preserve_email_username and $1.length >= 6 then $1[0, 3] + '[FILTERED]' + $2
elsif partially_preserve_email_username or preserve_email_hostname then '[FILTERED]' + $2
else '[EMAIL]'
end
end
end
end
class Validations
@@date = /[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/
@@time = /([01][0-9]|2[0-3])\:[0-5][0-9]\:[0-5][0-9]/
@@hostname = /([A-Za-z0-9][A-Za-z0-9\-]{,62}(?<!\-)\.)+[a-zA-Z]{2,3}/
@@integer = /\-{,1}(0{,1}|[1-9][0-9]*)/
def self.email?(value)
not not value =~ /\A[A-Za-z0-9][\w\+\.\-]{,200}\@#{@@hostname}\z/
end
def self.phone?(value)
- not not value =~ /\A(((00|\+)[1-9]\d{,2})|0)([\s\-\(\)]{,2}\d){6,11}\z/
+ not not value =~ /\A(((00|\+)[1-9]\d{,2})|0(?!0))([\s\-\(\)]{,2}\d){6,11}\z/
end
def self.hostname?(value)
not not value =~ /\A#{@@hostname}\z/
end
def self.ip_address?(value)
not not value =~ /\A((?<ip_number>0|(1{,1}[0-9]{1,2})|(2[0-5]{2}))\.){3}\g<ip_number>\z/
end
def self.number?(value)
not not value =~ /\A#{@@integer}(\.[0-9]+){,1}\z/
end
def self.integer?(value)
not not value =~ /\A#{@@integer}\z/
end
def self.date?(value)
not not value =~ /\A#{@@date}\z/
end
def self.time?(value)
not not value =~ /\A#{@@time}\z/
end
def self.date_time?(value)
not not value =~ /\A#{@@date}(\ |T)#{@@time}\z/
end
end