Class: Loqate::Email::Gateway

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

Overview

Validates the existence of email addresses.

Constant Summary collapse

BATCH_VALIDATE_ENDPOINT =
'/EmailValidation/Batch/Validate/v1.20/json3.ws'.freeze
VALIDATE_ENDPOINT =
'/EmailValidation/Interactive/Validate/v2.00/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 an email gateway.

Parameters:

  • client (Client)

    The client responsible for the HTTP interactions



22
23
24
25
26
# File 'lib/loqate/email/gateway.rb', line 22

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

Instance Method Details

#batch_validate(options) ⇒ Result

Verifies up to 100 emails per batch.

Examples:

result = email_gateway.batch_validate(emails: %w[[email protected] [email protected]])
result.value # => [#<Loqate::Email::BatchEmailValidation status="Invalid" ...]

Parameters:

  • options (Hash)

    The options to validate an email address.

Options Hash (options):

  • :emails (Array<String>)

    The email addresses to verify. Maximum 100 records.

Returns:

  • (Result)

    A result wrapping a list of email address validations



77
78
79
80
81
82
# File 'lib/loqate/email/gateway.rb', line 77

def batch_validate(options)
  options[:emails] = options[:emails].join(',') if options[:emails]
  response = client.get(BATCH_VALIDATE_ENDPOINT, options)

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

#batch_validate!(options) ⇒ Array<BatchEmailValidation>

Verifies up to 100 emails per batch.

Examples:

email_validations = email_gateway.batch_validate!(emails: %w[[email protected] [email protected]])

Parameters:

  • options (Hash)

    The options to validate an email address.

Options Hash (options):

  • :emails (Array<String>)

    The email addresses to verify. Maximum 100 records.

Returns:

Raises:

  • (Error)

    If the result is not a success



96
97
98
# File 'lib/loqate/email/gateway.rb', line 96

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

#validate(options) ⇒ Result

Verifies the existence of an email address.

Examples:

email_validation = email_gateway.validate(email: '[email protected]')

Parameters:

  • options (Hash)

    The options to validate an email address.

Options Hash (options):

  • :email (String)

    The email address to verify.

  • :timeout (Integer)

    The time (in milliseconds) you want to give for the validation attempt to be executed within. Value must be between 1 and 15000 (values outside of these ranges will fallback to the default of 15000).

Returns:

  • (Result)

    A result wrapping an email address validation



41
42
43
44
45
# File 'lib/loqate/email/gateway.rb', line 41

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

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

#validate!(options) ⇒ EmailValidation

Verifies the existence of an email address.

Examples:

email_validation = email_gateway.validate!(email: '[email protected]')

Parameters:

  • options (Hash)

    The options to validate an email address.

Options Hash (options):

  • :email (String)

    The email address to verify.

  • :timeout (Integer)

    The time (in milliseconds) you want to give for the validation attempt to be executed within. Value must be between 1 and 15000 (values outside of these ranges will fallback to the default of 15000).

Returns:

Raises:

  • (Error)

    If the result is not a success



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

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