Решение на Четвърта задача от Даяна Беркова

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

Към профила на Даяна Беркова

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 30 успешни тест(а)
  • 11 неуспешни тест(а)

Код

class Validations
def self.email?(value)
return true if /\A[a-z0-9](\w|\-|\+|\.){,200}@/i =~ value and Validations.hostname?(value.split("@")[1])
false
end
def self.hostname?(value)
return true if /\A[a-z0-9](([a-z0-9]|\-){,61}[a-z0-9])?\.[a-z]{2,3}(\.[a-z]{2})?/i =~ value
false
end
def self.phone?(value)
return true if /\A((00|\+)[1-9](\d){,2}|0)(( |\-|\(|\)){,2}\d){6,11}\z/ =~ value
false
end
def self.ip_address?(value)
return true if /\A((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(\.)){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\z/ =~ value
false
end
def self.number?(value)
return true if /\A-?(0(\.\d*[1-9])?|[1-9]\d*(\.\d*[1-9])?)\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[1-2])-(0[1-9]|[1-2][0-9]|3[0-1])\z/ =~ value
false
end
def self.time?(value)
return true if /\A([01]\d|2[0-3])(:[0-5]\d){2}\z/ =~ value
false
end
def self.date_time?(value)
return true if Validations.date?(value.split(/(\s|T)/)[0]) and Validations.time?(value.split(/(\s|T)/)[2])
false
end
end
class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
@text = text
@email_match = /[a-z0-9](\w|\-|\+|\.){,200}@[a-z0-9](([a-z0-9]|\-){,61}[a-z0-9])?\.[a-z]{2,3}(\.[a-z]{2})?/i
@phone_match = /((00|\+)[1-9](\d){,2}|0)(( |\-|\(|\)){,2}\d){6,11}/
end
def filtered
process_email_data if @email_match.match @text
process_phone_data if @phone_match.match @text
@text
end
def process_phone_data
@text = (@phone_match.match @text).to_s
filter_phone
@text = $` + @text + $'
filtered
end
def process_email_data
@text = (@email_match.match @text).to_s
filter_email
@text = $` + @text + $'
filtered
end
def filter_email
return filter_email_with_hostname if preserve_email_hostname and !partially_preserve_email_username
return filter_email_with_partial_username if partially_preserve_email_username
@text = '[EMAIL]'
end
def filter_email_with_hostname
@text = '[FILTERED]@' + @text.split("@")[1]
end
def filter_email_with_partial_username
return filter_email_with_hostname if @text[0..5].include? "@"
@text = @text[0..2] + '[FILTERED]@' + @text.split("@")[1]
end
def filter_phone
return filter_phone_with_country_code if preserve_phone_country_code
@text = '[PHONE]'
end
def filter_phone_with_country_code
return @text = @text.split(" ")[0] + ' [FILTERED]' if @text[0..4].include? ' ' and /(00|\+)/.match @text[0..1]
return @text = @text[0..3] + ' [FILTERED]' if @text[0] == '+'
return @text = @text[0..4] + ' [FILTEERD]' if @text[0..1] == '00'
@text = '[PHONE]'
end
end

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

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

Failures:

  1) PrivacyFilter obfuscates more complicated emails
     Failure/Error: filter(text).should eq filtered
       
       expected: "Contact: [EMAIL],[EMAIL]"
            got: "Contact: [EMAIL],[EMAIL]mple123.co.uk"
       
       (compared using ==)
     # /tmp/d20130203-23049-o1s1t7/spec.rb:40:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-o1s1t7/spec.rb:39:in `each'
     # /tmp/d20130203-23049-o1s1t7/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 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-o1s1t7/spec.rb:52:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-o1s1t7/spec.rb:46:in `each'
     # /tmp/d20130203-23049-o1s1t7/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)>'

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

  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( [FILTEERD]"
       
       (compared using ==)
     # /tmp/d20130203-23049-o1s1t7/spec.rb:144:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-o1s1t7/spec.rb:141:in `each'
     # /tmp/d20130203-23049-o1s1t7/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-o1s1t7/spec.rb:171:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-o1s1t7/spec.rb:170:in `each'
     # /tmp/d20130203-23049-o1s1t7/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-o1s1t7/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 #<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-o1s1t7/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-o1s1t7/spec.rb:209:in `each'
     # /tmp/d20130203-23049-o1s1t7/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 hostnames
     Failure/Error: Validations.hostname?('1.2.3.4.xip.io').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-o1s1t7/spec.rb:222: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) 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-o1s1t7/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)>'

  10) Validations validates more complex numbers
     Failure/Error: Validations.number?('0.0').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-o1s1t7/spec.rb:265: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 handles newlines in time and datetime validation
     Failure/Error: Validations.date_time?("2012-11-19 12:01:01\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-o1s1t7/spec.rb:362: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.04937 seconds
41 examples, 11 failures

Failed examples:

rspec /tmp/d20130203-23049-o1s1t7/spec.rb:30 # PrivacyFilter obfuscates more complicated emails
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:136 # PrivacyFilter separates preserved country code from filtered phone with a space
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:160 # Validations can validate more complex emails
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:175 # Validations does not break on emails in multiline strings
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:218 # Validations validates hostnames
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:232 # Validations handles multiline strings in hostname validation properly
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:257 # Validations validates more complex numbers
rspec /tmp/d20130203-23049-o1s1t7/spec.rb:359 # Validations handles newlines in time and datetime validation

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

Даяна обнови решението на 28.11.2012 14:05 (преди над 11 години)

+class Validations
+ def self.email?(value)
+ return true if /\A[a-z0-9](\w|\-|\+|\.){,200}@/i =~ value and Validations.hostname?(value.split("@")[1])
+ false
+ end
+
+ def self.hostname?(value)
+ return true if /\A[a-z0-9](([a-z0-9]|\-){,61}[a-z0-9])?\.[a-z]{2,3}(\.[a-z]{2})?/i =~ value
+ false
+ end
+
+ def self.phone?(value)
+ return true if /\A((00|\+)[1-9](\d){,2}|0)(( |\-|\(|\)){,2}\d){6,11}\z/ =~ value
+ false
+ end
+
+ def self.ip_address?(value)
+ return true if /\A((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(\.)){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\z/ =~ value
+ false
+ end
+
+ def self.number?(value)
+ return true if /\A-?(0(\.\d*[1-9])?|[1-9]\d*(\.\d*[1-9])?)\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[1-2])-(0[1-9]|[1-2][0-9]|3[0-1])\z/ =~ value
+ false
+ end
+
+ def self.time?(value)
+ return true if /\A([01]\d|2[0-3])(:[0-5]\d){2}\z/ =~ value
+ false
+ end
+
+ def self.date_time?(value)
+ return true if Validations.date?(value.split(/(\s|T)/)[0]) and Validations.time?(value.split(/(\s|T)/)[2])
+ false
+ end
+end
+
+class PrivacyFilter
+ attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+ def initialize(text)
+ @preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
+ @text = text
+ @email_match = /[a-z0-9](\w|\-|\+|\.){,200}@[a-z0-9](([a-z0-9]|\-){,61}[a-z0-9])?\.[a-z]{2,3}(\.[a-z]{2})?/i
+ @phone_match = /((00|\+)[1-9](\d){,2}|0)(( |\-|\(|\)){,2}\d){6,11}/
+ end
+
+ def filtered
+ process_email_data if @email_match.match @text
+ process_phone_data if @phone_match.match @text
+ @text
+ end
+
+ def process_phone_data
+ @text = (@phone_match.match @text).to_s
+ filter_phone
+ @text = $` + @text + $'
+ filtered
+ end
+
+ def process_email_data
+ @text = (@email_match.match @text).to_s
+ filter_email
+ @text = $` + @text + $'
+ filtered
+ end
+
+ def filter_email
+ return filter_email_with_hostname if preserve_email_hostname and !partially_preserve_email_username
+ return filter_email_with_partial_username if partially_preserve_email_username
+ @text = '[EMAIL]'
+ end
+
+ def filter_email_with_hostname
+ @text = '[FILTERED]@' + @text.split("@")[1]
+ end
+
+ def filter_email_with_partial_username
+ return filter_email_with_hostname if @text[0..5].include? "@"
+ @text = @text[0..2] + '[FILTERED]@' + @text.split("@")[1]
+ end
+
+ def filter_phone
+ return filter_phone_with_country_code if preserve_phone_country_code
+ @text = '[PHONE]'
+ end
+
+ def filter_phone_with_country_code
+ return @text = @text.split(" ")[0] + ' [FILTERED]' if @text[0..4].include? ' ' and /(00|\+)/.match @text[0..1]
+ return @text = @text[0..3] + ' [FILTERED]' if @text[0] == '+'
+ return @text = @text[0..4] + ' [FILTEERD]' if @text[0..1] == '00'
+ @text = '[PHONE]'
+ end
+end