Решение на Четвърта задача от Емил Гоцев

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

Към профила на Емил Гоцев

Резултати

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

Код

class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@email_regexp = /\b(([a-zA-Z0-9])[\w\+\.-]{,200})@((\g<2>((\g<2>|-){,61}\g<2>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\b/
@phone_regexp = /((\b00|\+)[1-9]\d{0,2}|\b0)([- \(\)]{,2}\d)\g<3>{5,10}\b/
end
def filtered
result = filter_emails @text
result = filter_phone_numbers result
end
private
def filter_emails(text)
text.gsub(@email_regexp) do |match|
if !preserve_email_hostname and !partially_preserve_email_username
'[EMAIL]'
else
preserve_email_components match
end
end
end
def filter_phone_numbers(text)
text.gsub(@phone_regexp) do |match|
if !preserve_phone_country_code
"[PHONE]"
else
preserve_phone_components $1, match
end
end
end
def preserve_email_components(text)
username, hostname = text.split('@')
return "[FILTERED]@#{hostname}" if username.size < 6
return "#{username[0,3]}[FILTERED]@#{hostname}" if partially_preserve_email_username
return "[FILTERED]@#{hostname}" if preserve_email_hostname
end
def preserve_phone_components(prefix, phone)
if prefix == '0'
"[PHONE]"
else
"#{prefix} [FILTERED]"
end
end
end
class Validations
def self.email?(value)
case value
when /\A([a-zA-Z0-9])[\w\+\.-]{,200}@(\g<1>([0-9a-zA-Z-]{,61}\g<1>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\z/
true
else
false
end
end
def self.phone?(value)
/\A((00|\+)[1-9]\d{0,2}|0)([- \(\)]{,2}\d)\g<3>{5,10}\z/ =~ value ? true : false
end
def self.hostname?(value)
/\A([0-9a-zA-Z]([0-9a-zA-Z-]{,61}[0-9a-zA-Z])?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\z/ =~ value ? true : false
end
def self.ip_address?(value)
/\A([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.\g<1>){3}\z/ =~ value ? true : false
end
def self.number?(value)
/\A-?(0|[1-9][0-9]*)(\.[0-9]+)?\z/ =~ value ? true : false
end
def self.integer?(value)
/\A-?(0|[1-9][0-9]*)\z/ =~ value ? true : false
end
def self.date?(value)
/\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[1-2][0-9]|3[01])\z/ =~ value ? true : false
end
def self.time?(value)
/\A(0[1-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\z/ =~ value ? true : false
end
def self.date_time?(value)
case value
when /\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[1-2][0-9]|3[01])( |T)(0[1-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\z/
true
else
false
end
end
end

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

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

Failures:

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

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

  3) Validations validates times
     Failure/Error: Validations.time?('00:00:00').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-1h1ibyw/spec.rb:334: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.04687 seconds
41 examples, 3 failures

Failed examples:

rspec /tmp/d20130203-23049-1h1ibyw/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-1h1ibyw/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-1h1ibyw/spec.rb:331 # Validations validates times

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

Емил обнови решението на 27.11.2012 14:24 (преди почти 12 години)

+class PrivacyFilter
+ attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+ def initialize(text)
+ @text = text
+ @email_regexp = /\b(([a-zA-Z0-9])[\w\+\.-]{,200})@((\g<2>((\g<2>|-){,61}\g<2>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\b/
+ @phone_regexp = /(0|(00|\+)[1-9]\d{0,2})([- \(\)]{,2}\d)\g<3>{5,10}\b/
+ end
+
+ def filtered
+ result = filter_emails @text
+ result = filter_phone_numbers result
+ end
+
+ private
+
+ def filter_emails(text)
+ text.gsub(@email_regexp) do |match|
+ if !preserve_email_hostname and !partially_preserve_email_username
+ '[EMAIL]'
+ else
+ preserve_email_components match
+ end
+ end
+ end
+
+ def filter_phone_numbers(text)
+ text.gsub(@phone_regexp) do |match|
+ if !preserve_phone_country_code
+ "[PHONE]"
+ else
+ preserve_phone_components $1, match
+ end
+ end
+ end
+
+ def preserve_email_components(text)
+ username, hostname = text.split('@')
+ return "[FILTERED]@#{hostname}" if username.size < 6
+ return "#{username[0,3]}[FILTERED]@#{hostname}" if partially_preserve_email_username
+ return "[FILTERED]@#{hostname}" if preserve_email_hostname
+ end
+
+ def preserve_phone_components(prefix, phone)
+ if prefix == '0'
+ "[PHONE]"
+ else
+ "#{prefix} [FILTERED]"
+ end
+ end
+end
+
+class Validations
+ def self.email?(value)
+ case value
+ when /\A([a-zA-Z0-9])[\w\+\.-]{,200}@(\g<1>([0-9a-zA-Z-]{,61}\g<1>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\z/
+ true
+ else
+ false
+ end
+ end
+
+ def self.phone?(value)
+ /\A(0|(00|\+)[1-9]\d{0,2})([- \(\)]{,2}\d)\g<3>{5,10}\z/ =~ value ? true : false
+ end
+
+ def self.hostname?(value)
+ /\A([0-9a-zA-Z]([0-9a-zA-Z-]{,61}[0-9a-zA-Z])?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\z/ =~ value ? true : false
+ end
+
+ def self.ip_address?(value)
+ /\A([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.\g<1>){3}\z/ =~ value ? true : false
+ end
+
+ def self.number?(value)
+ /\A-?(0|[1-9][0-9]*)(\.[0-9]+)?\z/ =~ value ? true : false
+ end
+
+ def self.integer?(value)
+ /\A-?(0|[1-9][0-9]*)\z/ =~ value ? true : false
+ end
+
+ def self.date?(value)
+ /\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[1-2][0-9]|3[01])\z/ =~ value ? true : false
+ end
+
+ def self.time?(value)
+ /\A(0[1-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\z/ =~ value ? true : false
+ end
+
+ def self.date_time?(value)
+ case value
+ when /\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[1-2][0-9]|3[01])( |T)(0[1-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\z/
+ true
+ else
+ false
+ end
+ end
+end

Емил обнови решението на 27.11.2012 15:05 (преди почти 12 години)

class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@email_regexp = /\b(([a-zA-Z0-9])[\w\+\.-]{,200})@((\g<2>((\g<2>|-){,61}\g<2>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\b/
- @phone_regexp = /(0|(00|\+)[1-9]\d{0,2})([- \(\)]{,2}\d)\g<3>{5,10}\b/
+ @phone_regexp = /((\b00|\+)[1-9]\d{0,2}|\b0)([- \(\)]{,2}\d)\g<3>{5,10}\b/
end
def filtered
result = filter_emails @text
result = filter_phone_numbers result
end
private
def filter_emails(text)
text.gsub(@email_regexp) do |match|
if !preserve_email_hostname and !partially_preserve_email_username
'[EMAIL]'
else
preserve_email_components match
end
end
end
def filter_phone_numbers(text)
text.gsub(@phone_regexp) do |match|
if !preserve_phone_country_code
"[PHONE]"
else
preserve_phone_components $1, match
end
end
end
def preserve_email_components(text)
username, hostname = text.split('@')
return "[FILTERED]@#{hostname}" if username.size < 6
return "#{username[0,3]}[FILTERED]@#{hostname}" if partially_preserve_email_username
return "[FILTERED]@#{hostname}" if preserve_email_hostname
end
def preserve_phone_components(prefix, phone)
if prefix == '0'
"[PHONE]"
else
"#{prefix} [FILTERED]"
end
end
end
class Validations
def self.email?(value)
case value
when /\A([a-zA-Z0-9])[\w\+\.-]{,200}@(\g<1>([0-9a-zA-Z-]{,61}\g<1>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\z/
true
else
false
end
end
def self.phone?(value)
- /\A(0|(00|\+)[1-9]\d{0,2})([- \(\)]{,2}\d)\g<3>{5,10}\z/ =~ value ? true : false
+ /\A((00|\+)[1-9]\d{0,2}|0)([- \(\)]{,2}\d)\g<3>{5,10}\z/ =~ value ? true : false
end
def self.hostname?(value)
/\A([0-9a-zA-Z]([0-9a-zA-Z-]{,61}[0-9a-zA-Z])?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\z/ =~ value ? true : false
end
def self.ip_address?(value)
/\A([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.\g<1>){3}\z/ =~ value ? true : false
end
def self.number?(value)
/\A-?(0|[1-9][0-9]*)(\.[0-9]+)?\z/ =~ value ? true : false
end
def self.integer?(value)
/\A-?(0|[1-9][0-9]*)\z/ =~ value ? true : false
end
def self.date?(value)
/\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[1-2][0-9]|3[01])\z/ =~ value ? true : false
end
def self.time?(value)
/\A(0[1-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\z/ =~ value ? true : false
end
def self.date_time?(value)
case value
when /\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[1-2][0-9]|3[01])( |T)(0[1-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\z/
true
else
false
end
end
end