Решение на Четвърта задача от Иван Георгиев

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

Към профила на Иван Георгиев

Резултати

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

Код

class PrivacyFilter
attr_accessor :text, :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
PHONE_PATTERN = /(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d([- \(\)]?)\6?)\g<5>{6,9}\d\b/
EMAIL_PATTERN = /\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
def initialize(text)
@text = text
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially_preserve_email_username = false
end
def filtered
if Validations.email?(@text) and PHONE_PATTERN =~ @text then filter_phone(filter_email(@text))
elsif Validations.email?(@text) then filter_email(@text)
elsif PHONE_PATTERN =~ @text then filter_phone(@text)
else @text
end
end
def filter_email(text)
if preserve_email_hostname and EMAIL_PATTERN.match(text) then text.gsub(EMAIL_PATTERN, '[FILTERED]'+$3)
elsif partially_preserve_email_username and EMAIL_PATTERN.match(text) then filter_email_partially(text)
else text.gsub(EMAIL_PATTERN, '[EMAIL]')
end
end
def filter_email_partially(text)
if partially_preserve_email_username and EMAIL_PATTERN.match(text)
if $1.to_s.length >= 6 then text.gsub(EMAIL_PATTERN, $2+'[FILTERED]'+$3)
else text.gsub(EMAIL_PATTERN, '[FILTERED]'+$3)
end
end
end
def filter_phone(text)
if /((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d([- \(\)]?)\5?)\g<4>{6,9}\d\b/ =~ text and preserve_phone_country_code
text1 = text.gsub(/((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d([- \(\)]?)\5?)\g<4>{6,9}\d\b/, $1+' [FILTERED]')
return text1.gsub(/0(([- \(\)])?\2?\d\2{,2})\g<1>{4,9}\d/, '[PHONE]')
else text.gsub(PHONE_PATTERN, '[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([- \(\)]?)\6?)\g<5>{6,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\d{4}-(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\d{4}-(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

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

........FF.F.F..FF.F.........F...........

Failures:

  1) PrivacyFilter filters more complex phone numbers
     Failure/Error: filter(text).should eq filtered
       
       expected: "[PHONE]"
            got: "+1 (555) 123-456-99"
       
       (compared using ==)
     # /tmp/d20130203-23049-l85s2j/spec.rb:85:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-l85s2j/spec.rb:84:in `each'
     # /tmp/d20130203-23049-l85s2j/spec.rb:84: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: "0005551234569"
            got: "0[PHONE]"
       
       (compared using ==)
     # /tmp/d20130203-23049-l85s2j/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-l85s2j/spec.rb:95:in `each'
     # /tmp/d20130203-23049-l85s2j/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) PrivacyFilter filters more than one phone or email
     Failure/Error: filter(text).should eq filtered
       
       expected: "\n      Contacts\n\n      Phones: [PHONE] or [PHONE]\n      Email: [EMAIL] or [EMAIL]\n    "
            got: "\n      Contacts\n\n      Phones: +1 (555) 123-456-99 or [PHONE]\n      Email: [EMAIL] or [EMAIL]\n    "
       
       (compared using ==)
       
       Diff:
       @@ -1,7 +1,7 @@
        
              Contacts
        
       -      Phones: [PHONE] or [PHONE]
       +      Phones: +1 (555) 123-456-99 or [PHONE]
              Email: [EMAIL] or [EMAIL]
            
     # /tmp/d20130203-23049-l85s2j/spec.rb:119: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) PrivacyFilter separates preserved country code from filtered phone with a space
     Failure/Error: filter.filtered.should eq filtered
       
       expected: "Phone: 0025 [FILTERED]"
            got: "Phone: 0025 (55) 12 12255"
       
       (compared using ==)
     # /tmp/d20130203-23049-l85s2j/spec.rb:144:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-l85s2j/spec.rb:141:in `each'
     # /tmp/d20130203-23049-l85s2j/spec.rb:141: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)>'

  5) Validations can validate more complex emails
     Failure/Error: Validations.email?(email).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-l85s2j/spec.rb:171:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-l85s2j/spec.rb:170:in `each'
     # /tmp/d20130203-23049-l85s2j/spec.rb:170: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)>'

  6) Validations does not break on emails in multiline strings
     Failure/Error: Validations.email?("foo@bar.com\nwat?").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-l85s2j/spec.rb:176: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)>'

  7) Validations can validate more complex phone numbers
     Failure/Error: Validations.phone?(phone).should be(valid)
       
       expected #<TrueClass:2> => true
            got #<FalseClass:0> => false
       
       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-l85s2j/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-l85s2j/spec.rb:209:in `each'
     # /tmp/d20130203-23049-l85s2j/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)>'

  8) Validations validates more complex integers
     Failure/Error: Validations.integer?('-0').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-l85s2j/spec.rb:289: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.05256 seconds
41 examples, 8 failures

Failed examples:

rspec /tmp/d20130203-23049-l85s2j/spec.rb:76 # PrivacyFilter filters more complex phone numbers
rspec /tmp/d20130203-23049-l85s2j/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-l85s2j/spec.rb:104 # PrivacyFilter filters more than one phone or email
rspec /tmp/d20130203-23049-l85s2j/spec.rb:136 # PrivacyFilter separates preserved country code from filtered phone with a space
rspec /tmp/d20130203-23049-l85s2j/spec.rb:160 # Validations can validate more complex emails
rspec /tmp/d20130203-23049-l85s2j/spec.rb:175 # Validations does not break on emails in multiline strings
rspec /tmp/d20130203-23049-l85s2j/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-l85s2j/spec.rb:283 # Validations validates more complex integers

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

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

Иван обнови решението на 26.11.2012 13:53 (преди почти 12 години)

class PrivacyFilter
attr_accessor :text, :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+ PhonePattern = /(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d([- \(\)]?)\6?)\g<5>{6,9}\d\b/
+ EmailPattern = /\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
+
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
+ if Validations.email?(@text) and PhonePattern =~ @text then filter_phone(filter_email(@text))
+ elsif Validations.email?(@text) then filter_email(@text)
+ elsif PhonePattern =~ @text then filter_phone(@text)
+ else @text
+ end
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]')
+ if preserve_email_hostname and EmailPattern.match(text) then text.gsub(EmailPattern, '[FILTERED]'+$3)
+ elsif partially_preserve_email_username and EmailPattern.match(text) then filter_email_partially(text)
+ else text.gsub(EmailPattern, '[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)
+ def filter_email_partially(text)
+ if partially_preserve_email_username and EmailPattern.match(text)
+ if $1.to_s.length >= 6 then text.gsub(EmailPattern, $2+'[FILTERED]'+$3)
+ else text.gsub(EmailPattern, '[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]')
+ if /((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d([- \(\)]?)\5?)\g<4>{6,9}\d\b/ =~ text and preserve_phone_country_code
+ text1 = text.gsub(/((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d([- \(\)]?)\5?)\g<4>{6,9}\d\b/, $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]')
+ else text.gsub(PhonePattern, '[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
+ return true if /\A(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d([- \(\)]?)\6?)\g<5>{6,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

Иван обнови решението на 26.11.2012 14:05 (преди почти 12 години)

class PrivacyFilter
attr_accessor :text, :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
PhonePattern = /(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d([- \(\)]?)\6?)\g<5>{6,9}\d\b/
EmailPattern = /\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
def initialize(text)
@text = text
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially_preserve_email_username = false
end
def filtered
if Validations.email?(@text) and PhonePattern =~ @text then filter_phone(filter_email(@text))
elsif Validations.email?(@text) then filter_email(@text)
elsif PhonePattern =~ @text then filter_phone(@text)
else @text
end
end
def filter_email(text)
if preserve_email_hostname and EmailPattern.match(text) then text.gsub(EmailPattern, '[FILTERED]'+$3)
elsif partially_preserve_email_username and EmailPattern.match(text) then filter_email_partially(text)
else text.gsub(EmailPattern, '[EMAIL]')
end
end
def filter_email_partially(text)
if partially_preserve_email_username and EmailPattern.match(text)
if $1.to_s.length >= 6 then text.gsub(EmailPattern, $2+'[FILTERED]'+$3)
else text.gsub(EmailPattern, '[FILTERED]'+$3)
end
end
end
def filter_phone(text)
if /((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d([- \(\)]?)\5?)\g<4>{6,9}\d\b/ =~ text and preserve_phone_country_code
text1 = text.gsub(/((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d([- \(\)]?)\5?)\g<4>{6,9}\d\b/, $1+' [FILTERED]')
return text1.gsub(/0(([- \(\)])?\2?\d\2{,2})\g<1>{4,9}\d/, '[PHONE]')
else text.gsub(PhonePattern, '[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([- \(\)]?)\6?)\g<5>{6,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
+ return true if /\A\d{4}-(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
+ return true if /\A\d{4}-(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

Иван обнови решението на 26.11.2012 16:34 (преди почти 12 години)

class PrivacyFilter
attr_accessor :text, :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
- PhonePattern = /(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d([- \(\)]?)\6?)\g<5>{6,9}\d\b/
- EmailPattern = /\b(([[:alnum:]][\w\+\.\-]{,2})[\w\+\.\-]{,198})(@([[:alnum:]]
+ PHONE_PATTERN = /(0|((0{2}|\+)[1-9]\d{,2}))([- \(\)])?\4?(\d([- \(\)]?)\6?)\g<5>{6,9}\d\b/
+ EMAIL_PATTERN = /\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
def initialize(text)
@text = text
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially_preserve_email_username = false
end
def filtered
- if Validations.email?(@text) and PhonePattern =~ @text then filter_phone(filter_email(@text))
+ if Validations.email?(@text) and PHONE_PATTERN =~ @text then filter_phone(filter_email(@text))
elsif Validations.email?(@text) then filter_email(@text)
- elsif PhonePattern =~ @text then filter_phone(@text)
+ elsif PHONE_PATTERN =~ @text then filter_phone(@text)
else @text
end
end
def filter_email(text)
- if preserve_email_hostname and EmailPattern.match(text) then text.gsub(EmailPattern, '[FILTERED]'+$3)
- elsif partially_preserve_email_username and EmailPattern.match(text) then filter_email_partially(text)
- else text.gsub(EmailPattern, '[EMAIL]')
+ if preserve_email_hostname and EMAIL_PATTERN.match(text) then text.gsub(EMAIL_PATTERN, '[FILTERED]'+$3)
+ elsif partially_preserve_email_username and EMAIL_PATTERN.match(text) then filter_email_partially(text)
+ else text.gsub(EMAIL_PATTERN, '[EMAIL]')
end
end
def filter_email_partially(text)
- if partially_preserve_email_username and EmailPattern.match(text)
- if $1.to_s.length >= 6 then text.gsub(EmailPattern, $2+'[FILTERED]'+$3)
- else text.gsub(EmailPattern, '[FILTERED]'+$3)
+ if partially_preserve_email_username and EMAIL_PATTERN.match(text)
+ if $1.to_s.length >= 6 then text.gsub(EMAIL_PATTERN, $2+'[FILTERED]'+$3)
+ else text.gsub(EMAIL_PATTERN, '[FILTERED]'+$3)
end
end
end
def filter_phone(text)
if /((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d([- \(\)]?)\5?)\g<4>{6,9}\d\b/ =~ text and preserve_phone_country_code
text1 = text.gsub(/((\+|0{2})[1-9]\d{,2})([- \(\)])?\3?(\d([- \(\)]?)\5?)\g<4>{6,9}\d\b/, $1+' [FILTERED]')
return text1.gsub(/0(([- \(\)])?\2?\d\2{,2})\g<1>{4,9}\d/, '[PHONE]')
- else text.gsub(PhonePattern, '[PHONE]')
+ else text.gsub(PHONE_PATTERN, '[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([- \(\)]?)\6?)\g<5>{6,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\d{4}-(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\d{4}-(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