Решение на Четвърта задача от Красимир Георгиев

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

Към профила на Красимир Георгиев

Резултати

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

Код

module RegexConstants
TLD = /[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?/
DOMAIN_NAME = /[[:alnum:]]|[[:alnum:]][[:alnum:]-]{,60}[[:alnum:]]/
SUBDOMAIN = /#{DOMAIN_NAME}/
DOMAIN = /#{DOMAIN_NAME}\.#{TLD}/
HOSTNAME = /(#{SUBDOMAIN}\.)*#{DOMAIN}/
USERNAME = /[[:alnum:]][\w+.-]{,200}/
EMAIL = /(?<username>#{USERNAME})@(?<hostname>#{HOSTNAME})/
INTERNATIONAL_CODE = /[1-9]\d{,2}/
INTERNATIONAL_PREFIX = /(00|\+)#{INTERNATIONAL_CODE}/
PHONE_PREFIX = /0|#{INTERNATIONAL_PREFIX}/
PHONE_DELIMITERS = /[ ()-]/
PHONE_BODY = /\d(#{PHONE_DELIMITERS}{,2}\d){5,10}/
PHONE = /#{PHONE_PREFIX}#{PHONE_DELIMITERS}*#{PHONE_BODY}/
INTERNATIONAL_PHONE = /(?<prefix>#{INTERNATIONAL_PREFIX})#{PHONE_DELIMITERS}*#{PHONE_BODY}/
BYTE = /0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?/
IP_ADDRESS = /#{BYTE}(\.#{BYTE}){3}/
INTEGER = /-?(0|[1-9]\d*)/
NUMBER = /#{INTEGER}(\.\d+)?/
DATE = /\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])/
TIME = /([0-1]\d|2[0-3]):(?<six>[0-5]\d):\g<six>/
DATE_TIME = /#{DATE}[ T](#{TIME})/
end
class Validations
class << self
include RegexConstants
def exact?(regex, value)
not (value =~ /\A#{regex}\z/).nil?
end
def email?(value)
exact?(EMAIL, value)
end
def phone?(value)
exact?(PHONE, value)
end
def hostname?(value)
exact?(HOSTNAME, value)
end
def ip_address?(value)
exact?(IP_ADDRESS, value)
end
def number?(value)
exact?(NUMBER, value)
end
def integer?(value)
exact?(INTEGER, value)
end
def date?(value)
exact?(DATE, value)
end
def time?(value)
exact?(TIME, value)
end
def date_time?(value)
exact?(DATE_TIME, value)
end
end
end
class PrivacyFilter
include RegexConstants
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :partially_preserve_email_username
def initialize(text)
@text = text
end
def filter_name(name)
return '[FILTERED]' if name.length < 6
name[0...3] + '[FILTERED]'
end
def filtered_from_email
result = @text.dup
result.gsub!(EMAIL) { |s| "#{filter_name $1}@#{$2}" } if partially_preserve_email_username
result.gsub!(EMAIL, '[FILTERED]@\k<hostname>') if preserve_email_hostname
result.gsub(EMAIL, '[EMAIL]')
end
def filtered
result = filtered_from_email
result.gsub!(INTERNATIONAL_PHONE, '\k<prefix> [FILTERED]') if preserve_phone_country_code
result.gsub(PHONE, '[PHONE]')
end
end

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

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

Failures:

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

Failed examples:

rspec /tmp/d20130203-23049-iy5c1h/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-iy5c1h/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-iy5c1h/spec.rb:184 # Validations can validate more complex phone numbers

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

Красимир обнови решението на 27.11.2012 23:46 (преди почти 12 години)

+module RegexConstants
+ TLD = /[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?/
+ DOMAIN_NAME = /[[:alnum:]]|[[:alnum:]][[:alnum:]-]{,60}[[:alnum:]]/
+ SUBDOMAIN = /#{DOMAIN_NAME}/
+ DOMAIN = /#{DOMAIN_NAME}\.#{TLD}/
+ HOSTNAME = /(#{SUBDOMAIN}\.)*#{DOMAIN}/
+ USERNAME = /[[:alnum:]][\w+.-]{,200}/
+ EMAIL = /(?<username>#{USERNAME})@(?<hostname>#{HOSTNAME})/
+
+ INTERNATIONAL_CODE = /[1-9]\d{,2}/
+ INTERNATIONAL_PREFIX = /(00|\+)#{INTERNATIONAL_CODE}/
+ PHONE_PREFIX = /0|#{INTERNATIONAL_PREFIX}/
+ PHONE_DELIMITERS = /[ ()-]/
+ PHONE_BODY = /\d(#{PHONE_DELIMITERS}{,2}\d){5,10}/
+ PHONE = /#{PHONE_PREFIX}#{PHONE_DELIMITERS}*#{PHONE_BODY}/
+ INTERNATIONAL_PHONE = /(?<prefix>#{INTERNATIONAL_PREFIX})#{PHONE_DELIMITERS}*#{PHONE_BODY}/
+
+ BYTE = /0|1\d\d|2[0-4]\d|25[0-5]/
+ IP_ADDRESS = /#{BYTE}(\.#{BYTE}){3}/
+
+ INTEGER = /-?(0|[1-9]\d*)/
+ NUMBER = /#{INTEGER}(\.\d+)?/
+
+ DATE = /\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])/
+ TIME = /([0-1]\d|2[0-3]):(?<six>[0-5]\d):\g<six>/
+ DATE_TIME = /#{DATE}[ T](#{TIME})/
+end
+
+class Validations
+ class << self
+ include RegexConstants
+
+ def exact?(regex, value)
+ not (value =~ /\A#{regex}\z/).nil?
+ end
+
+ def email?(value)
+ exact?(EMAIL, value)
+ end
+
+ def phone?(value)
+ exact?(PHONE, value)
+ end
+
+ def hostname?(value)
+ exact?(HOSTNAME, value)
+ end
+
+ def ip_address?(value)
+ exact?(IP_ADDRESS, value)
+ end
+
+ def number?(value)
+ exact?(NUMBER, value)
+ end
+
+ def integer?(value)
+ exact?(INTEGER, value)
+ end
+
+ def date?(value)
+ exact?(DATE, value)
+ end
+
+ def time?(value)
+ exact?(TIME, value)
+ end
+
+ def date_time?(value)
+ exact?(DATE_TIME, value)
+ end
+ end
+end
+
+class PrivacyFilter
+ include RegexConstants
+
+ attr_accessor :preserve_phone_country_code
+ attr_accessor :preserve_email_hostname
+ attr_accessor :partially_preserve_email_username
+
+ def initialize(text)
+ @text = text
+ end
+
+ def filter_name(name)
+ return '[FILTERED]' if name.length < 6
+ name[0...3] + '[FILTERED]'
+ end
+
+ def filtered_from_email
+ result = @text.dup
+ result.gsub!(EMAIL) { |s| "#{filter_name $1}@#{$2}" } if partially_preserve_email_username
+ result.gsub!(EMAIL, '[FILTERED]@\k<hostname>') if preserve_email_hostname
+ result.gsub(EMAIL, '[EMAIL]')
+ end
+
+ def filtered
+ result = filtered_from_email
+ result.gsub!(INTERNATIONAL_PHONE, '\k<prefix>[FILTERED]') if preserve_phone_country_code
+ result.gsub(PHONE, '[PHONE]')
+ end
+end

Красимир обнови решението на 27.11.2012 23:54 (преди почти 12 години)

module RegexConstants
TLD = /[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?/
DOMAIN_NAME = /[[:alnum:]]|[[:alnum:]][[:alnum:]-]{,60}[[:alnum:]]/
SUBDOMAIN = /#{DOMAIN_NAME}/
DOMAIN = /#{DOMAIN_NAME}\.#{TLD}/
HOSTNAME = /(#{SUBDOMAIN}\.)*#{DOMAIN}/
USERNAME = /[[:alnum:]][\w+.-]{,200}/
EMAIL = /(?<username>#{USERNAME})@(?<hostname>#{HOSTNAME})/
INTERNATIONAL_CODE = /[1-9]\d{,2}/
INTERNATIONAL_PREFIX = /(00|\+)#{INTERNATIONAL_CODE}/
PHONE_PREFIX = /0|#{INTERNATIONAL_PREFIX}/
PHONE_DELIMITERS = /[ ()-]/
PHONE_BODY = /\d(#{PHONE_DELIMITERS}{,2}\d){5,10}/
PHONE = /#{PHONE_PREFIX}#{PHONE_DELIMITERS}*#{PHONE_BODY}/
INTERNATIONAL_PHONE = /(?<prefix>#{INTERNATIONAL_PREFIX})#{PHONE_DELIMITERS}*#{PHONE_BODY}/
- BYTE = /0|1\d\d|2[0-4]\d|25[0-5]/
+ BYTE = /0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?/
IP_ADDRESS = /#{BYTE}(\.#{BYTE}){3}/
INTEGER = /-?(0|[1-9]\d*)/
NUMBER = /#{INTEGER}(\.\d+)?/
DATE = /\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])/
TIME = /([0-1]\d|2[0-3]):(?<six>[0-5]\d):\g<six>/
DATE_TIME = /#{DATE}[ T](#{TIME})/
end
class Validations
class << self
include RegexConstants
def exact?(regex, value)
not (value =~ /\A#{regex}\z/).nil?
end
def email?(value)
exact?(EMAIL, value)
end
def phone?(value)
exact?(PHONE, value)
end
def hostname?(value)
exact?(HOSTNAME, value)
end
def ip_address?(value)
exact?(IP_ADDRESS, value)
end
def number?(value)
exact?(NUMBER, value)
end
def integer?(value)
exact?(INTEGER, value)
end
def date?(value)
exact?(DATE, value)
end
def time?(value)
exact?(TIME, value)
end
def date_time?(value)
exact?(DATE_TIME, value)
end
end
end
class PrivacyFilter
include RegexConstants
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :partially_preserve_email_username
def initialize(text)
@text = text
end
def filter_name(name)
return '[FILTERED]' if name.length < 6
name[0...3] + '[FILTERED]'
end
def filtered_from_email
result = @text.dup
result.gsub!(EMAIL) { |s| "#{filter_name $1}@#{$2}" } if partially_preserve_email_username
result.gsub!(EMAIL, '[FILTERED]@\k<hostname>') if preserve_email_hostname
result.gsub(EMAIL, '[EMAIL]')
end
def filtered
result = filtered_from_email
- result.gsub!(INTERNATIONAL_PHONE, '\k<prefix>[FILTERED]') if preserve_phone_country_code
+ result.gsub!(INTERNATIONAL_PHONE, '\k<prefix> [FILTERED]') if preserve_phone_country_code
result.gsub(PHONE, '[PHONE]')
end
end