Class: Loqate::Phone::Gateway

Inherits:
Object
  • Object
show all
Includes:
Result::Mixin
Defined in:
lib/loqate/phone/gateway.rb

Overview

Starts a new phone number validation request.

Constant Summary collapse

VALIDATE_ENDPOINT =
'/PhoneNumberValidation/Interactive/Validate/v2.20/json3.ws'.freeze

Constants included from Result::Mixin

Result::Mixin::Failure, Result::Mixin::Success

Instance Method Summary collapse

Methods included from Result::Mixin

#Failure, #Success, #unwrap_result_or_raise

Constructor Details

#initialize(client) ⇒ Gateway

Creates a phone gateway

Parameters:

  • client (Client)

    The client responsible for the HTTP interactions



20
21
22
23
24
# File 'lib/loqate/phone/gateway.rb', line 20

def initialize(client)
  @client       = client
  @mapper       = Mappers::GenericMapper.new
  @error_mapper = Mappers::ErrorMapper.new
end

Instance Method Details

#validate(options) ⇒ Result

Validates phone numbers.

Examples:

phone_validation = phone_gateway.validate(phone: '447440019210', country: 'GB')

Parameters:

  • options (Hash)

    The options to validate a phone number.

Options Hash (options):

  • :phone (String)

    The mobile/cell phone number to verify. This must be in international format (+447528471411 or 447528471411) if no country code is provided or national format with a Country parameter provided (07528471411 and GB as the Country parameter).

  • :country (String)

    The ISO2 country code of the number you are trying to validate (if provided in national format).

Returns:

  • (Result)

    A result wrapping a phone number validation



40
41
42
43
44
# File 'lib/loqate/phone/gateway.rb', line 40

def validate(options)
  response = client.get(VALIDATE_ENDPOINT, options)

  response.errors? && build_error_from(response.items.first) || build_phone_validation_from(response.items.first)
end

#validate!(options) ⇒ PhoneNumberValidation

Validates phone numbers.

Examples:

phone_validation = phone_gateway.validate(phone: '447440019210', country: 'GB')

Parameters:

  • options (Hash)

    The options to validate a phone number.

Options Hash (options):

  • :phone (String)

    The mobile/cell phone number to verify. This must be in international format (+447528471411 or 447528471411) if no country code is provided or national format with a Country parameter provided (07528471411 and GB as the Country parameter).

  • :country (String)

    The ISO2 country code of the number you are trying to validate (if provided in national format).

Returns:

Raises:

  • (Error)

    If the result is not a success



62
63
64
# File 'lib/loqate/phone/gateway.rb', line 62

def validate!(options)
  unwrap_result_or_raise { validate(options) }
end