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

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

Към профила на Радослав Върбанов

Резултати

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

Код

class Validations
def self.email?(value)
(/(?x)\A[A-Za-z0-9][\w|\.|\+|\-]{,200}[\@]
([A-Za-z0-9][A-Za-z0-9\-]{,60}([A-Za-z0-9]?\.)){1,2}
(([A-Za-z]{2,3})(\z|\.[A-Za-z]{2}\z))/.match value) ? true : false
end
def self.hostname?(value)
(/(?x)\A([A-Za-z0-9][A-Za-z0-9\-]{,60}(([A-Za-z0-9]?)\.)){1,2}
(([A-Za-z]{2,3})(\z|\.[A-Za-z]{2}\z))/.match value) ? true : false
end
def self.phone?(value)
(/\A(?<prefix>(00|\+)[1-9][0-9]{,2}|[0])([ \(\)\-]{,2}[0-9]){6,11}\z/.match value) ? true : false
end
def self.ip_address?(value)
(/\A(?<byte>[0-1]?[0-9]{1,2}|2([5][0-5]|[0-4][0-9]))\.\g<byte>\.\g<byte>\.\g<byte>\z/.match value) ? true : false
end
def self.number?(value)
(/\A[\-]?(?<int>0|[1-9][0-9]*)(?<fract>\.[0-9]+)?\z/.match value) ? true : false
end
def self.integer?(value)
(/\A[\-]?(?<int>0|[1-9][0-9]*)\z/.match value) ? true : false
end
def self.date?(value)
(/\A[0-9]{4}\-(?<month>0[1-9]|1[0-2])\-(?<day>0[1-9]|[1-2][0-9]|3[0-1])\z/.match value) ? true : false
end
def self.time?(value)
(/\A(?<hour>[0-1][0-9]|2[0-3])(?<minsec>:[0-5][0-9]){2}\z/.match value) ? true : false
end
def self.date_time?(value)
(/(?x)\A[0-9]{4}\-(?<month>0[1-9]|1[0-2])\-(?<day>0[1-9]|[1-2][0-9]|3[0-1])[ \sT]
(?<hour>[0-1][0-9]|2[0-3])(?<minsec>:[0-5][0-9]){2}\z/.match value) ? true : 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
@initial_text = text
end
def filtered
filter_phones(filter_emails(@initial_text))
end
private
def filter_emails(text)
text.gsub(email_regex) do
email_replacement($~)
end
end
def filter_phones(text)
text.gsub(phone_regex) do
phone_replacement($~)
end
end
def phone_regex
/(?<prefix>(00|\+)[1-9][0-9]{,2}|[0])([ \(\)\-]{,2}[0-9]){6,11}/
end
def phone_replacement(matched)
filtered, prefix = "[PHONE]", matched[:prefix] == "0" ? "" : matched[:prefix] + ' '
if @preserve_phone_country_code then filtered = prefix + "[FILTERED]" end
filtered
end
def email_regex
/(?x)(?<mail>[A-Za-z0-9][\w|\.|\+|\-]{,200})(?<host>[\@]
([A-Za-z0-9][A-Za-z0-9\-]{,60}([A-Za-z0-9]?\.)){1,2}
(([A-Za-z]{2,3})((\.[A-Za-z]{2})?)))/
end
def email_replacement(matched)
filtered, name_first ="[EMAIL]", matched[:mail].length > 5 ? matched[:mail][0..2] : ""
if @partially_preserve_email_username then filtered = name_first + "[FILTERED]" + matched[:host]
elsif @preserve_email_hostname then filtered = "[FILTERED]" + matched[:host] end
filtered
end
end

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

...F.....F..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-e9g3e7/spec.rb:52:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-e9g3e7/spec.rb:46:in `each'
     # /tmp/d20130203-23049-e9g3e7/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-e9g3e7/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-e9g3e7/spec.rb:95:in `each'
     # /tmp/d20130203-23049-e9g3e7/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 allows country code to be preserved for internationally-formatted phone numbers
     Failure/Error: filter.filtered.should eq filtered
       
       expected: "Phone: [PHONE]"
            got: "Phone: [FILTERED]"
       
       (compared using ==)
     # /tmp/d20130203-23049-e9g3e7/spec.rb:132:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-e9g3e7/spec.rb:129:in `each'
     # /tmp/d20130203-23049-e9g3e7/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)>'

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

  5) Validations validates hostnames
     Failure/Error: Validations.hostname?('some.long-subdomain.domain.co.ul').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-e9g3e7/spec.rb:220: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.04725 seconds
41 examples, 5 failures

Failed examples:

rspec /tmp/d20130203-23049-e9g3e7/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-e9g3e7/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-e9g3e7/spec.rb:122 # PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
rspec /tmp/d20130203-23049-e9g3e7/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-e9g3e7/spec.rb:218 # Validations validates hostnames

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

Радослав обнови решението на 27.11.2012 19:16 (преди почти 12 години)

+class Validations
+ def self.email?(value)
+ (/(?x)\A[A-Za-z0-9][\w|\.|\+|\-]{,200}[\@]
+ ([A-Za-z0-9][A-Za-z0-9\-]{,60}([A-Za-z0-9]?\.)){1,2}
+ (([A-Za-z]{2,3})(\z|\.[A-Za-z]{2}\z))/.match value) ? true : false
+ end
+
+ def self.hostname?(value)
+ (/(?x)\A([A-Za-z0-9][A-Za-z0-9\-]{,60}(([A-Za-z0-9]?)\.)){1,2}
+ (([A-Za-z]{2,3})(\z|\.[A-Za-z]{2}\z))/.match value) ? true : false
+ end
+
+ def self.phone?(value)
+ (/\A(?<prefix>(00|\+)[1-9][0-9]{,2}|[0])([ \(\)\-]{,2}[0-9]){6,11}\z/.match value) ? true : false
+ end
+
+ def self.ip_address?(value)
+ (/\A(?<byte>[0-1]?[0-9]{1,2}|2([5][0-5]|[0-4][0-9]))\.\g<byte>\.\g<byte>\.\g<byte>\z/.match value) ? true : false
+ end
+
+ def self.number?(value)
+ (/\A[\-]?(?<int>0|[1-9][0-9]*)(?<fract>\.[0-9]+)?\z/.match value) ? true : false
+ end
+
+ def self.integer?(value)
+ (/\A[\-]?(?<int>0|[1-9][0-9]*)\z/.match value) ? true : false
+ end
+
+ def self.date?(value)
+ (/\A[0-9]{4}\-(?<month>0[1-9]|1[0-2])\-(?<day>0[1-9]|[1-2][0-9]|3[0-1])\z/.match value) ? true : false
+ end
+
+ def self.time?(value)
+ (/\A(?<hour>[0-1][0-9]|2[0-3])(?<minsec>:[0-5][0-9]){2}\z/.match value) ? true : false
+ end
+
+ def self.date_time?(value)
+ (/(?x)\A[0-9]{4}\-(?<month>0[1-9]|1[0-2])\-(?<day>0[1-9]|[1-2][0-9]|3[0-1])[ \sT]
+ (?<hour>[0-1][0-9]|2[0-3])(?<minsec>:[0-5][0-9]){2}\z/.match value) ? true : 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
+ @initial_text = text
+ end
+
+ def filtered
+ filter_phones(filter_emails(@initial_text))
+ end
+
+ private
+ def filter_emails(text)
+ text.gsub(email_regex) do
+ email_replacement($~)
+ end
+ end
+
+ def filter_phones(text)
+ text.gsub(phone_regex) do
+ phone_replacement($~)
+ end
+ end
+
+ def phone_regex
+ /(?<prefix>(00|\+)[1-9][0-9]{,2}|[0])([ \(\)\-]{,2}[0-9]){6,11}/
+ end
+
+ def phone_replacement(matched)
+ filtered, prefix = "[PHONE]", matched[:prefix] == "0" ? "" : matched[:prefix] + ' '
+ if @preserve_phone_country_code then filtered = prefix + "[FILTERED]" end
+ filtered
+ end
+
+ def email_regex
+ /(?x)(?<mail>[A-Za-z0-9][\w|\.|\+|\-]{,200})(?<host>[\@]
+ ([A-Za-z0-9][A-Za-z0-9\-]{,60}([A-Za-z0-9]?\.)){1,2}
+ (([A-Za-z]{2,3})((\.[A-Za-z]{2})?)))/
+ end
+
+ def email_replacement(matched)
+ filtered, name_first ="[EMAIL]", matched[:mail].length > 5 ? matched[:mail][0..2] : ""
+ if @partially_preserve_email_username then filtered = name_first + "[FILTERED]" + matched[:host]
+ elsif @preserve_email_hostname then filtered = "[FILTERED]" + matched[:host] end
+ filtered
+ end
+end