Решение на Четвърта задача от Мартина Величкова

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

Към профила на Мартина Величкова

Резултати

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

Код

class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
HOSTNAME_REGULAR_EXPRESSION = /(?<host>([0-9a-zA-Z][0-9a-zA-Z-]{,61}[0-9a-zA-Z]\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))/
EMAIL_REGULAR_EXPRESSION = /([0-9a-zA-Z][\w\+\.-]{,200})@#{HOSTNAME_REGULAR_EXPRESSION}/
EMAIL_PARTITIONAL_USERNAME = /([0-9a-zA-Z][\w\+\.-]{2})([\w\+\.-]{,200})@/
PHONE_REGULAR_EXPRESSION = /(0|((\+\d{3})|(00\d)))(?<spliter>( |-|\(|\)){,2})(\d\g<spliter>){5,10}\d/
PHONE_COUNTRY_CODE_REGULAR_EXPRESSION = /((\+\d{3})|(00\d))(?<spliter>( |-|\(|\)){,2})(\d\g<spliter>){5,10}\d/
PHONE_LOCAL_CODE_REGULAR_EXPRESSION = /0(?<spliter>( |-|\(|\)){,2})(\d\g<spliter>){5,10}\d/
def initialize(text)
@preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
@text = text
end
def filtered
filtered_emails
filtered_phones
@text
end
private
def filter_email
@text.gsub!(EMAIL_REGULAR_EXPRESSION, '[EMAIL]')
end
def filter_username
@text.gsub!(EMAIL_REGULAR_EXPRESSION, '[FILTERED]@\k<host>')
end
def filter_partitional_username
@text.gsub!(EMAIL_REGULAR_EXPRESSION) { |email| email.gsub(EMAIL_PARTITIONAL_USERNAME, '\1[FILTERED]@') }
end
def filter_phone
@text.gsub!(PHONE_REGULAR_EXPRESSION, '[PHONE]')
end
def filter_phone_id
@text.gsub!(PHONE_COUNTRY_CODE_REGULAR_EXPRESSION) { |phone| phone[0..3]+' [FILTERED]' }
@text.gsub!(PHONE_LOCAL_CODE_REGULAR_EXPRESSION, '[PHONE]')
end
def filtered_emails
filter_username if preserve_email_hostname
filter_partitional_username if partially_preserve_email_username
filter_email if preserve_email_hostname == false and partially_preserve_email_username == false
end
def filtered_phones
filter_phone if @preserve_phone_country_code == false
filter_phone_id if @preserve_phone_country_code
end
end
class Validations
HOSTNAME_REGULAR_EXPRESSION = /([0-9a-zA-Z][0-9a-zA-Z-]{,61}[0-9a-zA-Z]\.)+([a-zA-Z]{2,3}(\.[a-zA-Z][a-zA-Z])?)/
def self.email?(value)
/\A[0-9a-zA-Z][\w\+\.-]{,200}@#{HOSTNAME_REGULAR_EXPRESSION}\z/.match(value) ? true : false
end
def self.phone?(value)
/\A(0|((\+\d{3})|(00\d)))(?<spliter>( |-|\(|\)){,2})(\d\g<spliter>){5,10}\d\z/.match(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][a-zA-Z])?)\z/.match(value) ? true : false
end
def self.ip_address?(value)
/\A(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).\g<1>.\g<1>.\g<1>\z/.match(value) ? true : false
end
def self.number?(value)
/\A-?(0|[1-9])\d*(\d*|(.?\d+))\z/.match(value) ? true : false
end
def self.integer?(value)
/\A-?\d+\z/.match(value) ? true : false
end
def self.date?(value)
/\A(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\z/.match(value) ? true : false
end
def self.time?(value)
/\A(([01][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]\z/.match(value) ? true : false
end
def self.date_time?(value)
/\A(\d{4})-(\d{2})-(\d{2})( |T)(([01][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]\z/.match(value) ? true : false
end
end

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

..F...FFFF.FFF.....F.F....F..F.........F.

Failures:

  1) PrivacyFilter obfuscates more complicated emails
     Failure/Error: PrivacyFilter.new(text).filtered
     RuntimeError:
       can't modify frozen String
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `gsub!'
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `filter_email'
     # /tmp/d20130203-23049-1615lcs/solution.rb:48:in `filtered_emails'
     # /tmp/d20130203-23049-1615lcs/solution.rb:17:in `filtered'
     # /tmp/d20130203-23049-1615lcs/spec.rb:5:in `filter'
     # /tmp/d20130203-23049-1615lcs/spec.rb:40:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1615lcs/spec.rb:39:in `each'
     # /tmp/d20130203-23049-1615lcs/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 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@example.com"
       
       (compared using ==)
     # /tmp/d20130203-23049-1615lcs/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@example.com"
       
       (compared using ==)
     # /tmp/d20130203-23049-1615lcs/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: PrivacyFilter.new(text).filtered
     RuntimeError:
       can't modify frozen String
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `gsub!'
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `filter_email'
     # /tmp/d20130203-23049-1615lcs/solution.rb:48:in `filtered_emails'
     # /tmp/d20130203-23049-1615lcs/solution.rb:17:in `filtered'
     # /tmp/d20130203-23049-1615lcs/spec.rb:5:in `filter'
     # /tmp/d20130203-23049-1615lcs/spec.rb:85:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1615lcs/spec.rb:84:in `each'
     # /tmp/d20130203-23049-1615lcs/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: PrivacyFilter.new(text).filtered
     RuntimeError:
       can't modify frozen String
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `gsub!'
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `filter_email'
     # /tmp/d20130203-23049-1615lcs/solution.rb:48:in `filtered_emails'
     # /tmp/d20130203-23049-1615lcs/solution.rb:17:in `filtered'
     # /tmp/d20130203-23049-1615lcs/spec.rb:5:in `filter'
     # /tmp/d20130203-23049-1615lcs/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1615lcs/spec.rb:95:in `each'
     # /tmp/d20130203-23049-1615lcs/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 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-1615lcs/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)>'

  7) PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
     Failure/Error: filter.filtered.should eq filtered
     RuntimeError:
       can't modify frozen String
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `gsub!'
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `filter_email'
     # /tmp/d20130203-23049-1615lcs/solution.rb:48:in `filtered_emails'
     # /tmp/d20130203-23049-1615lcs/solution.rb:17:in `filtered'
     # /tmp/d20130203-23049-1615lcs/spec.rb:132:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1615lcs/spec.rb:129:in `each'
     # /tmp/d20130203-23049-1615lcs/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)>'

  8) PrivacyFilter separates preserved country code from filtered phone with a space
     Failure/Error: filter.filtered.should eq filtered
     RuntimeError:
       can't modify frozen String
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `gsub!'
     # /tmp/d20130203-23049-1615lcs/solution.rb:25:in `filter_email'
     # /tmp/d20130203-23049-1615lcs/solution.rb:48:in `filtered_emails'
     # /tmp/d20130203-23049-1615lcs/solution.rb:17:in `filtered'
     # /tmp/d20130203-23049-1615lcs/spec.rb:144:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1615lcs/spec.rb:141:in `each'
     # /tmp/d20130203-23049-1615lcs/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)>'

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

  10) Validations validates hostnames
     Failure/Error: Validations.hostname?('1.2.3.4.xip.io').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-1615lcs/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)>'

  11) Validations validates more complex numbers
     Failure/Error: Validations.number?('00').should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1615lcs/spec.rb:263: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 validates more complex integers
     Failure/Error: Validations.integer?('00').should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1615lcs/spec.rb:286: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 datetime values
     Failure/Error: Validations.date_time?('2012-00-19T23:59:00').should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1615lcs/spec.rb:352: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.0495 seconds
41 examples, 13 failures

Failed examples:

rspec /tmp/d20130203-23049-1615lcs/spec.rb:30 # PrivacyFilter obfuscates more complicated emails
rspec /tmp/d20130203-23049-1615lcs/spec.rb:68 # PrivacyFilter filters whole email usernames if too short
rspec /tmp/d20130203-23049-1615lcs/spec.rb:72 # PrivacyFilter does not brake with unicode
rspec /tmp/d20130203-23049-1615lcs/spec.rb:76 # PrivacyFilter filters more complex phone numbers
rspec /tmp/d20130203-23049-1615lcs/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-1615lcs/spec.rb:104 # PrivacyFilter filters more than one phone or email
rspec /tmp/d20130203-23049-1615lcs/spec.rb:122 # PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
rspec /tmp/d20130203-23049-1615lcs/spec.rb:136 # PrivacyFilter separates preserved country code from filtered phone with a space
rspec /tmp/d20130203-23049-1615lcs/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-1615lcs/spec.rb:218 # Validations validates hostnames
rspec /tmp/d20130203-23049-1615lcs/spec.rb:257 # Validations validates more complex numbers
rspec /tmp/d20130203-23049-1615lcs/spec.rb:283 # Validations validates more complex integers
rspec /tmp/d20130203-23049-1615lcs/spec.rb:347 # Validations validates datetime values

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

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

+class PrivacyFilter
+ attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+
+ HOSTNAME_REGULAR_EXPRESSION = /(?<host>([0-9a-zA-Z][0-9a-zA-Z-]{,61}[0-9a-zA-Z]\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))/
+ EMAIL_REGULAR_EXPRESSION = /([0-9a-zA-Z][\w\+\.-]{,200})@#{HOSTNAME_REGULAR_EXPRESSION}/
+ EMAIL_PARTITIONAL_USERNAME = /([0-9a-zA-Z][\w\+\.-]{2})([\w\+\.-]{,200})@/
+ PHONE_REGULAR_EXPRESSION = /(0|((\+\d{3})|(00\d)))(?<spliter>( |-|\(|\)){,2})(\d\g<spliter>){5,10}\d/
+ PHONE_COUNTRY_CODE_REGULAR_EXPRESSION = /((\+\d{3})|(00\d))(?<spliter>( |-|\(|\)){,2})(\d\g<spliter>){5,10}\d/
+ PHONE_LOCAL_CODE_REGULAR_EXPRESSION = /0(?<spliter>( |-|\(|\)){,2})(\d\g<spliter>){5,10}\d/
+
+ def initialize(text)
+ @preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
+ @text = text
+ end
+
+ def filtered
+ filtered_emails
+ filtered_phones
+ @text
+ end
+
+ private
+
+ def filter_email
+ @text.gsub!(EMAIL_REGULAR_EXPRESSION, '[EMAIL]')
+ end
+
+ def filter_username
+ @text.gsub!(EMAIL_REGULAR_EXPRESSION, '[FILTERED]@\k<host>')
+ end
+
+ def filter_partitional_username
+ @text.gsub!(EMAIL_REGULAR_EXPRESSION) { |email| email.gsub(EMAIL_PARTITIONAL_USERNAME, '\1[FILTERED]@') }
+ end
+
+ def filter_phone
+ @text.gsub!(PHONE_REGULAR_EXPRESSION, '[PHONE]')
+ end
+
+ def filter_phone_id
+ @text.gsub!(PHONE_COUNTRY_CODE_REGULAR_EXPRESSION) { |phone| phone[0..3]+' [FILTERED]' }
+ @text.gsub!(PHONE_LOCAL_CODE_REGULAR_EXPRESSION, '[PHONE]')
+ end
+
+ def filtered_emails
+ filter_username if preserve_email_hostname
+ filter_partitional_username if partially_preserve_email_username
+ filter_email if preserve_email_hostname == false and partially_preserve_email_username == false
+ end
+
+ def filtered_phones
+ filter_phone if @preserve_phone_country_code == false
+ filter_phone_id if @preserve_phone_country_code
+ end
+end
+
+class Validations
+ HOSTNAME_REGULAR_EXPRESSION = /([0-9a-zA-Z][0-9a-zA-Z-]{,61}[0-9a-zA-Z]\.)+([a-zA-Z]{2,3}(\.[a-zA-Z][a-zA-Z])?)/
+ def self.email?(value)
+ /\A[0-9a-zA-Z][\w\+\.-]{,200}@#{HOSTNAME_REGULAR_EXPRESSION}\z/.match(value) ? true : false
+ end
+
+ def self.phone?(value)
+ /\A(0|((\+\d{3})|(00\d)))(?<spliter>( |-|\(|\)){,2})(\d\g<spliter>){5,10}\d\z/.match(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][a-zA-Z])?)\z/.match(value) ? true : false
+ end
+
+ def self.ip_address?(value)
+ /\A(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).\g<1>.\g<1>.\g<1>\z/.match(value) ? true : false
+ end
+
+ def self.number?(value)
+ /\A-?(0|[1-9])\d*(\d*|(.?\d+))\z/.match(value) ? true : false
+ end
+
+ def self.integer?(value)
+ /\A-?\d+\z/.match(value) ? true : false
+ end
+
+ def self.date?(value)
+ /\A(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\z/.match(value) ? true : false
+ end
+
+ def self.time?(value)
+ /\A(([01][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]\z/.match(value) ? true : false
+ end
+
+ def self.date_time?(value)
+ /\A(\d{4})-(\d{2})-(\d{2})( |T)(([01][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]\z/.match(value) ? true : false
+ end
+end
+