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

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

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

Резултати

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

Код

class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially_preserve_email_username = false
end
def filtered
filter_emails(filter_phones(@text))
end
def filter_phones(text)
if preserve_phone_country_code
filtered = text.gsub(/(?<code>(00|\+)[1-9]\d{,2})([- ()]{,2}\d){6,11}/, '\k<code> [FILTERED]')
filtered.gsub(/(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/, '[PHONE]')
else
text.gsub(/((00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11})|(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/, '[PHONE]')
end
end
def filter_emails(text)
text.gsub(/([0-9a-zA-Z])[-\+\.\w]{,200}@(\g<1>([-0-9a-zA-Z]{,61}\g<1>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?/) do |mail|
if !preserve_email_hostname and !partially_preserve_email_username
"[EMAIL]"
else
filter_email(mail)
end
end
end
private
def filter_email(email)
name, host = email.split(/@/)
filter_name(name) + "@" + host
end
def filter_name(name)
if partially_preserve_email_username and name.size >= 6
name.gsub(/(?<=[-\+\.\w]{3})[-\+\.\w]+/, "[FILTERED]")
elsif preserve_email_hostname
"[FILTERED]"
end
end
end
module Validations
def self.date?(value)
!!(/\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\z/ =~ value)
end
def self.time?(value)
!!(/\A([01]\d|2[0-3]):[0-5]\d:[0-5]\d\z/ =~ value)
end
def self.date_time?(value)
date, time = value.split(/[ T]/, 2)
!!date?(date) and !!time?(time)
end
def self.integer?(value)
!!(/\A-?(0|[1-9]\d*)\z/ =~ value)
end
def self.number?(value)
!!(/\A-?(0|[1-9]\d*)(\.\d+)?\z/ =~ value)
end
def self.ip_address?(value)
!!(/\A(0|[1-9]\d?|2[0-5]{2}).\g<1>.\g<1>.\g<1>/ =~ value)
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]{2})?\z/ =~ value)
end
def self.phone?(value)
!!(/\A0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10}\z/ =~ value) or
!!(/\A(00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11}\z/ =~ value)
end
def self.email?(value)
name, host = value.split(/@/, 2)
!!(/\A[0-9a-zA-Z][-\+\.\w]{,200}/ =~ name and hostname?(host))
end
end

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

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

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-1u38ew5/spec.rb:52:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1u38ew5/spec.rb:46:in `each'
     # /tmp/d20130203-23049-1u38ew5/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 filters whole email usernames if too short
     Failure/Error: filter.filtered
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20130203-23049-1u38ew5/solution.rb:38:in `filter_email'
     # /tmp/d20130203-23049-1u38ew5/solution.rb:29:in `block in filter_emails'
     # /tmp/d20130203-23049-1u38ew5/solution.rb:25:in `gsub'
     # /tmp/d20130203-23049-1u38ew5/solution.rb:25:in `filter_emails'
     # /tmp/d20130203-23049-1u38ew5/solution.rb:12:in `filtered'
     # /tmp/d20130203-23049-1u38ew5/spec.rb:17:in `partially_filter_email_usernames'
     # /tmp/d20130203-23049-1u38ew5/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: filter.filtered
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20130203-23049-1u38ew5/solution.rb:38:in `filter_email'
     # /tmp/d20130203-23049-1u38ew5/solution.rb:29:in `block in filter_emails'
     # /tmp/d20130203-23049-1u38ew5/solution.rb:25:in `gsub'
     # /tmp/d20130203-23049-1u38ew5/solution.rb:25:in `filter_emails'
     # /tmp/d20130203-23049-1u38ew5/solution.rb:12:in `filtered'
     # /tmp/d20130203-23049-1u38ew5/spec.rb:17:in `partially_filter_email_usernames'
     # /tmp/d20130203-23049-1u38ew5/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 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-1u38ew5/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1u38ew5/spec.rb:95:in `each'
     # /tmp/d20130203-23049-1u38ew5/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)>'

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

  6) Validations validates IP addresses
     Failure/Error: Validations.ip_address?('300.2.3.4').should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1u38ew5/spec.rb:239: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 handles multiline strings in IP validation properly
     Failure/Error: Validations.ip_address?("8.8.8.8\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1u38ew5/spec.rb:245: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.04727 seconds
41 examples, 7 failures

Failed examples:

rspec /tmp/d20130203-23049-1u38ew5/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-1u38ew5/spec.rb:68 # PrivacyFilter filters whole email usernames if too short
rspec /tmp/d20130203-23049-1u38ew5/spec.rb:72 # PrivacyFilter does not brake with unicode
rspec /tmp/d20130203-23049-1u38ew5/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-1u38ew5/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-1u38ew5/spec.rb:237 # Validations validates IP addresses
rspec /tmp/d20130203-23049-1u38ew5/spec.rb:244 # Validations handles multiline strings in IP validation properly

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

Георги обнови решението на 28.11.2012 16:37 (преди почти 12 години)

+class PrivacyFilter
+ attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+
+ def initialize(text)
+ @text = text
+ @preserve_phone_country_code = false
+ @preserve_email_hostname = false
+ @partially_preserve_email_username = false
+ end
+
+ def filtered
+ filter_emails(filter_phones(@text))
+ end
+
+ def filter_phones(text)
+ if preserve_phone_country_code
+ filtered = text.gsub(/(?<code>(00|\+)[1-9]\d{,2})([- ()]{,2}\d){6,11}/, '\k<code> [FILTERED]')
+ filtered.gsub(/(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/,'[PHONE]')
+ else
+ text.gsub(/((00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11})|(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/,'[PHONE]')
+ end
+ end
+
+ def filter_emails(text)
+ text.gsub(/([0-9a-zA-Z])[-\+\.\w]{,200}@(\g<1>([-0-9a-zA-Z]{,61}\g<1>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?/) do |mail|
+ if !preserve_email_hostname and !partially_preserve_email_username
+ "[EMAIL]"
+ else
+ filter_email(mail)
+ end
+ end
+ end
+
+ private
+
+ def filter_email(email)
+ name, host = email.split(/@/)
+ filter_name(name) + "@" + host
+ end
+
+ def filter_name(name)
+ if partially_preserve_email_username and name.size >= 6
+ name.gsub(/(?<=[-\+\.\w]{3})[-\+\.\w]+/,"[FILTERED]")
+ elsif preserve_email_hostname
+ "[FILTERED]"
+ end
+ end
+end
+
+module Validations
+ def self.date?(value)
+ !!(/\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\z/ =~ value)
+ end
+
+ def self.time?(value)
+ !!(/\A([01]\d|2[0-3]):[0-5]\d:[0-5]\d\z/ =~ value)
+ end
+
+ def self.date_time?(value)
+ date, time = value.split(/[ T]/,2)
+ !!date?(date) and !!time?(time)
+ end
+
+ def self.integer?(value)
+ !!(/\A-?(0|[1-9]\d*)\z/ =~ value)
+ end
+
+ def self.number?(value)
+ !!(/\A-?(0|[1-9]\d*)(\.\d+)?\z/ =~ value)
+ end
+
+ def self.ip_address?(value)
+ !!(/\A(0|[1-9]\d?|2[0-5]{2}).\g<1>.\g<1>.\g<1>/ =~ value)
+ 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]{2})?\z/ =~ value)
+ end
+
+ def self.phone?(value)
+ !!(/\A0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10}\z/ =~ value) or
+ !!(/\A(00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11}\z/ =~ value)
+ end
+
+ def self.email?(value)
+ name, host = value.split(/@/,2)
+ !!(/\A[0-9a-zA-Z][-\+\.\w]{,200}/ =~ name and hostname?(host))
+ end
+end

Георги обнови решението на 28.11.2012 16:41 (преди почти 12 години)

class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially_preserve_email_username = false
end
def filtered
filter_emails(filter_phones(@text))
end
def filter_phones(text)
if preserve_phone_country_code
filtered = text.gsub(/(?<code>(00|\+)[1-9]\d{,2})([- ()]{,2}\d){6,11}/, '\k<code> [FILTERED]')
- filtered.gsub(/(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/,'[PHONE]')
+ filtered.gsub(/(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/, '[PHONE]')
else
- text.gsub(/((00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11})|(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/,'[PHONE]')
+ text.gsub(/((00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11})|(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/, '[PHONE]')
end
end
def filter_emails(text)
text.gsub(/([0-9a-zA-Z])[-\+\.\w]{,200}@(\g<1>([-0-9a-zA-Z]{,61}\g<1>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?/) do |mail|
if !preserve_email_hostname and !partially_preserve_email_username
"[EMAIL]"
else
filter_email(mail)
end
end
end
private
def filter_email(email)
name, host = email.split(/@/)
filter_name(name) + "@" + host
end
def filter_name(name)
if partially_preserve_email_username and name.size >= 6
- name.gsub(/(?<=[-\+\.\w]{3})[-\+\.\w]+/,"[FILTERED]")
+ name.gsub(/(?<=[-\+\.\w]{3})[-\+\.\w]+/, "[FILTERED]")
elsif preserve_email_hostname
"[FILTERED]"
end
end
end
module Validations
def self.date?(value)
!!(/\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\z/ =~ value)
end
def self.time?(value)
!!(/\A([01]\d|2[0-3]):[0-5]\d:[0-5]\d\z/ =~ value)
end
def self.date_time?(value)
date, time = value.split(/[ T]/,2)
!!date?(date) and !!time?(time)
end
def self.integer?(value)
!!(/\A-?(0|[1-9]\d*)\z/ =~ value)
end
def self.number?(value)
!!(/\A-?(0|[1-9]\d*)(\.\d+)?\z/ =~ value)
end
def self.ip_address?(value)
!!(/\A(0|[1-9]\d?|2[0-5]{2}).\g<1>.\g<1>.\g<1>/ =~ value)
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]{2})?\z/ =~ value)
end
def self.phone?(value)
!!(/\A0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10}\z/ =~ value) or
!!(/\A(00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11}\z/ =~ value)
end
def self.email?(value)
name, host = value.split(/@/,2)
!!(/\A[0-9a-zA-Z][-\+\.\w]{,200}/ =~ name and hostname?(host))
end
end

Георги обнови решението на 28.11.2012 16:42 (преди почти 12 години)

class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially_preserve_email_username = false
end
def filtered
filter_emails(filter_phones(@text))
end
def filter_phones(text)
if preserve_phone_country_code
filtered = text.gsub(/(?<code>(00|\+)[1-9]\d{,2})([- ()]{,2}\d){6,11}/, '\k<code> [FILTERED]')
filtered.gsub(/(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/, '[PHONE]')
else
text.gsub(/((00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11})|(0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10})/, '[PHONE]')
end
end
def filter_emails(text)
text.gsub(/([0-9a-zA-Z])[-\+\.\w]{,200}@(\g<1>([-0-9a-zA-Z]{,61}\g<1>)?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?/) do |mail|
if !preserve_email_hostname and !partially_preserve_email_username
"[EMAIL]"
else
filter_email(mail)
end
end
end
private
def filter_email(email)
name, host = email.split(/@/)
filter_name(name) + "@" + host
end
def filter_name(name)
if partially_preserve_email_username and name.size >= 6
name.gsub(/(?<=[-\+\.\w]{3})[-\+\.\w]+/, "[FILTERED]")
elsif preserve_email_hostname
"[FILTERED]"
end
end
end
module Validations
def self.date?(value)
!!(/\A\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\z/ =~ value)
end
def self.time?(value)
!!(/\A([01]\d|2[0-3]):[0-5]\d:[0-5]\d\z/ =~ value)
end
def self.date_time?(value)
- date, time = value.split(/[ T]/,2)
+ date, time = value.split(/[ T]/, 2)
!!date?(date) and !!time?(time)
end
def self.integer?(value)
!!(/\A-?(0|[1-9]\d*)\z/ =~ value)
end
def self.number?(value)
!!(/\A-?(0|[1-9]\d*)(\.\d+)?\z/ =~ value)
end
def self.ip_address?(value)
!!(/\A(0|[1-9]\d?|2[0-5]{2}).\g<1>.\g<1>.\g<1>/ =~ value)
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]{2})?\z/ =~ value)
end
def self.phone?(value)
!!(/\A0[- ()]{,2}[1-9]([- ()]{,2}\d){5,10}\z/ =~ value) or
!!(/\A(00|\+)[1-9]\d{,2}([- ()]{,2}\d){6,11}\z/ =~ value)
end
def self.email?(value)
- name, host = value.split(/@/,2)
+ name, host = value.split(/@/, 2)
!!(/\A[0-9a-zA-Z][-\+\.\w]{,200}/ =~ name and hostname?(host))
end
-end
+end