Class: PayCallSms::PhoneNumberUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/pay_call_sms/phone_number_utils.rb

Constant Summary collapse

@@valid_lengths =
{
  :cellular => '545123456'.length,
  :land_line => ['31235678'.length, '777078406'.length]
}
@@country_code =
'972'

Class Method Summary collapse

Class Method Details

.ensure_country_code(phone) ⇒ Object

this method adds 972 country code to given phone if needed if phone is blank –> doesn’t change it



12
13
14
15
16
17
18
# File 'lib/pay_call_sms/phone_number_utils.rb', line 12

def self.ensure_country_code(phone)
  if !phone.blank? && !phone.start_with?(@@country_code)
    phone = phone[1..phone.size] if phone.start_with?('0')
    phone = "972#{phone}"
  end
  phone
end

.phone_number_to_id_string(phone) ⇒ Object

this method will convert given phone number to base 36 string if phone contains digits only if phone contains digits and letters it will leave it untouched



52
53
54
55
# File 'lib/pay_call_sms/phone_number_utils.rb', line 52

def self.phone_number_to_id_string(phone)
  phone = phone.to_i.to_s(36) if phone =~ /^[0-9]+$/
  phone
end

.valid_cellular_phone?(phone) ⇒ Boolean

validates that given phone is Israeli cellular format with country code: 972545123456

Returns:

  • (Boolean)


25
26
27
# File 'lib/pay_call_sms/phone_number_utils.rb', line 25

def self.valid_cellular_phone?(phone)
  valid_phone_length?(phone, @@valid_lengths[:cellular])
end

.valid_land_line_phone?(phone) ⇒ Boolean

validates that given phone is Israeli landline format with country code: 972545123456

Returns:

  • (Boolean)


30
31
32
# File 'lib/pay_call_sms/phone_number_utils.rb', line 30

def self.valid_land_line_phone?(phone)
  valid_phone_length?(phone, @@valid_lengths[:land_line].first) || valid_phone_length?(phone, @@valid_lengths[:land_line].last)
end

.valid_phone_length?(phone, length) ⇒ Boolean

make sure phone is in given length and starts with country code

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/pay_call_sms/phone_number_utils.rb', line 45

def self.valid_phone_length?(phone, length)
  phone = phone.to_s
  phone.start_with?(@@country_code) && phone =~ /^#{@@country_code}[0-9]{#{length}}$/
end

.valid_sender_name?(sender_name) ⇒ Boolean

valid sender name is between 2 and 11 latin chars or digits

Returns:

  • (Boolean)


40
41
42
# File 'lib/pay_call_sms/phone_number_utils.rb', line 40

def self.valid_sender_name?(sender_name)
  sender_name =~ /^[a-z0-9]{2,11}$/i
end

.valid_sender_number?(sender_number) ⇒ Boolean

valid sender number is between 4 and 14 digits

Returns:

  • (Boolean)


35
36
37
# File 'lib/pay_call_sms/phone_number_utils.rb', line 35

def self.valid_sender_number?(sender_number)
  sender_number =~ /^[0-9]{4,14}$/i
end

.without_country_code(phone) ⇒ Object



20
21
22
# File 'lib/pay_call_sms/phone_number_utils.rb', line 20

def self.without_country_code(phone)
  phone.start_with?('972') ? phone.gsub('972', '0') : phone
end