Module: NotificationService::Sms::Validations

Included in:
Alibaba::Service, Kaleyra::Service
Defined in:
app/services/notification_service/sms/validations.rb

Overview

Description: Module to validate phone number before sending SMS

Instance Method Summary collapse

Instance Method Details

#fixed_line_number_type?(phone_number) ⇒ Boolean

Checks if the given phone number is of fixed line type.

Parameters:

  • phone_number (String)

    The phone number to be checked.

Returns:

  • (Boolean)

    Returns true if the phone number is of fixed line type, false otherwise.



38
39
40
41
# File 'app/services/notification_service/sms/validations.rb', line 38

def fixed_line_number_type?(phone_number)
  parsed_number = Phonelib.parse(phone_number)
  parsed_number.type == :fixed_line
end

#parse_phone_number_as_full_e164(phone) ⇒ String

Parses the given phone number and adds a plus prefix if it is missing.

Parameters:

  • phone (String)

    The phone number to parse.

Returns:

  • (String)

    The parsed phone number with a plus prefix.



47
48
49
# File 'app/services/notification_service/sms/validations.rb', line 47

def parse_phone_number_as_full_e164(phone)
  Phonelib.parse(phone).full_e164
end

#send_report_when_fail_send_sms?Boolean

this method returns false if the sms provider is the same as the service provider as example, if the sms provider is kaleyra and the service provider is kaleyra, it will return false otherwise, it will return true

Returns:

  • (Boolean)


7
8
9
10
# File 'app/services/notification_service/sms/validations.rb', line 7

def send_report_when_fail_send_sms?
  sms_provider = AdminSetting.sms_provider.to_sym
  sms_provider != @service_provider.to_sym
end

#valid_thai_phone_number?(phone_number) ⇒ Boolean

Checks if a given phone number is a valid Thai phone number.

Parameters:

  • phone_number (String)

    The phone number to validate.

Returns:

  • (Boolean)

    Returns true if the phone number is valid for Thailand, false otherwise.



29
30
31
32
# File 'app/services/notification_service/sms/validations.rb', line 29

def valid_thai_phone_number?(phone_number)
  phone = phone_number.to_s.split(',').map(&:strip).first.to_s
  Phonelib.valid_for_country?(phone, 'TH')
end

#valid_to_send_sms?(phone_number) ⇒ Boolean

Checks if the given phone number is valid to send an SMS.

Parameters:

  • phone_number (String)

    The phone number to validate.

Returns:

  • (Boolean)

    Returns true if the phone number is valid to send an SMS, false otherwise.



16
17
18
19
20
21
22
23
# File 'app/services/notification_service/sms/validations.rb', line 16

def valid_to_send_sms?(phone_number)
  parsed_phone_number = parse_phone_number_as_full_e164(phone_number)

  return false unless valid_thai_phone_number?(parsed_phone_number)
  return false if fixed_line_number_type?(parsed_phone_number)

  true
end