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

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

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

Резултати

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

Код

class PrivacyFilter
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :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
if partially_preserve_email_username == true then @text = username_filtered(1) end
if preserve_email_hostname == true then @text = email_filtered(0) end
if preserve_phone_country_code == true then @text = phone_filtered(1) end
simply_filtered
end
def username_filtered(flag)
host = '(@([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)'
if (name = (/([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200})#{host}/.match @text)) == nil or $1.size < 6 or flag == 0
email_filtered(flag)
else
result = name.pre_match + $1[0,3] + '[FILTERED]' + $2 + PrivacyFilter.new(name.post_match).username_filtered(flag)
end
end
def email_filtered(flag)
name = /[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}/.match @text
if (host = /(@([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text) and name != nil
result = name.pre_match + '[FILTERED]' + $1 + PrivacyFilter.new(host.post_match).username_filtered(flag)
else
@text
end
end
def phone_filtered(flag)
phone = /((00|\+)[1-9]\d{0,2})(([-\(\)\s]{0,2}\d){6,11})/.match @text
if phone == nil or flag == 0
simply_phone_filtered(flag)
else
result = phone.pre_match + $1 + ' [FILTERED]' + PrivacyFilter.new(phone.post_match).phone_filtered(flag)
end
end
def simply_filtered
result = simply_email_filtered
PrivacyFilter.new(result).simply_phone_filtered(1)
end
def simply_phone_filtered(flag)
phone = /((((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11})/.match @text
if phone != nil
result = phone.pre_match + '[PHONE]' + PrivacyFilter.new(phone.post_match).phone_filtered(flag)
else
@text
end
end
def simply_email_filtered
hostname = /(([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text
if (name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@)/.match @text) != nil and hostname != nil
result = PrivacyFilter.new(name.pre_match).filtered + '[EMAIL]' + PrivacyFilter.new(hostname.post_match).filtered
else
@text
end
end
end
class Validations
def Validations.email?(value)
if (name = /\A[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@/.match value) and
Validations.hostname?(name.post_match)
true
else
false
end
end
def Validations.phone?(value)
if /\A(((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11}\z/ =~ value
true
else
false
end
end
def Validations.hostname?(value)
if /\A([a-zA-Z0-9][a-zA-Z0-9-]{0,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?\z/ =~ value
true
else
false
end
end
def Validations.ip_address?(value)
if (ip = /\A(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\z/.match value) and
ip[1].split('.').map{ |number| Integer(number) < 256 }.count{ |item| item == true } == 4
true
else
false
end
end
def Validations.number?(value)
if /\A-?((0(\.\d+)?)|[1-9]\d*(\.\d+)?)\z/ =~ value
true
else
false
end
end
def Validations.integer?(value)
if /\A-?(0|[1-9]\d*)\z/ =~ value
true
else
false
end
end
def Validations.date?(value)
if (match = /\A(\d{4}-\d{2}-\d{2})\z/.match value)
date = match[1].split('-').map{ |number| Integer(number) }
date[1] > 0 and date[1] < 13 and date[2] > 0 and date[2] < 32
end
end
def Validations.time?(value)
if (match = /\A(\d{2}:\d{2}:\d{2})\z/.match value)
time = match[1].split(':').map{ |number| Integer(number) }
time[0] >= 0 and time[0] <24 and time[1] >= 0 and time[1] < 60 and time[2] >= 0 and time[2] < 60
end
end
def Validations.date_time?(value)
if (Validations.date?(value.split[0]) and Validations.time?(value.split[1])) or
(Validations.date?(value.split('T')[0]) and Validations.time?(value.split('T')[1]))
true
else
false
end
end
end

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

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

Failures:

  1) PrivacyFilter obfuscates more complicated emails
     Failure/Error: filter(text).should eq filtered
       
       expected: "[EMAIL]"
            got: "[EMAIL][EMAIL]"
       
       (compared using ==)
     # /tmp/d20130203-23049-1pkykh2/spec.rb:40:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1pkykh2/spec.rb:39:in `each'
     # /tmp/d20130203-23049-1pkykh2/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-1pkykh2/spec.rb:52:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1pkykh2/spec.rb:46:in `each'
     # /tmp/d20130203-23049-1pkykh2/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-1pkykh2/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1pkykh2/spec.rb:95:in `each'
     # /tmp/d20130203-23049-1pkykh2/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 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: [PHONE] or 00441 [FILTERED]\n      Email: [EMAIL] or [EMAIL]\n    "
       
       (compared using ==)
       
       Diff:
       @@ -1,7 +1,7 @@
        
              Contacts
        
       -      Phones: [PHONE] or [PHONE]
       +      Phones: [PHONE] or 00441 [FILTERED]
              Email: [EMAIL] or [EMAIL]
            
     # /tmp/d20130203-23049-1pkykh2/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)>'

  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-1pkykh2/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1pkykh2/spec.rb:209:in `each'
     # /tmp/d20130203-23049-1pkykh2/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 hostnames
     Failure/Error: Validations.hostname?('not-a-hostname-.com').should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1pkykh2/spec.rb:227: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 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-1pkykh2/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.04889 seconds
41 examples, 7 failures

Failed examples:

rspec /tmp/d20130203-23049-1pkykh2/spec.rb:30 # PrivacyFilter obfuscates more complicated emails
rspec /tmp/d20130203-23049-1pkykh2/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-1pkykh2/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-1pkykh2/spec.rb:104 # PrivacyFilter filters more than one phone or email
rspec /tmp/d20130203-23049-1pkykh2/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-1pkykh2/spec.rb:218 # Validations validates hostnames
rspec /tmp/d20130203-23049-1pkykh2/spec.rb:359 # Validations handles newlines in time and datetime validation

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

Цвета обнови решението на 25.11.2012 14:02 (преди почти 12 години)

+class PrivacyFilter
+ attr_accessor :preserve_phone_country_code
+ attr_accessor :preserve_email_hostname
+ attr_accessor :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
+ if preserve_phone_country_code == true then phone_filtered
+ elsif preserve_email_hostname == true then email_filtered
+ elsif partially_preserve_email_username == true then username_filtered
+ else simply_filtered
+ end
+ end
+
+ def phone_filtered
+ phone = /((00|\+)[1-9]\d{0,2})(([-\(\)\s]{0,2}\d){6,11})/.match @text
+ if phone == nil
+ simply_phone_filtrered
+ else
+ result = phone.pre_match + $1 + ' [FILTERED]' + PrivacyFilter.new(phone.post_match).filtered
+ end
+ end
+
+ def email_filtered
+ name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200})/.match @text
+ if /(@([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text and name != nil
+ result = name.pre_match + '[FILTERED]' + PrivacyFilter.new(name.post_match).filtered
+ else
+ @text
+ end
+ end
+
+ def username_filtered
+ name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200})/.match @text
+ if $1.size < 6 or name == nil
+ email_filtered
+ else
+ result = name.pre_match + $1[0] + $1[1] + $1[2] + '[FILTERED]' + PrivacyFilter.new(name.post_match).filtered
+ end
+ end
+
+ def simply_filtered
+ if /((((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11})/ =~ @text
+ simply_phone_filtered
+ else
+ simply_email_filtered
+ end
+ end
+
+ def simply_phone_filtered
+ phone = /((((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11})/.match @text
+ if phone != nil
+ result = phone.pre_match + '[PHONE]' + PrivacyFilter.new(phone.post_match).filtered
+ else
+ @text
+ end
+ end
+
+ def simply_email_filtered
+ hostname = /(([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text
+ if (name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@)/.match @text) != nil and hostname != nil
+ result = name.pre_match + '[EMAIL]' + PrivacyFilter.new(hostname.post_match).filtered
+ else
+ @text
+ end
+ end
+end
+
+class Validations
+ def Validations.email?(value)
+ if (name = /\A[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@/.match value) and
+ Validations.hostname?(name.post_match)
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.phone?(value)
+ if /\A(((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11}\z/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.hostname?(value)
+ if /\A([a-zA-Z0-9][a-zA-Z0-9-]{0,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?\z/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.ip_address?(value)
+ if (ip = /\A(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\z/.match value) and
+ ip[1].split('.').map{ |number| Integer(number) < 256 }.count{ |item| item == true } == 4
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.number?(value)
+ if /\A-?((0(\.\d+)?)|[1-9]\d*(\.\d+)?)\z/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.integer?(value)
+ if /\A-?(0|[1-9]\d*)\z/ =~ value
+ true
+ else
+ false
+ end
+ end
+
+ def Validations.date?(value)
+ if (match = /\A(\d{4}-\d{2}-\d{2})\z/.match value)
+ date = match[1].split('-').map{ |number| Integer(number) }
+ date[1] > 0 and date[1] < 13 and date[2] > 0 and date[2] < 32
+ end
+ end
+
+ def Validations.time?(value)
+ if (match = /\A(\d{2}:\d{2}:\d{2})\z/.match value)
+ time = match[1].split(':').map{ |number| Integer(number) }
+ time[0] >= 0 and time[0] <24 and time[1] >= 0 and time[1] < 60 and time[2] >= 0 and time[2] < 60
+ end
+ end
+
+ def Validations.date_time?(value)
+ if (Validations.date?(value.split[0]) and Validations.time?(value.split[1])) or
+ (Validations.date?(value.split('T')[0]) and Validations.time?(value.split('T')[1]))
+ true
+ else
+ false
+ end
+ end
+end

Цвета обнови решението на 26.11.2012 10:08 (преди почти 12 години)

class PrivacyFilter
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :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
if preserve_phone_country_code == true then phone_filtered
elsif preserve_email_hostname == true then email_filtered
elsif partially_preserve_email_username == true then username_filtered
else simply_filtered
end
end
def phone_filtered
phone = /((00|\+)[1-9]\d{0,2})(([-\(\)\s]{0,2}\d){6,11})/.match @text
if phone == nil
simply_phone_filtrered
else
result = phone.pre_match + $1 + ' [FILTERED]' + PrivacyFilter.new(phone.post_match).filtered
end
end
def email_filtered
name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200})/.match @text
if /(@([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text and name != nil
result = name.pre_match + '[FILTERED]' + PrivacyFilter.new(name.post_match).filtered
else
@text
end
end
def username_filtered
name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200})/.match @text
if $1.size < 6 or name == nil
email_filtered
else
result = name.pre_match + $1[0] + $1[1] + $1[2] + '[FILTERED]' + PrivacyFilter.new(name.post_match).filtered
end
end
def simply_filtered
- if /((((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11})/ =~ @text
- simply_phone_filtered
- else
- simply_email_filtered
- end
+ result = simply_email_filtered
+ PrivacyFilter.new(result).simply_phone_filtered
end
def simply_phone_filtered
phone = /((((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11})/.match @text
if phone != nil
result = phone.pre_match + '[PHONE]' + PrivacyFilter.new(phone.post_match).filtered
else
@text
end
end
def simply_email_filtered
hostname = /(([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text
if (name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@)/.match @text) != nil and hostname != nil
- result = name.pre_match + '[EMAIL]' + PrivacyFilter.new(hostname.post_match).filtered
+ result = PrivacyFilter.new(name.pre_match)filtered + '[EMAIL]' + PrivacyFilter.new(hostname.post_match).filtered
else
@text
end
end
end
class Validations
def Validations.email?(value)
if (name = /\A[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@/.match value) and
Validations.hostname?(name.post_match)
true
else
false
end
end
def Validations.phone?(value)
if /\A(((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11}\z/ =~ value
true
else
false
end
end
def Validations.hostname?(value)
if /\A([a-zA-Z0-9][a-zA-Z0-9-]{0,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?\z/ =~ value
true
else
false
end
end
def Validations.ip_address?(value)
if (ip = /\A(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\z/.match value) and
ip[1].split('.').map{ |number| Integer(number) < 256 }.count{ |item| item == true } == 4
true
else
false
end
end
def Validations.number?(value)
if /\A-?((0(\.\d+)?)|[1-9]\d*(\.\d+)?)\z/ =~ value
true
else
false
end
end
def Validations.integer?(value)
if /\A-?(0|[1-9]\d*)\z/ =~ value
true
else
false
end
end
def Validations.date?(value)
if (match = /\A(\d{4}-\d{2}-\d{2})\z/.match value)
date = match[1].split('-').map{ |number| Integer(number) }
date[1] > 0 and date[1] < 13 and date[2] > 0 and date[2] < 32
end
end
def Validations.time?(value)
if (match = /\A(\d{2}:\d{2}:\d{2})\z/.match value)
time = match[1].split(':').map{ |number| Integer(number) }
time[0] >= 0 and time[0] <24 and time[1] >= 0 and time[1] < 60 and time[2] >= 0 and time[2] < 60
end
end
def Validations.date_time?(value)
if (Validations.date?(value.split[0]) and Validations.time?(value.split[1])) or
(Validations.date?(value.split('T')[0]) and Validations.time?(value.split('T')[1]))
true
else
false
end
end
end

Цвета обнови решението на 27.11.2012 00:01 (преди почти 12 години)

class PrivacyFilter
attr_accessor :preserve_phone_country_code
attr_accessor :preserve_email_hostname
attr_accessor :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
- if preserve_phone_country_code == true then phone_filtered
- elsif preserve_email_hostname == true then email_filtered
- elsif partially_preserve_email_username == true then username_filtered
- else simply_filtered
- end
+ if partially_preserve_email_username == true then @text = username_filtered(1) end
+ if preserve_email_hostname == true then @text = email_filtered(0) end
+ if preserve_phone_country_code == true then @text = phone_filtered(1) end
+ simply_filtered
end
- def phone_filtered
- phone = /((00|\+)[1-9]\d{0,2})(([-\(\)\s]{0,2}\d){6,11})/.match @text
- if phone == nil
- simply_phone_filtrered
+ def username_filtered(flag)
+ host = '(@([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)'
+ if (name = (/([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200})#{host}/.match @text)) == nil or $1.size < 6 or flag == 0
+ email_filtered(flag)
else
- result = phone.pre_match + $1 + ' [FILTERED]' + PrivacyFilter.new(phone.post_match).filtered
+ result = name.pre_match + $1[0,3] + '[FILTERED]' + $2 + PrivacyFilter.new(name.post_match).username_filtered(flag)
end
end
- def email_filtered
- name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200})/.match @text
- if /(@([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text and name != nil
- result = name.pre_match + '[FILTERED]' + PrivacyFilter.new(name.post_match).filtered
+ def email_filtered(flag)
+ name = /[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}/.match @text
+ if (host = /(@([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text) and name != nil
+ result = name.pre_match + '[FILTERED]' + $1 + PrivacyFilter.new(host.post_match).username_filtered(flag)
else
@text
end
end
- def username_filtered
- name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200})/.match @text
- if $1.size < 6 or name == nil
- email_filtered
+ def phone_filtered(flag)
+ phone = /((00|\+)[1-9]\d{0,2})(([-\(\)\s]{0,2}\d){6,11})/.match @text
+ if phone == nil or flag == 0
+ simply_phone_filtered(flag)
else
- result = name.pre_match + $1[0] + $1[1] + $1[2] + '[FILTERED]' + PrivacyFilter.new(name.post_match).filtered
+ result = phone.pre_match + $1 + ' [FILTERED]' + PrivacyFilter.new(phone.post_match).phone_filtered(flag)
end
end
def simply_filtered
result = simply_email_filtered
- PrivacyFilter.new(result).simply_phone_filtered
+ PrivacyFilter.new(result).simply_phone_filtered(1)
end
- def simply_phone_filtered
+ def simply_phone_filtered(flag)
phone = /((((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11})/.match @text
if phone != nil
- result = phone.pre_match + '[PHONE]' + PrivacyFilter.new(phone.post_match).filtered
+ result = phone.pre_match + '[PHONE]' + PrivacyFilter.new(phone.post_match).phone_filtered(flag)
else
@text
end
end
def simply_email_filtered
hostname = /(([a-zA-Z0-9][a-zA-Z0-9-]{,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?)/.match @text
if (name = /([a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@)/.match @text) != nil and hostname != nil
- result = PrivacyFilter.new(name.pre_match)filtered + '[EMAIL]' + PrivacyFilter.new(hostname.post_match).filtered
+ result = PrivacyFilter.new(name.pre_match).filtered + '[EMAIL]' + PrivacyFilter.new(hostname.post_match).filtered
else
@text
end
end
end
class Validations
def Validations.email?(value)
if (name = /\A[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@/.match value) and
Validations.hostname?(name.post_match)
true
else
false
end
end
def Validations.phone?(value)
if /\A(((00|\+)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){6,11}\z/ =~ value
true
else
false
end
end
def Validations.hostname?(value)
if /\A([a-zA-Z0-9][a-zA-Z0-9-]{0,60}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z])?\z/ =~ value
true
else
false
end
end
def Validations.ip_address?(value)
if (ip = /\A(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\z/.match value) and
- ip[1].split('.').map{ |number| Integer(number) < 256 }.count{ |item| item == true } == 4
+ ip[1].split('.').map{ |number| Integer(number) < 256 }.count{ |item| item == true } == 4
true
else
false
end
end
def Validations.number?(value)
if /\A-?((0(\.\d+)?)|[1-9]\d*(\.\d+)?)\z/ =~ value
true
else
false
end
end
def Validations.integer?(value)
if /\A-?(0|[1-9]\d*)\z/ =~ value
true
else
false
end
end
def Validations.date?(value)
if (match = /\A(\d{4}-\d{2}-\d{2})\z/.match value)
date = match[1].split('-').map{ |number| Integer(number) }
date[1] > 0 and date[1] < 13 and date[2] > 0 and date[2] < 32
end
end
def Validations.time?(value)
if (match = /\A(\d{2}:\d{2}:\d{2})\z/.match value)
time = match[1].split(':').map{ |number| Integer(number) }
time[0] >= 0 and time[0] <24 and time[1] >= 0 and time[1] < 60 and time[2] >= 0 and time[2] < 60
end
end
def Validations.date_time?(value)
if (Validations.date?(value.split[0]) and Validations.time?(value.split[1])) or
(Validations.date?(value.split('T')[0]) and Validations.time?(value.split('T')[1]))
true
else
false
end
end
end