|
|
|
class Contact
|
|
|
|
attr_reader :phone, :telegram, :email
|
|
|
|
|
|
|
|
def initialize(args = {})
|
|
|
|
self.phone = args[:phone]
|
|
|
|
self.telegram = args[:telegram]
|
|
|
|
self.email = args[:email]
|
|
|
|
|
|
|
|
raise ArgumentError, "Необходимо указать хотя бы один контакт (телефон, Telegram или email)" unless present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def present?
|
|
|
|
@phone || @telegram || @email
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_single_contact
|
|
|
|
return "Phone: #{@phone}" if @phone
|
|
|
|
return "Telegram: #{@telegram}" if @telegram
|
|
|
|
return "Email: #{@email}" if @email
|
|
|
|
|
|
|
|
'No contact available'
|
|
|
|
end
|
|
|
|
|
|
|
|
def phone=(phone)
|
|
|
|
if phone && !self.class.valid_phone_number?(phone)
|
|
|
|
raise ArgumentError, "Недопустимый номер телефона: #{phone}"
|
|
|
|
end
|
|
|
|
@phone = phone
|
|
|
|
end
|
|
|
|
|
|
|
|
def telegram=(telegram)
|
|
|
|
if telegram && !self.class.valid_telegram?(telegram)
|
|
|
|
raise ArgumentError, "Некорректный Telegram: #{telegram}"
|
|
|
|
end
|
|
|
|
@telegram = telegram
|
|
|
|
end
|
|
|
|
|
|
|
|
def email=(email)
|
|
|
|
if email && !self.class.valid_email?(email)
|
|
|
|
raise ArgumentError, "Некорректный email: #{email}"
|
|
|
|
end
|
|
|
|
@email = email
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_phone_number?(phone)
|
|
|
|
/\A\+?[0-9]{9,15}\z/.match?(phone)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_telegram?(telegram)
|
|
|
|
/\A@[A-Za-z0-9_]{5,32}\z/.match?(telegram)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_email?(email)
|
|
|
|
/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/.match?(email)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.new_from_info(info_string)
|
|
|
|
info_string = info_string.sub(/^Contact: /, '').strip
|
|
|
|
|
|
|
|
case info_string
|
|
|
|
when /\APhone: (.+)\z/i
|
|
|
|
new(phone: Regexp.last_match(1).strip)
|
|
|
|
when /\ATelegram: (.+)\z/i
|
|
|
|
new(telegram: Regexp.last_match(1).strip)
|
|
|
|
when /\AEmail: (.+)\z/i
|
|
|
|
new(email: Regexp.last_match(1).strip)
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Некорректный формат строки контакта: #{info_string}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|