Решение на Четвърта задача от Никола Таушанов

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

Към профила на Никола Таушанов

Резултати

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

Код

module ValidationExpressions
PHONE = '((?<=[^a-zA-Z0-9\+]|\A)0|(?<prefix>(00|\+)[1-9]\d{,2}))([ \(\)-]{,2}\d){6,11}'
IP_ADDRESS = '((\d{1,2}|(1\d{2}|2([0-4]\d|5[0-5])))\.){3}\g<2>'
INTEGER = '-?(0|[1-9]\d*)'
NUMBER = INTEGER + '(\.\d+)?'
DATE = '\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])'
TIME = '([01]\d|2[0-3])\:(?<sec>[0-5]\d)\:\g<sec>'
DATE_TIME = "#{DATE}[ T]#{TIME}"
HOSTNAME = '(?<let>[a-zA-Z0-9])((\.\g<let>?|\-)?\g<let>){,30}\g<let>?\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
EMAIL_USERNAME = '(\b((?<partial>(?<n>[a-zA-Z0-9])(?<nws>[\w\+\.-]){2})\g<nws>{3,198}|\g<n>\g<nws>{,4}))'
FULL_EMAIL = "#{EMAIL_USERNAME}@#{HOSTNAME}"
end
class PrivacyFilter
include ValidationExpressions
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :partially_preserve_email_username
def initialize(text)
@text = text
end
def filtered
filter_phone filter_email @text
end
private
def filter_phone(text)
text.gsub /#{PHONE}/ do
if preserve_phone_country_code && !$~['prefix'].nil?
"#{$~['prefix']} [FILTERED]"
else
'[PHONE]'
end
end
end
def filter_email(text)
text.gsub /#{email_expression}/, email_filtered_message
end
def email_expression
if @partially_preserve_email_username or @preserve_email_hostname
"#{EMAIL_USERNAME}(?=@#{HOSTNAME})"
else
FULL_EMAIL
end
end
def email_filtered_message
if @partially_preserve_email_username then '\k<partial>[FILTERED]'
elsif @preserve_email_hostname then '[FILTERED]'
else '[EMAIL]'
end
end
end
class Validations
include ValidationExpressions
def self.phone?(value)
self.match_expression PHONE, value
end
def self.ip_address?(value)
self.match_expression IP_ADDRESS, value
end
def self.number?(value)
self.match_expression NUMBER, value
end
def self.integer?(value)
self.match_expression INTEGER, value
end
def self.date?(value)
self.match_expression DATE, value
end
def self.time?(value)
self.match_expression TIME, value
end
def self.date_time?(value)
self.match_expression DATE_TIME, value
end
def self.email?(value)
self.match_expression FULL_EMAIL, value
end
def self.hostname?(value)
self.match_expression HOSTNAME, value
end
protected
def self.match_expression(expression, value)
not /\A#{expression}\z/.match(value).nil?
end
end

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

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

Failures:

  1) PrivacyFilter does not filter invalid emails
     Failure/Error: filter(text_with_invalid_emails).should eq text_with_invalid_emails
       
       expected: "And more: someone@invalid.domaintld"
            got: "And more: [EMAIL]aintld"
       
       (compared using ==)
     # /tmp/d20130203-23049-16mnj42/spec.rb:52:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-16mnj42/spec.rb:46:in `each'
     # /tmp/d20130203-23049-16mnj42/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-16mnj42/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-16mnj42/spec.rb:95:in `each'
     # /tmp/d20130203-23049-16mnj42/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 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-16mnj42/spec.rb:132:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-16mnj42/spec.rb:129:in `each'
     # /tmp/d20130203-23049-16mnj42/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)>'

  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: [PHONE]5"
       
       (compared using ==)
     # /tmp/d20130203-23049-16mnj42/spec.rb:144:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-16mnj42/spec.rb:141:in `each'
     # /tmp/d20130203-23049-16mnj42/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 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-16mnj42/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-16mnj42/spec.rb:209:in `each'
     # /tmp/d20130203-23049-16mnj42/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)>'

Finished in 0.06881 seconds
41 examples, 5 failures

Failed examples:

rspec /tmp/d20130203-23049-16mnj42/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-16mnj42/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-16mnj42/spec.rb:122 # PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
rspec /tmp/d20130203-23049-16mnj42/spec.rb:136 # PrivacyFilter separates preserved country code from filtered phone with a space
rspec /tmp/d20130203-23049-16mnj42/spec.rb:184 # Validations can validate more complex phone numbers

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

Никола обнови решението на 27.11.2012 23:33 (преди почти 12 години)

+module ValidationExpressions
+ PHONE = '((?<=[^a-zA-Z0-9\+]|\A)0|(?<prefix>(00|\+)[1-9]\d{,2}))([ \(\)-]{,2}\d){6,11}'
+ IP_ADDRESS = '((\d{1,2}|(1\d{2}|2([0-4]\d|5[0-5])))\.){3}\g<2>'
+ INTEGER = '-?(0|[1-9]\d*)'
+ NUMBER = INTEGER + '(\.\d+)?'
+ DATE = '\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])'
+ TIME = '([01]\d|2[0-3])\:(?<sec>[0-5]\d)\:\g<sec>'
+ DATE_TIME = "#{DATE}[ T]#{TIME}"
+ HOSTNAME = '((?<let>[a-zA-Z0-9])((\g<let>|-){,61}\g<let>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+ EMAIL_USERNAME = '(\b((?<partial>(?<n>[a-zA-Z0-9])(?<nws>[\w\+\.-]){2})\g<nws>{3,198}|\g<n>\g<nws>{,4}))'
+ FULL_EMAIL = "#{EMAIL_USERNAME}@#{HOSTNAME}"
+end
+
+class PrivacyFilter
+ include ValidationExpressions
+
+ attr_accessor :preserve_phone_country_code
+ attr_accessor :preserve_email_hostname
+ attr_accessor :partially_preserve_email_username
+
+ def initialize(text)
+ @text = text
+ end
+
+ def filtered
+ filter_phone filter_email @text
+ end
+
+ private
+
+ def filter_phone(text)
+ text.gsub /#{PHONE}/ do
+ if preserve_phone_country_code && !$~['prefix'].nil?
+ "#{$~['prefix']} [FILTERED]"
+ else
+ '[PHONE]'
+ end
+ end
+ end
+
+ def filter_email(text)
+ text.gsub /#{email_expression}/, email_filtered_message
+ end
+
+ def email_expression
+ if @partially_preserve_email_username or @preserve_email_hostname
+ "#{EMAIL_USERNAME}(?=@#{HOSTNAME})"
+ else
+ FULL_EMAIL
+ end
+ end
+
+ def email_filtered_message
+ if @partially_preserve_email_username then '\k<partial>[FILTERED]'
+ elsif @preserve_email_hostname then '[FILTERED]'
+ else '[EMAIL]'
+ end
+ end
+end
+
+
+class Validations
+ include ValidationExpressions
+
+ def self.phone?(value)
+ self.match_expression PHONE, value
+ end
+
+ def self.ip_address?(value)
+ self.match_expression IP_ADDRESS, value
+ end
+
+ def self.number?(value)
+ self.match_expression NUMBER, value
+ end
+
+ def self.integer?(value)
+ self.match_expression INTEGER, value
+ end
+
+ def self.date?(value)
+ self.match_expression DATE, value
+ end
+
+ def self.time?(value)
+ self.match_expression TIME, value
+ end
+
+ def self.date_time?(value)
+ self.match_expression DATE_TIME, value
+ end
+
+ def self.email?(value)
+ self.match_expression FULL_EMAIL, value
+ end
+
+ def self.hostname?(value)
+ self.match_expression HOSTNAME, value
+ end
+
+ protected
+
+ def self.match_expression(expression, value)
+ not /\A#{expression}\z/.match(value).nil?
+ end
+end

Никола обнови решението на 27.11.2012 23:34 (преди почти 12 години)

module ValidationExpressions
PHONE = '((?<=[^a-zA-Z0-9\+]|\A)0|(?<prefix>(00|\+)[1-9]\d{,2}))([ \(\)-]{,2}\d){6,11}'
IP_ADDRESS = '((\d{1,2}|(1\d{2}|2([0-4]\d|5[0-5])))\.){3}\g<2>'
INTEGER = '-?(0|[1-9]\d*)'
NUMBER = INTEGER + '(\.\d+)?'
DATE = '\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])'
TIME = '([01]\d|2[0-3])\:(?<sec>[0-5]\d)\:\g<sec>'
DATE_TIME = "#{DATE}[ T]#{TIME}"
HOSTNAME = '((?<let>[a-zA-Z0-9])((\g<let>|-){,61}\g<let>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
EMAIL_USERNAME = '(\b((?<partial>(?<n>[a-zA-Z0-9])(?<nws>[\w\+\.-]){2})\g<nws>{3,198}|\g<n>\g<nws>{,4}))'
FULL_EMAIL = "#{EMAIL_USERNAME}@#{HOSTNAME}"
end
class PrivacyFilter
include ValidationExpressions
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :partially_preserve_email_username
def initialize(text)
@text = text
end
def filtered
filter_phone filter_email @text
end
private
def filter_phone(text)
text.gsub /#{PHONE}/ do
if preserve_phone_country_code && !$~['prefix'].nil?
"#{$~['prefix']} [FILTERED]"
else
'[PHONE]'
end
end
end
def filter_email(text)
text.gsub /#{email_expression}/, email_filtered_message
end
def email_expression
- if @partially_preserve_email_username or @preserve_email_hostname
+ if @partially_preserve_email_username or @preserve_email_hostname
"#{EMAIL_USERNAME}(?=@#{HOSTNAME})"
else
FULL_EMAIL
end
end
def email_filtered_message
if @partially_preserve_email_username then '\k<partial>[FILTERED]'
elsif @preserve_email_hostname then '[FILTERED]'
else '[EMAIL]'
end
end
end
class Validations
include ValidationExpressions
def self.phone?(value)
self.match_expression PHONE, value
end
def self.ip_address?(value)
self.match_expression IP_ADDRESS, value
end
def self.number?(value)
self.match_expression NUMBER, value
end
def self.integer?(value)
self.match_expression INTEGER, value
end
def self.date?(value)
self.match_expression DATE, value
end
def self.time?(value)
self.match_expression TIME, value
end
def self.date_time?(value)
self.match_expression DATE_TIME, value
end
def self.email?(value)
self.match_expression FULL_EMAIL, value
end
def self.hostname?(value)
self.match_expression HOSTNAME, value
end
protected
def self.match_expression(expression, value)
not /\A#{expression}\z/.match(value).nil?
end
end

Никола обнови решението на 28.11.2012 02:05 (преди почти 12 години)

module ValidationExpressions
PHONE = '((?<=[^a-zA-Z0-9\+]|\A)0|(?<prefix>(00|\+)[1-9]\d{,2}))([ \(\)-]{,2}\d){6,11}'
IP_ADDRESS = '((\d{1,2}|(1\d{2}|2([0-4]\d|5[0-5])))\.){3}\g<2>'
INTEGER = '-?(0|[1-9]\d*)'
NUMBER = INTEGER + '(\.\d+)?'
DATE = '\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])'
TIME = '([01]\d|2[0-3])\:(?<sec>[0-5]\d)\:\g<sec>'
DATE_TIME = "#{DATE}[ T]#{TIME}"
- HOSTNAME = '((?<let>[a-zA-Z0-9])((\g<let>|-){,61}\g<let>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+ # HOSTNAME = '((?<let>[a-zA-Z0-9])((\g<let>|-){,61}\g<let>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+ HOSTNAME = '(?<let>[a-zA-Z0-9])((\.\g<let>?|\-)?\g<let>){,30}\g<let>?\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
EMAIL_USERNAME = '(\b((?<partial>(?<n>[a-zA-Z0-9])(?<nws>[\w\+\.-]){2})\g<nws>{3,198}|\g<n>\g<nws>{,4}))'
FULL_EMAIL = "#{EMAIL_USERNAME}@#{HOSTNAME}"
end
class PrivacyFilter
include ValidationExpressions
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :partially_preserve_email_username
def initialize(text)
@text = text
end
def filtered
filter_phone filter_email @text
end
private
def filter_phone(text)
text.gsub /#{PHONE}/ do
if preserve_phone_country_code && !$~['prefix'].nil?
"#{$~['prefix']} [FILTERED]"
else
'[PHONE]'
end
end
end
def filter_email(text)
text.gsub /#{email_expression}/, email_filtered_message
end
def email_expression
if @partially_preserve_email_username or @preserve_email_hostname
"#{EMAIL_USERNAME}(?=@#{HOSTNAME})"
else
FULL_EMAIL
end
end
def email_filtered_message
if @partially_preserve_email_username then '\k<partial>[FILTERED]'
elsif @preserve_email_hostname then '[FILTERED]'
else '[EMAIL]'
end
end
end
class Validations
include ValidationExpressions
def self.phone?(value)
self.match_expression PHONE, value
end
def self.ip_address?(value)
self.match_expression IP_ADDRESS, value
end
def self.number?(value)
self.match_expression NUMBER, value
end
def self.integer?(value)
self.match_expression INTEGER, value
end
def self.date?(value)
self.match_expression DATE, value
end
def self.time?(value)
self.match_expression TIME, value
end
def self.date_time?(value)
self.match_expression DATE_TIME, value
end
def self.email?(value)
self.match_expression FULL_EMAIL, value
end
def self.hostname?(value)
self.match_expression HOSTNAME, value
end
protected
def self.match_expression(expression, value)
not /\A#{expression}\z/.match(value).nil?
end
end

Никола обнови решението на 28.11.2012 02:07 (преди почти 12 години)

module ValidationExpressions
- PHONE = '((?<=[^a-zA-Z0-9\+]|\A)0|(?<prefix>(00|\+)[1-9]\d{,2}))([ \(\)-]{,2}\d){6,11}'
- IP_ADDRESS = '((\d{1,2}|(1\d{2}|2([0-4]\d|5[0-5])))\.){3}\g<2>'
- INTEGER = '-?(0|[1-9]\d*)'
- NUMBER = INTEGER + '(\.\d+)?'
- DATE = '\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])'
- TIME = '([01]\d|2[0-3])\:(?<sec>[0-5]\d)\:\g<sec>'
- DATE_TIME = "#{DATE}[ T]#{TIME}"
- # HOSTNAME = '((?<let>[a-zA-Z0-9])((\g<let>|-){,61}\g<let>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
- HOSTNAME = '(?<let>[a-zA-Z0-9])((\.\g<let>?|\-)?\g<let>){,30}\g<let>?\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
- EMAIL_USERNAME = '(\b((?<partial>(?<n>[a-zA-Z0-9])(?<nws>[\w\+\.-]){2})\g<nws>{3,198}|\g<n>\g<nws>{,4}))'
- FULL_EMAIL = "#{EMAIL_USERNAME}@#{HOSTNAME}"
+ PHONE = '((?<=[^a-zA-Z0-9\+]|\A)0|(?<prefix>(00|\+)[1-9]\d{,2}))([ \(\)-]{,2}\d){6,11}'
+ IP_ADDRESS = '((\d{1,2}|(1\d{2}|2([0-4]\d|5[0-5])))\.){3}\g<2>'
+ INTEGER = '-?(0|[1-9]\d*)'
+ NUMBER = INTEGER + '(\.\d+)?'
+ DATE = '\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])'
+ TIME = '([01]\d|2[0-3])\:(?<sec>[0-5]\d)\:\g<sec>'
+ DATE_TIME = "#{DATE}[ T]#{TIME}"
+ HOSTNAME = '(?<let>[a-zA-Z0-9])((\.\g<let>?|\-)?\g<let>){,30}\g<let>?\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+ EMAIL_USERNAME = '(\b((?<partial>(?<n>[a-zA-Z0-9])(?<nws>[\w\+\.-]){2})\g<nws>{3,198}|\g<n>\g<nws>{,4}))'
+ FULL_EMAIL = "#{EMAIL_USERNAME}@#{HOSTNAME}"
end
class PrivacyFilter
include ValidationExpressions
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :partially_preserve_email_username
def initialize(text)
@text = text
end
def filtered
filter_phone filter_email @text
end
private
def filter_phone(text)
text.gsub /#{PHONE}/ do
if preserve_phone_country_code && !$~['prefix'].nil?
"#{$~['prefix']} [FILTERED]"
else
'[PHONE]'
end
end
end
def filter_email(text)
text.gsub /#{email_expression}/, email_filtered_message
end
def email_expression
if @partially_preserve_email_username or @preserve_email_hostname
"#{EMAIL_USERNAME}(?=@#{HOSTNAME})"
else
FULL_EMAIL
end
end
def email_filtered_message
if @partially_preserve_email_username then '\k<partial>[FILTERED]'
elsif @preserve_email_hostname then '[FILTERED]'
else '[EMAIL]'
end
end
end
class Validations
include ValidationExpressions
def self.phone?(value)
self.match_expression PHONE, value
end
def self.ip_address?(value)
self.match_expression IP_ADDRESS, value
end
def self.number?(value)
self.match_expression NUMBER, value
end
def self.integer?(value)
self.match_expression INTEGER, value
end
def self.date?(value)
self.match_expression DATE, value
end
def self.time?(value)
self.match_expression TIME, value
end
def self.date_time?(value)
self.match_expression DATE_TIME, value
end
def self.email?(value)
self.match_expression FULL_EMAIL, value
end
def self.hostname?(value)
self.match_expression HOSTNAME, value
end
protected
def self.match_expression(expression, value)
not /\A#{expression}\z/.match(value).nil?
end
end