Решение на Четвърта задача от Изабела Недялкова

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

Към профила на Изабела Недялкова

Резултати

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

Код

class Validations
def self.email?(value)
regexp = /\A[[:alnum:]][-+_.[:alnum:]]{,200}@/
!!(value =~ regexp) and hostname? regexp.match(value).post_match
end
def self.hostname?(value)
!!(value =~ /\A(([[:alnum:]][-[:alnum:]]{0,61}[[:alnum]]|[[:alnum:]]{1,2})\.)+[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\z/)
end
def self.phone?(value)
!!(value =~ /\A((0{2}|\+)[1-9]\d{,2}|0)([- \(\)]?[- \(\)]?\d){6,11}\z/)
end
def self.ip_address?(value)
!!(value =~ /\A(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])){3}\z/)
end
def self.number?(value)
!!(value =~ /\A-?(0(\.\d+)?|[1-9]\d*(\.\d+)?)\z/)
end
def self.integer?(value)
!!(value =~ /\A-?(0|[1-9]\d*)/)
end
def self.date?(value)
!!(value =~ /\A\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\z/)
end
def self.time?(value)
!!(value =~ /\A([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\z/)
end
def self.date_time?(value)
regexp = /[ T]/
return false if regexp !~ value
date? regexp.match(value).pre_match and time? regexp.match(value).post_match
end
end
class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially
attr_accessor :text
def initialize(text)
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially = false
@text = text
end
def email_filter(text)
E = /(?<user>[[:alnum:]][-+_.[:alnum:]]{,200})@(?<host>(([[:alnum:]][-[:alnum:]]{0,61}
[[:alnum]]|[[:alnum:]]{1,2})\.)+[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?)/x
text.gsub(E) do |m| if partially and E[:user].length >= 6 then "#{E[:user][0..2]}[FILTERED]@#{E[:host]}" end
if preserve_email_hostname and partially then "[FILTERED]@#{EMAIL[:host]}"
else "[EMAIL]"
end
end
end
def phone_filter(text)
phone = /(?<code>(0{2}|\+)[1-9]\d{,2}|0)(?<number>([- \(\)]?[- \(\)]?\d){6,11})/
text.gsub(phone) do |match|
if preserve_phone_country_code then "[FILTERED]@#{phone[:number]}"
else "[PHONE]"
end
end
end
def filtered
text_filter = @text
phone_filter(email_filter(text_filter))
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-gow1zm/solution.rb:54: dynamic constant assignment (SyntaxError)
    E = /(?<user>[[:alnum:]][-+_.[:a...
       ^
	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'

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

Изабела обнови решението на 28.11.2012 16:53 (преди почти 12 години)

+class Validations
+ def self.email?(value)
+ regexp = /[[:alnum:]][-+_.[:alnum:]]{,200}@/
+ !!(value =~ regexp) and hostname? regexp.match(value).post_match
+ end
+
+ def self.hostname?(value)
+ !!(value =~ /(([[:alnum:]][-[:alnum:]]{0,61}[[:alnum]]|[[:alnum:]]{1,2})\.)+[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?/)
+ end
+
+ def self.phone?(value)
+ !!(value =~ /((0{2}|\+)[1-9]\d{,2}|0)([- \(\)]?[- \(\)]?\d){6,11}/)
+ end
+
+ def self.ip_address?(value)
+ !!(value =~ /(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])){3}/)
+ end
+
+ def self.number?(value)
+ !!(value =~ /-?(0(\.\d+)?|[1-9]\d*(\.\d+)?)/)
+ end
+
+ def self.integer?(value)
+ !!(value =~ /-?(0|[1-9]\d*)/)
+ end
+
+ def self.date?(value)
+ !!(value =~ /\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])/)
+ end
+
+ def self.time?(value)
+ !!(value =~ /([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/)
+ end
+
+ def self.date_time?(value)
+ regexp = /[ T]/
+ return false if regexp !~ value
+ date? regexp.match(value).pre_match and time? regexp.match(value).post_match
+ end
+end
+
+class PrivacyFilter < Validations
+ attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially
+ attr_accessor :text
+
+ def initialize(text)
+ @preserve_phone_country_code = false
+ @preserve_email_hostname = false
+ @partially = false
+ @text = text
+ end
+
+ def email_filter(text)
+ E = /(?<user>[[:alnum:]][-+_.[:alnum:]]{,200})@(?<host>(([[:alnum:]][-[:alnum:]]{0,61}
+ [[:alnum]]|[[:alnum:]]{1,2})\.)+[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?)/x
+ text.gsub(E) do |m| if partially and E[:user].length >= 6 then "#{E[:user][0..2]}[FILTERED]@#{E[:host]}" end
+ if preserve_email_hostname and partially then "[FILTERED]@#{EMAIL[:host]}"
+ else "[EMAIL]"
+ end
+ end
+ end
+
+ def phone_filter(text)
+ phone = /(?<code>(0{2}|\+)[1-9]\d{,2}|0)(?<number>([- \(\)]?[- \(\)]?\d){6,11})/
+ text.gsub(phone) do |match|
+ if preserve_phone_country_code then "[FILTERED]@#{phone[:number]}"
+ else "[PHONE]"
+ end
+ end
+ end
+
+ def filtered
+ text_filter = @text
+ phone_filter(email_filter(text_filter))
+ end
+end

Изабела обнови решението на 28.11.2012 16:56 (преди почти 12 години)

class Validations
def self.email?(value)
- regexp = /[[:alnum:]][-+_.[:alnum:]]{,200}@/
+ regexp = /\A[[:alnum:]][-+_.[:alnum:]]{,200}@/
!!(value =~ regexp) and hostname? regexp.match(value).post_match
end
def self.hostname?(value)
- !!(value =~ /(([[:alnum:]][-[:alnum:]]{0,61}[[:alnum]]|[[:alnum:]]{1,2})\.)+[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?/)
+ !!(value =~ /\A(([[:alnum:]][-[:alnum:]]{0,61}[[:alnum]]|[[:alnum:]]{1,2})\.)+[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\z/)
end
def self.phone?(value)
- !!(value =~ /((0{2}|\+)[1-9]\d{,2}|0)([- \(\)]?[- \(\)]?\d){6,11}/)
+ !!(value =~ /\A((0{2}|\+)[1-9]\d{,2}|0)([- \(\)]?[- \(\)]?\d){6,11}\z/)
end
def self.ip_address?(value)
- !!(value =~ /(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])){3}/)
+ !!(value =~ /\A(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])){3}\z/)
end
def self.number?(value)
- !!(value =~ /-?(0(\.\d+)?|[1-9]\d*(\.\d+)?)/)
+ !!(value =~ /\A-?(0(\.\d+)?|[1-9]\d*(\.\d+)?)\z/)
end
def self.integer?(value)
- !!(value =~ /-?(0|[1-9]\d*)/)
+ !!(value =~ /\A-?(0|[1-9]\d*)/)
end
def self.date?(value)
- !!(value =~ /\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])/)
+ !!(value =~ /\A\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\z/)
end
def self.time?(value)
- !!(value =~ /([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/)
+ !!(value =~ /\A([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\z/)
end
def self.date_time?(value)
regexp = /[ T]/
return false if regexp !~ value
date? regexp.match(value).pre_match and time? regexp.match(value).post_match
end
end
-class PrivacyFilter < Validations
+class PrivacyFilter
attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially
attr_accessor :text
def initialize(text)
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially = false
@text = text
end
def email_filter(text)
E = /(?<user>[[:alnum:]][-+_.[:alnum:]]{,200})@(?<host>(([[:alnum:]][-[:alnum:]]{0,61}
[[:alnum]]|[[:alnum:]]{1,2})\.)+[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?)/x
text.gsub(E) do |m| if partially and E[:user].length >= 6 then "#{E[:user][0..2]}[FILTERED]@#{E[:host]}" end
if preserve_email_hostname and partially then "[FILTERED]@#{EMAIL[:host]}"
else "[EMAIL]"
end
end
end
def phone_filter(text)
phone = /(?<code>(0{2}|\+)[1-9]\d{,2}|0)(?<number>([- \(\)]?[- \(\)]?\d){6,11})/
text.gsub(phone) do |match|
if preserve_phone_country_code then "[FILTERED]@#{phone[:number]}"
else "[PHONE]"
end
end
end
def filtered
text_filter = @text
phone_filter(email_filter(text_filter))
end
end