class Verifalia::REST::EmailValidations

Constants

COMPLETION_INTERVAL
COMPLETION_MAX_RETRY

Public Class Methods

new(config, account_sid, account_token, args = {}) click to toggle source

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

   # File lib/rest/email_validations.rb
19 def initialize(config, account_sid, account_token, args = {})
20   @resources = build_resources(config, account_sid, account_token)
21   @unique_id = args[:unique_id] if args[:unique_id]
22 end

Public Instance Methods

completed?() click to toggle source

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.

    # File lib/rest/email_validations.rb
128 def completed?
129   @response.code == 200
130 end
destroy() click to toggle source

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

    # File lib/rest/email_validations.rb
106 def destroy
107   raise ArgumentError, 'You must call verify first or supply and uniqueId' unless @unique_id
108   begin
109     r = multiplex_request do |resource|
110       resource[@unique_id].delete
111     end
112     @error = nil
113     @response = nil
114     @query_result = nil
115     @unique_id = nil
116     true
117   rescue => e
118     return true if (e.is_a? RestClient::Exception && e.http_code == 410)
119     compute_error(e)
120     return false
121   end
122 end
error() click to toggle source
    # File lib/rest/email_validations.rb
132 def error
133   @error
134 end
query(options = {}) click to toggle source

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) }

   # File lib/rest/email_validations.rb
67 def query(options = {})
68   raise ArgumentError, 'You must call verify first or supply and uniqueId' unless @unique_id
69   opts = {
70     wait_for_completion: false,
71     completion_max_retry: COMPLETION_MAX_RETRY,
72     completion_interval: COMPLETION_INTERVAL,
73     }
74     .merge! options
75   if @query_result == nil || !completed?
76     begin
77       loop_count = 0
78       loop do
79         @response = multiplex_request do |resource|
80           resource[@unique_id].get
81         end
82         @query_result = JSON.parse(@response)
83         @error = nil
84         loop_count += 1
85         sleep opts[:completion_interval] if opts[:wait_for_completion]
86         break if !opts[:wait_for_completion] || (completed? || loop_count >= opts[:completion_max_retry])
87       end
88     rescue => e
89       compute_error(e)
90       return false
91     end
92   end
93   if (opts[:wait_for_completion] && !completed?)
94     @error = :not_completed
95     false
96   else
97     @query_result
98   end
99 end
verify(inputs, options = {}) click to toggle source

Query the Email Validations Api with:

<tt> inputs: ['test@test.com']

<tt> inputs: data = [{ inputData: 'first@first.it' }, { inputData: 'first@first.it' } ]

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

An array of emails to validate

   # File lib/rest/email_validations.rb
33 def verify(inputs, options = {})
34   raise ArgumentError, 'inputs must be not empty' if (inputs.nil? || inputs.empty?)
35   raise ArgumentError, 'options must be hash' if (!options.is_a?(Hash))
36   data = inputs.map do |input|
37     if (input.is_a? String)
38       { inputData: input }
39     elsif (input.is_a? Hash)
40       raise ArgumentError, 'if inputs content is a Hash you need to supply :inputData as key' if (!input.has_key?(:inputData))
41       input
42     else
43       raise ArgumentError, 'inputs content must be a String or a Hash'
44     end
45   end
46   content = ({ entries: data }.merge(options)).to_json
47   begin
48     @response = multiplex_request do |resource|
49       resource.post content
50     end
51     @query_result = JSON.parse(@response)
52     @unique_id = @query_result["uniqueID"]
53     @error = nil
54     @unique_id
55   rescue => e
56     compute_error(e)
57     false
58   end
59 end

Private Instance Methods

build_resources(config, account_sid, account_token) click to toggle source
    # File lib/rest/email_validations.rb
179 def build_resources(config, account_sid, account_token)
180   opts = {
181     user: account_sid,
182     password: account_token,
183     headers: {
184       content_type: :json,
185       user_agent: "verifalia-rest-client/ruby/#{Verifalia::VERSION}"
186     }
187   }
188   config[:hosts].map do |host|
189     api_url = "#{host}/#{config[:api_version]}/email-validations"
190     RestClient::Resource.new api_url, opts
191   end
192 end
compute_error(e) click to toggle source
    # File lib/rest/email_validations.rb
152 def compute_error(e)
153   unless e.is_a? RestClient::Exception
154     @error = :internal_server_error
155   end
156 
157   case e.http_code
158     when 400
159       @error = :bad_request
160     when 401
161       @error = :unauthorized
162     when 402
163       @error = :payment_required
164     when 403
165       @error = :forbidden
166     when 404
167       @error = :not_found
168     when 406
169       @error = :not_acceptable
170     when 410
171       @error = :gone
172     when 429
173       @error = :too_many_request
174     else
175       @error = :internal_server_error
176     end
177 end
multiplex_request() { |resource| ... } click to toggle source
    # File lib/rest/email_validations.rb
138 def multiplex_request
139   @resources.shuffle.each do |resource|
140     begin
141       response = yield(resource)
142       return response
143     rescue => e
144       if ((e.is_a? RestClient::Exception) && (e.http_code != 500))
145         raise e
146       end
147     end
148   end
149   raise RestClient::Exception.new(nil, 500)
150 end