You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.2 KiB
53 lines
1.2 KiB
4 weeks ago
|
class Contact
|
||
|
attr_reader :phone, :telegram, :email
|
||
|
|
||
|
def initialize(phone: nil, telegram: nil, email: nil)
|
||
|
@phone = phone
|
||
|
@telegram = telegram
|
||
|
@email = email
|
||
|
validate_contacts
|
||
|
end
|
||
|
|
||
|
def valid_phone_number?
|
||
|
return true if @phone.nil?
|
||
|
|
||
|
/\A\+?[0-9]{10,15}\z/.match?(@phone)
|
||
|
end
|
||
|
|
||
|
def valid_telegram?
|
||
|
return true if @telegram.nil?
|
||
|
|
||
|
/\A@[A-Za-z0-9_]{5,32}\z/.match?(@telegram)
|
||
|
end
|
||
|
|
||
|
def valid_email?
|
||
|
return true if @email.nil?
|
||
|
|
||
|
/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/.match?(@email)
|
||
|
end
|
||
|
|
||
|
def present?
|
||
|
@phone || @telegram || @email
|
||
|
end
|
||
|
|
||
|
def info
|
||
|
return "Phone: #{@phone}" if @phone
|
||
|
return "Telegram: #{@telegram}" if @telegram
|
||
|
return "Email: #{@email}" if @email
|
||
|
|
||
|
'No contact available'
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def validate_contacts
|
||
|
if !present?
|
||
|
raise ArgumentError, 'At least one contact (phone, telegram, or email) is required'
|
||
|
end
|
||
|
|
||
|
raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !valid_phone_number?
|
||
|
raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !valid_telegram?
|
||
|
raise ArgumentError, "Invalid email format: #{@email}" if @email && !valid_email?
|
||
|
end
|
||
|
end
|