Решение на Четвърта задача от Методи Димитров

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

Към профила на Методи Димитров

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 20 успешни тест(а)
  • 21 неуспешни тест(а)

Код

class Filtrations
def preserve_email_hostname=(value)
@preserve_email_hostname = value
end
def partially_preserve_email_username=(value)
@partially_preserve_email_username = value
end
def preserve_phone_country_code=(value)
@preserve_phone_country_code = value
end
def find_phone(text)
/(?<country>(0|((00|\+)[1-9]\d{2})))(?<phone>(([\(\-\)[:blank:]]){,2}[0-9]){6,11})/.match(text)
end
def find_mail(text)
/(?<host>(?<w>[A-Za-z0-9])[\w\+\.\-]{,200})@(\g<w>(\w{,61})\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?/.match(text)
end
def substitute_text(input ,value, key, text, offset)
input[0...value.begin(key)+offset] + text + input[value.end(key)...input.length]
end
end
class PrivacyFilter < Filtrations
def initialize(input)
@original_text = input
end
def filtered()
@filtered = @original_text
filter_mail
filter_phone_without_code
end
def filter_mail
filter_mail_hostname
filter_mail_partial_username
filter_whole_mail
end
def filter_whole_mail()
if mail = find_mail(@filtered)
@filtered = substitute_text(@filtered, mail, 0, "[EMAIL]", 0)
filter_whole_mail
end
end
def filter_mail_hostname()
if mail = find_mail(@filtered) and @preserve_email_hostname
@filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 0)
filter_mail_hostname
end
end
def filter_mail_partial_username()
if mail = find_mail(@filtered) and @partially_preserve_email_username
@filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 3)
filter_mail_partial_username
end
end
def filter_phone_with_code()
if phone = find_phone(@filtered)
@filtered = substitute_text(@filtered, phone, 0, "[PHONE]", 0)
@preserve_phone_country_code ? filter_phone_without_code : filter_phone_with_code
else @filtered end
end
def filter_phone_without_code()
if phone = find_phone(@filtered) and phone["country"].length > 1
@filtered = substitute_text(@filtered, phone, "phone", " [FILTERED]", 0)
filter_phone_without_code
else filter_phone_with_code end
end
end
class Validations
def Validations.email?(value)
if /^(?<w>[A-Za-z0-9])[\w\+\.\-]{,200}@(\g<w>\w{,61}\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?$/ =~ value
true
else
false
end
end
def Validations.phone?(value)
if /^(0|(\+|00)[1-9]\d{2})([\-\(\)[:blank:]]{,2}\d){6,11}$/ =~ value
true
else
false
end
end
def Validations.hostname?(value)
if /^([0-9a-zA-Z][0-9a-zA-Z\-]{,61}?[0-9a-zA-Z]?)*\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$/ =~ value
true
else
false
end
end
def Validations.ip_address?(value)
if /^(?<byte>(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}\g<byte>$/ =~ value
else
false
end
end
def Validations.number?(value)
if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
true
else
false
end
end
def Validations.integer?(value)
if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
true
else
false
end
end
def Validations.date?(value)
if /^\d{4}\-(0[1-9]|1[0-2])\-([012]\d|3[01])$/ =~ value
true
else
false
end
end
def Validations.time?(value)
if /^([01]\d|2[0-3]):([0-5][0-9]):\g<2>$/ =~ value
true
else
false
end
end
def Validations.date_time?(value)
divided_expression = /^([\w\-:]+)[[:blank:]T]([\w\-:]+)$/.match(value)
if divided_expression and self.date?(divided_expression[1]) and self.time?(divided_expression[2]) then true
elsif divided_expression and self.date?(divided_expression[2]) and self.time?(divided_expression[1]) then true
else false end
end
end

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

...F..FFFFFFFF...F.FFFFF...F..F...F.F..FF

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-1gjcvep/spec.rb:52:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1gjcvep/spec.rb:46:in `each'
     # /tmp/d20130203-23049-1gjcvep/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 filters whole email usernames if too short
     Failure/Error: partially_filter_email_usernames('me@example.com').should eq '[FILTERED]@example.com'
       
       expected: "[FILTERED]@example.com"
            got: "me@[FILTERED]@example.com"
       
       (compared using ==)
     # /tmp/d20130203-23049-1gjcvep/spec.rb:69: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 does not brake with unicode
     Failure/Error: partially_filter_email_usernames('За връзка: me@example.com').should eq 'За връзка: [FILTERED]@example.com'
       
       expected: "За връзка: [FILTERED]@example.com"
            got: "За връзка: me@[FILTERED]@example.com"
       
       (compared using ==)
     # /tmp/d20130203-23049-1gjcvep/spec.rb:73: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 filters more complex phone numbers
     Failure/Error: filter(text).should eq filtered
       
       expected: "[PHONE]"
            got: "+155 [FILTERED]"
       
       (compared using ==)
     # /tmp/d20130203-23049-1gjcvep/spec.rb:85:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1gjcvep/spec.rb:84:in `each'
     # /tmp/d20130203-23049-1gjcvep/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)>'

  5) 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-1gjcvep/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1gjcvep/spec.rb:95:in `each'
     # /tmp/d20130203-23049-1gjcvep/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)>'

  6) PrivacyFilter preserves whitespace around phones
     Failure/Error: filter(' +359881212-12-1 2 or...').should eq ' [PHONE] or...'
       
       expected: " [PHONE] or..."
            got: " +359 [FILTERED] or..."
       
       (compared using ==)
     # /tmp/d20130203-23049-1gjcvep/spec.rb:101: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) 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-1gjcvep/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)>'

  8) PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
     Failure/Error: filter.filtered.should eq filtered
       
       expected: "Phone: 0025 [FILTERED]"
            got: "Phone: [PHONE]5"
       
       (compared using ==)
     # /tmp/d20130203-23049-1gjcvep/spec.rb:132:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1gjcvep/spec.rb:129:in `each'
     # /tmp/d20130203-23049-1gjcvep/spec.rb:129: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)>'

  9) PrivacyFilter separates preserved country code from filtered phone with a space
     Failure/Error: filter.filtered.should eq filtered
       
       expected: "Phone: 0025 [FILTERED]"
            got: "Phone: [PHONE]5"
       
       (compared using ==)
     # /tmp/d20130203-23049-1gjcvep/spec.rb:144:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1gjcvep/spec.rb:141:in `each'
     # /tmp/d20130203-23049-1gjcvep/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)>'

  10) 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-1gjcvep/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)>'

  11) 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-1gjcvep/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1gjcvep/spec.rb:209:in `each'
     # /tmp/d20130203-23049-1gjcvep/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)>'

  12) Validations does not break on phones in multiline strings
     Failure/Error: Validations.phone?("0885123123\nwat?").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1gjcvep/spec.rb:215: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)>'

  13) Validations validates hostnames
     Failure/Error: Validations.hostname?('some.long-subdomain.domain.co.ul').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-1gjcvep/spec.rb:220: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)>'

  14) Validations handles multiline strings in hostname validation properly
     Failure/Error: Validations.hostname?("foo.com\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1gjcvep/spec.rb:233: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)>'

  15) Validations validates IP addresses
     Failure/Error: Validations.ip_address?('1.2.3.4').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-1gjcvep/spec.rb:238: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)>'

  16) Validations handles multiline strings in numbers validation properly
     Failure/Error: Validations.number?("42\n24").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1gjcvep/spec.rb:274: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)>'

  17) Validations handles multiline strings in integer validation properly
     Failure/Error: Validations.number?("42\n24").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1gjcvep/spec.rb:295: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)>'

  18) Validations does not allow zero months or days in dates
     Failure/Error: Validations.date?('1000-01-00').should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1gjcvep/spec.rb:316: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)>'

  19) Validations handles newlines in date validation
     Failure/Error: Validations.date?("2012-11-19\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1gjcvep/spec.rb:327: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)>'

  20) Validations validates datetime values
     Failure/Error: Validations.date_time?('2012-01-00T23:59:00').should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1gjcvep/spec.rb:353: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)>'

  21) Validations handles newlines in time and datetime validation
     Failure/Error: Validations.time?("12:01:01\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1gjcvep/spec.rb:360: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.05077 seconds
41 examples, 21 failures

Failed examples:

rspec /tmp/d20130203-23049-1gjcvep/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:68 # PrivacyFilter filters whole email usernames if too short
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:72 # PrivacyFilter does not brake with unicode
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:76 # PrivacyFilter filters more complex phone numbers
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:100 # PrivacyFilter preserves whitespace around phones
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:104 # PrivacyFilter filters more than one phone or email
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:122 # PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:136 # PrivacyFilter separates preserved country code from filtered phone with a space
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:175 # Validations does not break on emails in multiline strings
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:214 # Validations does not break on phones in multiline strings
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:218 # Validations validates hostnames
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:232 # Validations handles multiline strings in hostname validation properly
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:237 # Validations validates IP addresses
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:273 # Validations handles multiline strings in numbers validation properly
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:294 # Validations handles multiline strings in integer validation properly
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:314 # Validations does not allow zero months or days in dates
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:326 # Validations handles newlines in date validation
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:347 # Validations validates datetime values
rspec /tmp/d20130203-23049-1gjcvep/spec.rb:359 # Validations handles newlines in time and datetime validation

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

Методи обнови решението на 27.11.2012 22:34 (преди почти 12 години)

+class Filtrations
+ def preserve_email_hostname=(value)
+ @preserve_email_hostname = value
+ end
+
+ def partially_preserve_email_username=(value)
+ @partially_preserve_email_username = value
+ end
+
+ def preserve_phone_country_code=(value)
+ @preserve_phone_country_code = value
+ end
+
+ def find_phone(text)
+ /(?<country>(0|((00|\+)[1-9]\d{2})))(?<phone>(([\(\-\)[:blank:]]){,2}[0-9]){6,11})/.match(text)
+ end
+
+ def find_mail(text)
+ /(?<host>(?<w>[A-Za-z0-9])[\w\+\.\-]{,200})@(\g<w>(\w{,61})\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?/.match(text)
+ end
+
+ def substitute_text(input ,value, key, text, offset)
+ input[0...value.begin(key)+offset] + text + input[value.end(key)...input.length]
+ end
+end
+
+class PrivacyFilter < Filtrations
+ def initialize(input)
+ @original_text = input
+ end
+
+ def filtered()
+ @filtered = @original_text
+ filter_mail
+ filter_phone_without_code
+ end
+
+ def filter_mail
+ filter_mail_hostname
+ filter_mail_partial_username
+ filter_whole_mail
+ end
+
+ def filter_whole_mail()
+ if mail = find_mail(@filtered)
+ @filtered = substitute_text(@filtered, mail, 0, "[EMAIL]", 0)
+ filter_whole_mail
+ end
+ end
+
+ def filter_mail_hostname()
+ if mail = find_mail(@filtered) and @preserve_email_hostname
+ @filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 0)
+ filter_mail_hostname
+ end
+ end
+
+ def filter_mail_partial_username()
+ if mail = find_mail(@filtered) and @partially_preserve_email_username
+ @filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 3)
+ filter_mail_partial_username
+ end
+ end
+
+ def filter_phone_with_code()
+ if phone = find_phone(@filtered)
+ @filtered = substitute_text(@filtered, phone, 0, "[PHONE]", 0)
+ @preserve_phone_country_code ? filter_phone_without_code : filter_phone_with_code
+ else @filtered end
+ end
+
+ def filter_phone_without_code()
+ if phone = find_phone(@filtered) and phone["country"].length > 1
+ @filtered = substitute_text(@filtered, phone, "phone", " [FILTERED]", 0)
+ filter_phone_without_code
+ else filter_phone_with_code end
+ end
+
+end
+
+class Validations
+
+ def Validations.email?(value)
+ if /^(?<w>[A-Za-z0-9])[\w\+\.\-]{,200}@(\g<w>\w{,61}\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?$/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.phone?(value)
+ if /^(0|(\+|00)[1-9]\d{2})([\-\(\)[:blank:]]{,2}\d){6,11}$/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.hostname?(value)
+ if /^([0-9a-zA-Z][0-9a-zA-Z\-]{,61}?[0-9a-zA-Z]?)*\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.ip_address?(value)
+ if /^((?<byte>1[0-9][0-9]|2[0-5]{2}|[0-9]{,2})\.){3}\g<byte>$/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.number?(value)
+ if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.integer?(value)
+ if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.date?(value)
+ if /^\d{4}\-(0[1-9]|1[0-2])\-([012]\d|3[01])$/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.time?(value)
+ if /^([01]\d|2[0-3]):([0-5][0-9]):\g<2>$/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.date_time?(value)
+ divided_expression = /^([\w\-:]+)[[:blank:]T]([\w\-:]+)$/.match(value)
+ (divided_expression) ? (self.date?(divided_expression[1]) and self.time?(divided_expression[2])) : false
+ end
+
+end

Методи обнови решението на 27.11.2012 22:54 (преди почти 12 години)

class Filtrations
def preserve_email_hostname=(value)
@preserve_email_hostname = value
end
def partially_preserve_email_username=(value)
@partially_preserve_email_username = value
end
def preserve_phone_country_code=(value)
@preserve_phone_country_code = value
end
def find_phone(text)
/(?<country>(0|((00|\+)[1-9]\d{2})))(?<phone>(([\(\-\)[:blank:]]){,2}[0-9]){6,11})/.match(text)
end
def find_mail(text)
/(?<host>(?<w>[A-Za-z0-9])[\w\+\.\-]{,200})@(\g<w>(\w{,61})\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?/.match(text)
end
def substitute_text(input ,value, key, text, offset)
input[0...value.begin(key)+offset] + text + input[value.end(key)...input.length]
end
end
class PrivacyFilter < Filtrations
def initialize(input)
@original_text = input
end
def filtered()
@filtered = @original_text
filter_mail
filter_phone_without_code
end
def filter_mail
filter_mail_hostname
filter_mail_partial_username
filter_whole_mail
end
def filter_whole_mail()
if mail = find_mail(@filtered)
@filtered = substitute_text(@filtered, mail, 0, "[EMAIL]", 0)
filter_whole_mail
end
end
def filter_mail_hostname()
if mail = find_mail(@filtered) and @preserve_email_hostname
@filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 0)
filter_mail_hostname
end
end
def filter_mail_partial_username()
if mail = find_mail(@filtered) and @partially_preserve_email_username
@filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 3)
filter_mail_partial_username
end
end
def filter_phone_with_code()
if phone = find_phone(@filtered)
@filtered = substitute_text(@filtered, phone, 0, "[PHONE]", 0)
@preserve_phone_country_code ? filter_phone_without_code : filter_phone_with_code
else @filtered end
end
def filter_phone_without_code()
if phone = find_phone(@filtered) and phone["country"].length > 1
@filtered = substitute_text(@filtered, phone, "phone", " [FILTERED]", 0)
filter_phone_without_code
else filter_phone_with_code end
end
end
class Validations
def Validations.email?(value)
if /^(?<w>[A-Za-z0-9])[\w\+\.\-]{,200}@(\g<w>\w{,61}\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?$/ =~ value
true
else
false
end
end
def Validations.phone?(value)
if /^(0|(\+|00)[1-9]\d{2})([\-\(\)[:blank:]]{,2}\d){6,11}$/ =~ value
true
else
false
end
end
def Validations.hostname?(value)
if /^([0-9a-zA-Z][0-9a-zA-Z\-]{,61}?[0-9a-zA-Z]?)*\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$/ =~ value
true
else
false
end
end
def Validations.ip_address?(value)
- if /^((?<byte>1[0-9][0-9]|2[0-5]{2}|[0-9]{,2})\.){3}\g<byte>$/ =~ value
+ if /^(?<byte>(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}\g<byte>$/ =~ value
true
else
false
end
end
def Validations.number?(value)
if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
true
else
false
end
end
def Validations.integer?(value)
if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
true
else
false
end
end
def Validations.date?(value)
if /^\d{4}\-(0[1-9]|1[0-2])\-([012]\d|3[01])$/ =~ value
true
else
false
end
end
def Validations.time?(value)
if /^([01]\d|2[0-3]):([0-5][0-9]):\g<2>$/ =~ value
true
else
false
end
end
def Validations.date_time?(value)
divided_expression = /^([\w\-:]+)[[:blank:]T]([\w\-:]+)$/.match(value)
(divided_expression) ? (self.date?(divided_expression[1]) and self.time?(divided_expression[2])) : false
end
end

Методи обнови решението на 28.11.2012 11:02 (преди почти 12 години)

class Filtrations
def preserve_email_hostname=(value)
@preserve_email_hostname = value
end
def partially_preserve_email_username=(value)
@partially_preserve_email_username = value
end
def preserve_phone_country_code=(value)
@preserve_phone_country_code = value
end
def find_phone(text)
/(?<country>(0|((00|\+)[1-9]\d{2})))(?<phone>(([\(\-\)[:blank:]]){,2}[0-9]){6,11})/.match(text)
end
def find_mail(text)
/(?<host>(?<w>[A-Za-z0-9])[\w\+\.\-]{,200})@(\g<w>(\w{,61})\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?/.match(text)
end
def substitute_text(input ,value, key, text, offset)
input[0...value.begin(key)+offset] + text + input[value.end(key)...input.length]
end
end
class PrivacyFilter < Filtrations
def initialize(input)
@original_text = input
end
def filtered()
@filtered = @original_text
filter_mail
filter_phone_without_code
end
def filter_mail
filter_mail_hostname
filter_mail_partial_username
filter_whole_mail
end
def filter_whole_mail()
if mail = find_mail(@filtered)
@filtered = substitute_text(@filtered, mail, 0, "[EMAIL]", 0)
filter_whole_mail
end
end
def filter_mail_hostname()
if mail = find_mail(@filtered) and @preserve_email_hostname
@filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 0)
filter_mail_hostname
end
end
def filter_mail_partial_username()
if mail = find_mail(@filtered) and @partially_preserve_email_username
@filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 3)
filter_mail_partial_username
end
end
def filter_phone_with_code()
if phone = find_phone(@filtered)
@filtered = substitute_text(@filtered, phone, 0, "[PHONE]", 0)
@preserve_phone_country_code ? filter_phone_without_code : filter_phone_with_code
else @filtered end
end
def filter_phone_without_code()
if phone = find_phone(@filtered) and phone["country"].length > 1
@filtered = substitute_text(@filtered, phone, "phone", " [FILTERED]", 0)
filter_phone_without_code
else filter_phone_with_code end
end
end
class Validations
def Validations.email?(value)
if /^(?<w>[A-Za-z0-9])[\w\+\.\-]{,200}@(\g<w>\w{,61}\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?$/ =~ value
true
else
false
end
end
def Validations.phone?(value)
if /^(0|(\+|00)[1-9]\d{2})([\-\(\)[:blank:]]{,2}\d){6,11}$/ =~ value
true
else
false
end
end
def Validations.hostname?(value)
if /^([0-9a-zA-Z][0-9a-zA-Z\-]{,61}?[0-9a-zA-Z]?)*\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$/ =~ value
true
else
false
end
end
def Validations.ip_address?(value)
if /^(?<byte>(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}\g<byte>$/ =~ value
- true
else
false
end
end
def Validations.number?(value)
if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
true
else
false
end
end
def Validations.integer?(value)
if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
true
else
false
end
end
def Validations.date?(value)
if /^\d{4}\-(0[1-9]|1[0-2])\-([012]\d|3[01])$/ =~ value
true
else
false
end
end
def Validations.time?(value)
if /^([01]\d|2[0-3]):([0-5][0-9]):\g<2>$/ =~ value
true
else
false
end
end
def Validations.date_time?(value)
divided_expression = /^([\w\-:]+)[[:blank:]T]([\w\-:]+)$/.match(value)
(divided_expression) ? (self.date?(divided_expression[1]) and self.time?(divided_expression[2])) : false
end
-end
+end

Методи обнови решението на 28.11.2012 13:20 (преди почти 12 години)

class Filtrations
def preserve_email_hostname=(value)
@preserve_email_hostname = value
end
def partially_preserve_email_username=(value)
@partially_preserve_email_username = value
end
def preserve_phone_country_code=(value)
@preserve_phone_country_code = value
end
def find_phone(text)
/(?<country>(0|((00|\+)[1-9]\d{2})))(?<phone>(([\(\-\)[:blank:]]){,2}[0-9]){6,11})/.match(text)
end
def find_mail(text)
/(?<host>(?<w>[A-Za-z0-9])[\w\+\.\-]{,200})@(\g<w>(\w{,61})\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?/.match(text)
end
def substitute_text(input ,value, key, text, offset)
input[0...value.begin(key)+offset] + text + input[value.end(key)...input.length]
end
end
class PrivacyFilter < Filtrations
def initialize(input)
@original_text = input
end
def filtered()
@filtered = @original_text
filter_mail
filter_phone_without_code
end
def filter_mail
filter_mail_hostname
filter_mail_partial_username
filter_whole_mail
end
def filter_whole_mail()
if mail = find_mail(@filtered)
@filtered = substitute_text(@filtered, mail, 0, "[EMAIL]", 0)
filter_whole_mail
end
end
def filter_mail_hostname()
if mail = find_mail(@filtered) and @preserve_email_hostname
@filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 0)
filter_mail_hostname
end
end
def filter_mail_partial_username()
if mail = find_mail(@filtered) and @partially_preserve_email_username
@filtered = substitute_text(@filtered, mail, "host", "[FILTERED]", 3)
filter_mail_partial_username
end
end
def filter_phone_with_code()
if phone = find_phone(@filtered)
@filtered = substitute_text(@filtered, phone, 0, "[PHONE]", 0)
@preserve_phone_country_code ? filter_phone_without_code : filter_phone_with_code
else @filtered end
end
def filter_phone_without_code()
if phone = find_phone(@filtered) and phone["country"].length > 1
@filtered = substitute_text(@filtered, phone, "phone", " [FILTERED]", 0)
filter_phone_without_code
else filter_phone_with_code end
end
end
class Validations
def Validations.email?(value)
if /^(?<w>[A-Za-z0-9])[\w\+\.\-]{,200}@(\g<w>\w{,61}\g<w>?\.)+(?<j>[A-Za-z]){2,3}(\.\g<j>{2})?$/ =~ value
true
else
false
end
end
def Validations.phone?(value)
if /^(0|(\+|00)[1-9]\d{2})([\-\(\)[:blank:]]{,2}\d){6,11}$/ =~ value
true
else
false
end
end
def Validations.hostname?(value)
if /^([0-9a-zA-Z][0-9a-zA-Z\-]{,61}?[0-9a-zA-Z]?)*\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$/ =~ value
true
else
false
end
end
def Validations.ip_address?(value)
if /^(?<byte>(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}\g<byte>$/ =~ value
else
false
end
end
def Validations.number?(value)
if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
true
else
false
end
end
def Validations.integer?(value)
if /^\-?(0|([1-9]\d*))(\.\d+)?$/ =~ value
true
else
false
end
end
def Validations.date?(value)
if /^\d{4}\-(0[1-9]|1[0-2])\-([012]\d|3[01])$/ =~ value
true
else
false
end
end
def Validations.time?(value)
if /^([01]\d|2[0-3]):([0-5][0-9]):\g<2>$/ =~ value
true
else
false
end
end
def Validations.date_time?(value)
divided_expression = /^([\w\-:]+)[[:blank:]T]([\w\-:]+)$/.match(value)
- (divided_expression) ? (self.date?(divided_expression[1]) and self.time?(divided_expression[2])) : false
+ if divided_expression and self.date?(divided_expression[1]) and self.time?(divided_expression[2]) then true
+ elsif divided_expression and self.date?(divided_expression[2]) and self.time?(divided_expression[1]) then true
+ else false end
end
-end
+end