Решение на Четвърта задача от Александър Иванов

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

Към профила на Александър Иванов

Резултати

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

Код

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 phone_filtered(text)
text.gsub(/(((00)|\+([1-9]\d{0,2}))|0)((\ |\(\)){0,2}\d){6,11}/) do |match|
($1 != '0' and @preserve_phone_country_code) ? "#{$1} [FILTERED]" : "[PHONE]"
end
end
def email_filtered(text)
text.gsub(
/([a-zA-Z0-9](\w|\-|\_|\+|\.){,200})\@(((\w)(\w|\-){0,61}(\w)\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)/) do |match|
case
when @partially_preserve_email_username and $1.length < 6 then "[FILTERED]@#{$3}"
when @partially_preserve_email_username then "#{$1[0..2]}[FILTERED]@#{$3}"
when @preserve_email_hostname ? then "[FILTERED]@#{$3}"
else "[EMAIL]"
end
end
end
def filtered
phone_filtered(email_filtered(@text))
end
end
module Validations
def self.email?(value)
email_beginning = value.match /^[a-zA-Z0-9](\w|\-|\_|\+|\.){,200}\@/
!!(email_beginning and hostname? value[value.index('@')+1..-1]) # value.match(/[[:alnum]]\w+\@/) {
end
def self.hostname?(value)
!!value.match(/^((\w)(\w|\-){0,61}(\w)\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$/)
end
def self.phone?(value)
!!value.match(/^(0|(00|\+[1-9]\d{0,2}))((\ |\(\)){0,2}\d){6,11}$/)
end
def self.ip_address?(value)
ip_match = value.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)
ip_match ? ip_match.captures.all? { |capture| capture.to_i >= 0 and capture.to_i <= 255} : false
end
def self.number?(value)
!!value.match(/^\-?(0|([1-9]\d*))(\.\d+)?$/)
end
def self.integer?(value)
!!value.match(/^\-?(0|([1-9]\d*))$/)
end
def self.date?(value)
!!value.match(/^\d{4}\-(0[1-9])|(1|[0-2])\-([0-2][0-9])|(3|([0-1]))$/)
end
def self.time?(value)
!!value.match(/^([0-1][0-9])|(2[0-3])\:[0-5][0-9]\:[0-5][0-9]$/)
end
def self.date_time?(value)
!!value.match(
/^\d{4}\-(0[1-9])|(1|[0-2])\-([0-2][0-9])|(3|([0-1]))(\ |\T)([0-1][0-9])|(2[0-3])\:[0-5][0-9]\:[0-5][0-9]$/)
end
end

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

/data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/configuration.rb:434:in `require': /tmp/d20130203-23049-xlktxa/solution.rb:21: syntax error, unexpected keyword_and, expecting keyword_then or ',' or ';' or '\n' (SyntaxError)
...lly_preserve_email_username and $1.length < 6 then "[FILTERE...
...                               ^
/tmp/d20130203-23049-xlktxa/solution.rb:21: syntax error, unexpected keyword_then, expecting keyword_end
...username and $1.length < 6 then "[FILTERED]@#{$3}"
...                               ^
/tmp/d20130203-23049-xlktxa/solution.rb:22: syntax error, unexpected keyword_when, expecting keyword_end
      when @partially_preserve_email_us...
          ^
/tmp/d20130203-23049-xlktxa/solution.rb:23: syntax error, unexpected keyword_when, expecting keyword_end
      when @preserve_email_hostname ?  ...
          ^
/tmp/d20130203-23049-xlktxa/solution.rb:24: syntax error, unexpected keyword_else, expecting keyword_end
      else                             ...
          ^
/tmp/d20130203-23049-xlktxa/solution.rb:32: syntax error, unexpected keyword_end, expecting $end
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/configuration.rb:434:in `block in requires='
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/configuration.rb:434:in `map'
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/configuration.rb:434:in `requires='
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/configuration_options.rb:20:in `block in configure'
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/configuration_options.rb:19:in `each'
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/configuration_options.rb:19:in `configure'
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/command_line.rb:21:in `run'
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/runner.rb:69:in `run'
	from /data/rails/evans-2012/shared/bundled_gems/ruby/1.9.1/gems/rspec-core-2.11.0/lib/rspec/core/runner.rb:8:in `block in autorun'

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

Александър обнови решението на 28.11.2012 15:55 (преди почти 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 phone_filtered(text)
+ text.gsub(/(((00)|\+([1-9]\d{0,2}))|0)((\ |\(\)){0,2}\d){6,11}/) do |match|
+ ($1 != '0' and @preserve_phone_country_code) ? "#{$1} [FILTERED]" : "[PHONE]"
+ end
+ end
+
+ def email_filtered(text)
+ text.gsub(
+ /([a-zA-Z0-9](\w|\-|\_|\+|\.){,200})\@(((\w)(\w|\-){0,61}(\w)\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)/) do |match|
+ case
+ when @partially_preserve_email_username and $1.length < 6 then "[FILTERED]@#{$3}"
+ when @partially_preserve_email_username then "#{$1[0..2]}[FILTERED]@#{$3}"
+ when @preserve_email_hostname ? then "[FILTERED]@#{$3}"
+ else "[EMAIL]"
+ end
+ end
+ end
+
+ def filtered
+ phone_filtered(email_filtered(@text))
+ end
+end
+
+module Validations
+ def self.email?(value)
+ email_beginning = value.match /^[a-zA-Z0-9](\w|\-|\_|\+|\.){,200}\@/
+ !!(email_beginning and hostname? value[value.index('@')+1..-1]) # value.match(/[[:alnum]]\w+\@/) {
+ end
+
+ def self.hostname?(value)
+ !!value.match(/^((\w)(\w|\-){0,61}(\w)\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$/)
+ end
+
+ def self.phone?(value)
+ !!value.match(/^(0|(00|\+[1-9]\d{0,2}))((\ |\(\)){0,2}\d){6,11}$/)
+ end
+
+ def self.ip_address?(value)
+ ip_match = value.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)
+ ip_match ? ip_match.captures.all? { |capture| capture.to_i >= 0 and capture.to_i <= 255} : false
+ end
+
+ def self.number?(value)
+ !!value.match(/^\-?(0|([1-9]\d*))(\.\d+)?$/)
+ end
+
+ def self.integer?(value)
+ !!value.match(/^\-?(0|([1-9]\d*))$/)
+ end
+
+ def self.date?(value)
+ !!value.match(/^\d{4}\-(0[1-9])|(1|[0-2])\-([0-2][0-9])|(3|([0-1]))$/)
+ end
+
+ def self.time?(value)
+ !!value.match(/^([0-1][0-9])|(2[0-3])\:[0-5][0-9]\:[0-5][0-9]$/)
+ end
+
+ def self.date_time?(value)
+ !!value.match(
+ /^\d{4}\-(0[1-9])|(1|[0-2])\-([0-2][0-9])|(3|([0-1]))(\ |\T)([0-1][0-9])|(2[0-3])\:[0-5][0-9]\:[0-5][0-9]$/)
+ end
+end