Phone ID - Tutorial: Cleanse a number with Telesign Ruby SDK

📘

NOTE:

This tutorial applies only to self-service accounts, not full-service accounts.

This tutorial walks you step-by-step through how to write code that makes a request to Telesign Phone ID using a Ruby SDK to cleanse a phone number. Phone number cleansing corrects common formatting issues in submitted phone numbers. Skip to the end of this page to see the full sample code.

This code will take your test phone number, add an extra digit to make it invalid, and send it to Phone ID to cleanse. It then takes the cleansed phone number from the response and prints it to the console.

Before you begin

Make sure you have the following ready:

📘

NOTE:

This tutorial uses the following:

  • MacOS
  • Ruby 3.3.6

Please modify accordingly if your developer environment differs from these details.

Set up your project

  1. Follow the Telesign Self-service Ruby SDK install instructions here on GitHub incorporating these details:

    • Use phoneidTest as the project name.

    You should end up in the top-level directory for your project in the Terminal.

  2. Create a new file called phone_id_cleanse_number.rb and open it in your editor.

    touch phone_id_cleanse_number.rb
    

Cleanse a number with Phone ID

  1. Follow the steps below or copy the sample code into the file and update it accordingly.
  2. Import this dependency from the SDK.
require 'telesign'
  1. Define variables to store your authentication credentials.
customer_id = ENV['CUSTOMER_ID'] || 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890'
api_key = ENV['API_KEY'] || 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=='
  1. Define a variable to hold the phone number you want to get information for. Use the complete number, including the country code. This should be a string with no spaces or special characters. For example: 16505551212.
phone_number = ENV['PHONE_NUMBER'] || '11234567890'

📘

NOTE:

In your production integration, pull the phone number from your recipient database instead of hardcoding it.

  1. This code adds an extra digit to the number to make it invalid.
extra_digit = '0'
incorrect_phone_number = "#{phone_number}#{extra_digit}"
  1. Instantiate a PhoneIdClient object and pass it your customer ID and your API key.
phoneid_client = Telesign::PhoneIdClient.new(customer_id, api_key)
  1. Use Phone ID to check and cleanse the incorrect phone number you created earlier.
response = phoneid_client.phoneid(incorrect_phone_number)
  1. Use two print statements to check the results of the cleansing procedure on the incorrect phone number. The first print statement explains what the cleansed phone number is and displays the JSON response for the cleansed number. The second print statement shows the original phone number and displays the JSON response for the original phone number.
if response.ok
    puts 'Cleansed phone number has country code %s and phone number is %s.' %
        [response.json['numbering']['cleansing']['call']['country_code'],
        response.json['numbering']['cleansing']['call']['phone_number']]

    puts 'Original phone number was %s.' %
        [response.json['numbering']['original']['complete_phone_number']]
end
  1. If everything works, you send an incorrect phone number to Phone ID for cleansing, and you get back a response containing your cleansed phone number and the original phone number.

Test your integration

  1. Switch from your editor to the terminal and run:
ruby phone_id_cleanse_number.rb

Sample code

require 'telesign'

customer_id = ENV['CUSTOMER_ID'] || 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890'
api_key = ENV['API_KEY'] || 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=='
phone_number = ENV['PHONE_NUMBER'] || '11234567890'

extra_digit = '0'
incorrect_phone_number = "#{phone_number}#{extra_digit}"

phoneid_client = Telesign::PhoneIdClient.new(customer_id, api_key)
response = phoneid_client.phoneid(incorrect_phone_number)

if response.ok
    puts 'Cleansed phone number has country code %s and phone number is %s.' %
        [response.json['numbering']['cleansing']['call']['country_code'],
        response.json['numbering']['cleansing']['call']['phone_number']]

    puts 'Original phone number was %s.' %
        [response.json['numbering']['original']['complete_phone_number']]
end