Class: Verifalia::REST::EmailValidations

Inherits:
Object
  • Object
show all
Defined in:
lib/rest/email_validations.rb

Constant Summary collapse

COMPLETION_MAX_RETRY =
5
COMPLETION_INTERVAL =
1

Instance Method Summary collapse

Constructor Details

#initialize(config, account_sid, account_token, args = {}) ⇒ EmailValidations

The Verifalia::REST::EmailValidations class allow you to comminucate with Email Validations Api. You don’t need to instantiate this class, but use the client for autoconfiguration. # The args parameter is a hash of configuration The following keys are supported:

unique_id: 'example-example'

The unique if of the Verifalia Email Validation resource



19
20
21
22
# File 'lib/rest/email_validations.rb', line 19

def initialize(config, , , args = {})
  @resources = build_resources(config, , )
  @unique_id = args[:unique_id] if args[:unique_id]
end

Instance Method Details

#completed?Boolean

Check if the Email validation entity is completed processed. In order to use this method you need to supply unique_id during initialization or call verify first.

Returns:

  • (Boolean)


128
129
130
# File 'lib/rest/email_validations.rb', line 128

def completed?
  @response.code == 200
end

#destroyObject

Destroy an Email Validations entity. In order to use this method you need to supply unique_id during initialization or call verify first. If request fail, you can call error to receive detailed information

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rest/email_validations.rb', line 106

def destroy
  raise ArgumentError, 'You must call verify first or supply and uniqueId' unless @unique_id
  begin
    r = multiplex_request do |resource|
      resource[@unique_id].delete
    end
    @error = nil
    @response = nil
    @query_result = nil
    @unique_id = nil
    true
  rescue => e
    return true if (e.is_a? RestClient::Exception && e.http_code == 410)
    compute_error(e)
    return false
  end
end

#errorObject



132
133
134
# File 'lib/rest/email_validations.rb', line 132

def error
  @error
end

#query(options = {}) ⇒ Object

Query the Email Validations Api for specific result. In order to use this method you need to supply unique_id uring initialization or call verify first. If request fail, you can call error to receive detailed information

<tt> options: { wait_for_completion: true, completion_max_retry: 5, completion_interval: 1(seconds) }

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rest/email_validations.rb', line 67

def query(options = {})
  raise ArgumentError, 'You must call verify first or supply and uniqueId' unless @unique_id
  opts = {
    wait_for_completion: false,
    completion_max_retry: COMPLETION_MAX_RETRY,
    completion_interval: COMPLETION_INTERVAL,
    }
    .merge! options
  if @query_result == nil || !completed?
    begin
      loop_count = 0
      loop do
        @response = multiplex_request do |resource|
          resource[@unique_id].get
        end
        @query_result = JSON.parse(@response)
        @error = nil
        loop_count += 1
        sleep opts[:completion_interval] if opts[:wait_for_completion]
        break if !opts[:wait_for_completion] || (completed? || loop_count >= opts[:completion_max_retry])
      end
    rescue => e
      compute_error(e)
      return false
    end
  end
  if (opts[:wait_for_completion] && !completed?)
    @error = :not_completed
    false
  else
    @query_result
  end
end

#verify(inputs, options = {}) ⇒ Object

Query the Email Validations Api with:

<tt> inputs: [‘[email protected]’]

<tt> inputs: data = [{ inputData: ‘[email protected]’ }, { inputData: ‘[email protected]’ } ]

<tt> options: { quality: ‘high’, priority: 100, deduplication: ‘safe’ } view verifalia API documentation

An array of emails to validate

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rest/email_validations.rb', line 33

def verify(inputs, options = {})
  raise ArgumentError, 'inputs must be not empty' if (inputs.nil? || inputs.empty?)
  raise ArgumentError, 'options must be hash' if (!options.is_a?(Hash))
  data = inputs.map do |input|
    if (input.is_a? String)
      { inputData: input }
    elsif (input.is_a? Hash)
      raise ArgumentError, 'if inputs content is a Hash you need to supply :inputData as key' if (!input.has_key?(:inputData))
      input
    else
      raise ArgumentError, 'inputs content must be a String or a Hash'
    end
  end
  content = ({ entries: data }.merge(options)).to_json
  begin
    @response = multiplex_request do |resource|
      resource.post content
    end
    @query_result = JSON.parse(@response)
    @unique_id = @query_result["uniqueID"]
    @error = nil
    @unique_id
  rescue => e
    compute_error(e)
    false
  end
end