Class: PhoneValidator::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/genderapi-phone-validator/client.rb

Overview

PhoneValidator::Client

Ruby SDK for the Phone Number Validation & Formatter API provided by GenderAPI.

This class allows you to:

  • Validate international phone numbers
  • Detect number type (mobile, landline, VoIP, etc.)
  • Retrieve region and country metadata
  • Format numbers to E.164 or national format

Examples:

Basic usage

client = PhoneValidator::Client.new(api_key: "your_api_key")
result = client.validate(number: "+1 212 867 5309", address: "US")
puts result["e164"]

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: nil) ⇒ Client

Initializes a new client instance.

Parameters:

  • api_key (String)

    Your GenderAPI API key as a Bearer token.

  • base_url (String) (defaults to: nil)

    Optional override for the base URL (defaults to api.genderapi.io).



33
34
35
36
37
38
39
40
# File 'lib/genderapi-phone-validator/client.rb', line 33

def initialize(api_key:, base_url: nil)
  @api_key = api_key
  self.class.base_uri(base_url) if base_url
  @headers = {
    "Authorization" => "Bearer #{@api_key}",
    "Content-Type" => "application/json"
  }
end

Instance Method Details

#validate(number:, address: "") ⇒ Hash

Validates and formats a phone number.

Examples:

Validate number

client.validate(number: "+1 212 867 5309", address: "US")

Parameters:

  • number (String)

    Phone number in any format (required).

  • address (String) (defaults to: "")

    Optional. Country code (e.g., 'US'), full country name, or city name to improve accuracy.

Returns:

  • (Hash)

    The parsed JSON response as a Ruby Hash.



53
54
55
56
57
58
59
60
# File 'lib/genderapi-phone-validator/client.rb', line 53

def validate(number:, address: "")
  payload = {
    number: number,
    address: address
  }

  _post_request("/api/phone", payload)
end