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

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

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

Резултати

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

Код

module Validations
def self.email?(value)
email_parts = value.match /\A[A-Za-z0-9][-A-Za-z0-9_+\.]{,200}@(?<host>.+)\z/
return false if !email_parts
hostname? email_parts[:host]
end
def self.phone?(value)
!!(value =~ /\A(0|(\+|00)[1-9]\d{,2})([- ()]{,2}\d){6,11}\z/)
end
def self.hostname?(value)
!!(value =~ /\A(([A-Za-z0-9]{1,2}|[A-Za-z0-9][-A-Za-z0-9]{,60}[A-Za-z0-9])\.)+[A-Za-z]{2,3}\z/)
end
def self.ip_address?(value)
octets = value.split '.'
octets.size == 4 && octets.all? { |octet| integer?(octet) && (0..255).include?(octet.to_i) }
end
def self.number?(value)
!!(value =~ /\A-?(0|[1-9]\d*)(\.\d+)?\z/)
end
def self.integer?(value)
!!(value =~ /\A-?(0|[1-9]\d*)\z/)
end
def self.date?(value)
return false if !(value =~ /\A\d\d\d\d-(\d\d)-(\d\d)\z/)
(1..12).include?($1.to_i) && (1..31).include?($2.to_i)
end
def self.time?(value)
return false if !(value =~ /\A(\d\d):(\d\d):(\d\d)\z/)
(0..23).include?($1.to_i) && (0..59).include?($2.to_i) && (0..59).include?($3.to_i)
end
def self.date_time?(value)
date_parts = value.match /\A(?<date>[-\d]+)[\s|T](?<time>[:\d]+)\z/
return false if !date_parts
date?(date_parts[:date]) && time?(date_parts[:time])
end
end
class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
end
def filtered
filtered = @text.gsub(/\b\S+@\S+\b/) { |match| Validations.email?(match) ? filter_email(match) : match }
filtered.gsub!(/(0|\+)[- ()\d]+/) { |match| Validations.phone?(match) ? filter_phone(match) : match }
filtered
end
private
def filter_email(email)
return email.sub(/[^@]+/) { |username| obfuscure_username username } if @partially_preserve_email_username
return email.sub(/[^@]+/, '[FILTERED]') if @preserve_email_hostname
'[EMAIL]'
end
def obfuscure_username(username)
if username.length > 6
username[0, 3] + '[FILTERED]'
else
'[FILTERED]'
end
end
def filter_phone(phone)
if @preserve_phone_country_code && phone_international?(phone)
phone[/\A(00|\+)\d{,3}/] + ' [FILTERED]'
else
'[PHONE]'
end
end
def phone_international?(phone)
!!(phone =~ /\A\+|00/)
end
end

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

..F......FFFF......F.....................

Failures:

  1) PrivacyFilter obfuscates more complicated emails
     Failure/Error: filter(text).should eq filtered
       
       expected: "Contact:[EMAIL]"
            got: "Contact:someone@example.com"
       
       (compared using ==)
     # /tmp/d20130203-23049-1egemfh/spec.rb:40:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1egemfh/spec.rb:39:in `each'
     # /tmp/d20130203-23049-1egemfh/spec.rb:39: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-1egemfh/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1egemfh/spec.rb:95:in `each'
     # /tmp/d20130203-23049-1egemfh/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 preserves whitespace around phones
     Failure/Error: filter(' +359881212-12-1 2 or...').should eq ' [PHONE] or...'
       
       expected: " [PHONE] or..."
            got: " +359881212-12-1 2 or..."
       
       (compared using ==)
     # /tmp/d20130203-23049-1egemfh/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)>'

  4) 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-1egemfh/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)>'

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

  6) 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-1egemfh/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1egemfh/spec.rb:209:in `each'
     # /tmp/d20130203-23049-1egemfh/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.04811 seconds
41 examples, 6 failures

Failed examples:

rspec /tmp/d20130203-23049-1egemfh/spec.rb:30 # PrivacyFilter obfuscates more complicated emails
rspec /tmp/d20130203-23049-1egemfh/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-1egemfh/spec.rb:100 # PrivacyFilter preserves whitespace around phones
rspec /tmp/d20130203-23049-1egemfh/spec.rb:104 # PrivacyFilter filters more than one phone or email
rspec /tmp/d20130203-23049-1egemfh/spec.rb:122 # PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
rspec /tmp/d20130203-23049-1egemfh/spec.rb:184 # Validations can validate more complex phone numbers

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

Пламен обнови решението на 28.11.2012 07:55 (преди почти 12 години)

+module Validations
+ def self.email?(value)
+ email_parts = value.match /\A[A-Za-z0-9][-A-Za-z0-9_+\.]{,200}@(?<host>.+)\z/
+ return false if !email_parts
+
+ hostname? email_parts[:host]
+ end
+
+ def self.phone?(value)
+ !!(value =~ /\A(0|(\+|00)[1-9]\d{,2})([- ()]{,2}\d){6,11}\z/)
+ end
+
+ def self.hostname?(value)
+ !!(value =~ /\A(([A-Za-z0-9]{1,2}|[A-Za-z0-9][-A-Za-z0-9]{,60}[A-Za-z0-9])\.)+[A-Za-z]{2,3}\z/)
+ end
+
+ def self.ip_address?(value)
+ octets = value.split '.'
+
+ octets.size == 4 && octets.all? { |octet| integer?(octet) && (0..255).include?(octet.to_i) }
+ end
+
+ def self.number?(value)
+ !!(value =~ /\A-?(0|[1-9]\d*)(\.\d+)?\z/)
+ end
+
+ def self.integer?(value)
+ !!(value =~ /\A-?(0|[1-9]\d*)\z/)
+ end
+
+ def self.date?(value)
+ return false if !(value =~ /\A\d\d\d\d-(\d\d)-(\d\d)\z/)
+
+ (1..12).include?($1.to_i) && (1..31).include?($2.to_i)
+ end
+
+ def self.time?(value)
+ return false if !(value =~ /\A(\d\d):(\d\d):(\d\d)\z/)
+
+ (0..23).include?($1.to_i) && (0..59).include?($2.to_i) && (0..59).include?($3.to_i)
+ end
+
+ def self.date_time?(value)
+ date_parts = value.match /\A(?<date>[-\d]+)[\s|T](?<time>[:\d]+)\z/
+ return false if !date_parts
+
+ date?(date_parts[:date]) && time?(date_parts[:time])
+ end
+end
+
+class PrivacyFilter
+ attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+
+ def initialize(text)
+ @text = text
+ end
+
+ def filtered
+ filtered = @text.gsub(/\b\S+@\S+\b/) { |match| Validations.email?(match) ? filter_email(match) : match }
+ filtered.gsub!(/(0|\+)[- ()\d]+/) { |match| Validations.phone?(match) ? filter_phone(match) : match }
+
+ filtered
+ end
+
+ private
+
+ def filter_email(email)
+ return email.sub(/[^@]+/) { |username| obfuscure_username username } if @partially_preserve_email_username
+ return email.sub(/[^@]+/, '[FILTERED]') if @preserve_email_hostname
+
+ '[EMAIL]'
+ end
+
+ def obfuscure_username(username)
+ if username.length > 6
+ username[0, 3] + '[FILTERED]'
+ else
+ '[FILTERED]'
+ end
+ end
+
+ def filter_phone(phone)
+ if @preserve_phone_country_code && phone_international?(phone)
+ phone[/\A(00|\+)\d{3}/] + ' [FILTERED]'
+ else
+ '[PHONE]'
+ end
+ end
+
+ def phone_international?(phone)
+ !!(phone =~ /\A\+|00/)
+ end
+end

Пламен обнови решението на 28.11.2012 07:59 (преди почти 12 години)

module Validations
def self.email?(value)
email_parts = value.match /\A[A-Za-z0-9][-A-Za-z0-9_+\.]{,200}@(?<host>.+)\z/
return false if !email_parts
hostname? email_parts[:host]
end
def self.phone?(value)
!!(value =~ /\A(0|(\+|00)[1-9]\d{,2})([- ()]{,2}\d){6,11}\z/)
end
def self.hostname?(value)
!!(value =~ /\A(([A-Za-z0-9]{1,2}|[A-Za-z0-9][-A-Za-z0-9]{,60}[A-Za-z0-9])\.)+[A-Za-z]{2,3}\z/)
end
def self.ip_address?(value)
octets = value.split '.'
octets.size == 4 && octets.all? { |octet| integer?(octet) && (0..255).include?(octet.to_i) }
end
def self.number?(value)
!!(value =~ /\A-?(0|[1-9]\d*)(\.\d+)?\z/)
end
def self.integer?(value)
!!(value =~ /\A-?(0|[1-9]\d*)\z/)
end
def self.date?(value)
return false if !(value =~ /\A\d\d\d\d-(\d\d)-(\d\d)\z/)
(1..12).include?($1.to_i) && (1..31).include?($2.to_i)
end
def self.time?(value)
return false if !(value =~ /\A(\d\d):(\d\d):(\d\d)\z/)
(0..23).include?($1.to_i) && (0..59).include?($2.to_i) && (0..59).include?($3.to_i)
end
def self.date_time?(value)
date_parts = value.match /\A(?<date>[-\d]+)[\s|T](?<time>[:\d]+)\z/
return false if !date_parts
date?(date_parts[:date]) && time?(date_parts[:time])
end
end
class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
end
def filtered
filtered = @text.gsub(/\b\S+@\S+\b/) { |match| Validations.email?(match) ? filter_email(match) : match }
filtered.gsub!(/(0|\+)[- ()\d]+/) { |match| Validations.phone?(match) ? filter_phone(match) : match }
filtered
end
private
def filter_email(email)
return email.sub(/[^@]+/) { |username| obfuscure_username username } if @partially_preserve_email_username
return email.sub(/[^@]+/, '[FILTERED]') if @preserve_email_hostname
'[EMAIL]'
end
def obfuscure_username(username)
if username.length > 6
username[0, 3] + '[FILTERED]'
else
'[FILTERED]'
end
end
def filter_phone(phone)
if @preserve_phone_country_code && phone_international?(phone)
- phone[/\A(00|\+)\d{3}/] + ' [FILTERED]'
+ phone[/\A(00|\+)\d{,3}/] + ' [FILTERED]'
else
'[PHONE]'
end
end
def phone_international?(phone)
!!(phone =~ /\A\+|00/)
end
end