Class: QrForge::Payloads::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/qr_forge/payloads/phone.rb

Overview

Represents a telephone payload

Examples:

return “example.com” or “example.com


Instance Method Summary collapse

Constructor Details

#initialize(phone_number) ⇒ Phone



9
10
11
# File 'lib/qr_forge/payloads/phone.rb', line 9

def initialize(phone_number)
  @phone_number = phone_number
end

Instance Method Details

#to_sObject



13
14
15
# File 'lib/qr_forge/payloads/phone.rb', line 13

def to_s
  "tel:#{@phone_number}"
end

#validate!Object

Validates that the passed phone number is in a valid format.

Examples:

valid phones:
  +919367788755
  8989829304
  +16308520397
  786-307-3615
  555.555.5555

Raises:



26
27
28
29
30
31
# File 'lib/qr_forge/payloads/phone.rb', line 26

def validate!
  # credit: https://ihateregex.io/expr/phone/
  return if @phone_number =~ /^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/

  raise PayloadValidationError, "Invalid phone number format"
end