Class: Verifalia::EmailValidations::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/verifalia/email_validation/client.rb

Overview

Allows to verify email addresses and manage email verification jobs using the Verifalia service.

Instance Method Summary collapse

Constructor Details

#initialize(rest_client) ⇒ Client

Returns a new instance of Client.



45
46
47
# File 'lib/verifalia/email_validation/client.rb', line 45

def initialize(rest_client)
  @rest_client = rest_client
end

Instance Method Details

#delete(id) ⇒ Object

Deletes an email validation job previously submitted for processing.

Parameters:

  • id (String)

    The ID of the email validation job to delete.



227
228
229
230
231
232
233
234
# File 'lib/verifalia/email_validation/client.rb', line 227

def delete(id)
  response = @rest_client.invoke 'delete',
                                 "email-validations/#{id}"

  return if response.status == 200 || response.status == 410

  raise "Unexpected HTTP response: #{response.status} #{response.body}"
end

#export(id, format) ⇒ String

Exports the validated entries for a completed email validation job, using the specified output format. See ExportedEntriesFormat for a list of the values supported at the time this SDK has been released.

Supported formats:

  • Comma-Separated Values (CSV)

  • Microsoft Excel (.xlsx)

  • Microsoft Excel 97-2003 (.xls)

Parameters:

  • id (String)

    The ID of the email validation job to retrieve.

  • format (String)

    The MIME content type of the desired output file format.

Returns:

  • (String)

    The exported data.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/verifalia/email_validation/client.rb', line 209

def export(id, format)
  response = @rest_client.invoke 'get',
                                 "email-validations/#{id}/entries",
                                 {
                                   headers:
                                     {
                                       'Accept': format
                                     }
                                 }

  return nil if response.status == 404 || response.status == 410
  return response.body if response.status == 200

  raise "Unexpected HTTP response: #{response.status} #{response.body}"
end

#get(id, wait_options: nil) ⇒ Verifalia::EmailValidations::Job

Returns an email validation job previously submitted for processing.

By default, this method waits for the completion of the email validation job: pass a WaitOptions to request a different waiting behavior.

Parameters:

  • id (String)

    The ID of the email validation job to retrieve.

  • wait_options (nil) (defaults to: nil)

    The options which rule out how to wait for the completion of the email validation.

Returns:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/verifalia/email_validation/client.rb', line 171

def get(id, wait_options: nil)
  wait_options_or_default = wait_options.nil? ? WaitOptions.default : wait_options

  loop do
    response = @rest_client.invoke 'get',
                                   "email-validations/#{id}?waitTime=#{wait_options_or_default.poll_wait_time}"

    return nil if response.status == 404 || response.status == 410

    unless response.status == 202 || response.status == 200
      raise "Unexpected HTTP response: #{response.status} #{response.body}"
    end

    job = Job.from_json(JSON.parse(response.body))

    return job if wait_options_or_default == WaitOptions.no_wait || response.status == 200

    # Fires a progress, since we are not yet completed

    wait_options.progress&.call(job.overview)

    # Wait for the next polling schedule

    wait_options.wait_for_next_poll(job)
  end
end

#submit(data, quality: nil, priority: nil, deduplication: nil, name: nil, retention: nil, completion_callback: nil, wait_options: nil) ⇒ Verifalia::EmailValidations::Job

Submits a new email validation for processing.

By default, this method waits for the completion of the email validation job: pass a WaitOptions to request a different waiting behavior.

Parameters:

  • data

    The input data to verify.

  • quality (nil) (defaults to: nil)

    The desired results quality for this email validation.

  • priority (nil) (defaults to: nil)
  • deduplication (nil) (defaults to: nil)

    An optional string with the name of the algorithm our engine will use to scrub the list of email addresses and remove its duplicates. The following values are currently supported: Off does not mark duplicated email addresses, Safe mark duplicated email addresses with an algorithm which guarantees no false duplicates are returned, Relaxed mark duplicated email addresses using a set of relaxed rules which assume the target email service providers are configured with modern settings only. If not specified, Verifalia will not mark duplicated email addresses.

  • name (nil) (defaults to: nil)

    An optional user-defined name for the validation job, for your own reference.

  • retention (nil) (defaults to: nil)

    An optional string with the desired data retention period to observe for the validation job, expressed in the format dd.hh:mm:ss (where dd. days, hh: hours, mm: minutes, ss: seconds); the initial dd. part is added only for periods of more than 24 hours. The value has a minimum of 5 minutes (0:5:0) and a maximum of 30 days (30.0:0:0): Verifalia will delete the job and its data once its data retention period is over, starting to count when it gets completed. If not specified, Verifalia falls back to the configured data retention period of the submitting user / browser app and, should it be unset, to the configured data retention period of the Verifalia account, with a default of 30 days.

  • completion_callback (nil) (defaults to: nil)

    An optional hash describing the desired completion callback behavior, with the following keys: url A string with the URL Verifalia will invoke once the job results are ready. See how to handle completion callbacks. version An optional string which defines the callback schema our dispatcher must obey when invoking the provided callback URL. Valid values are: 1.0 the callback includes the completed job ID. 1.1 everything included with 1.0 plus the job name. If not specified, Verifalia will use the most recent schema available at the time the used API version was released. skipServerCertificateValidation An optional boolean which allows to skip the server certificate validation for the external callback server, useful for testing purposes at development time when the callback server is using a self-signed certificate.

  • wait_options (nil) (defaults to: nil)

    The options which rule out how to wait for the completion of the email validation.

Returns:



63
64
65
66
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/verifalia/email_validation/client.rb', line 63

def submit(data,
           quality: nil,
           priority: nil,
           deduplication: nil,
           name: nil,
           retention: nil,
           completion_callback: nil,
           wait_options: nil)
  # Determine how to handle the submission, based on the type of the argument

  if data.nil?
    raise ArgumentError, 'data can\'t be nil.'
  elsif data.is_a?(String)
    data = Request.new [(RequestEntry.new data)],
                       quality: quality
  elsif data.is_a? Enumerable
    entries = data.map do |entry|
      case entry
      when String
        # data is an Array[String]
        RequestEntry.new entry.to_s
      when RequestEntry
        # data is an Array[RequestEntry]
        entry
      when Hash
        # data is an Array[{ :inputData, :custom }]

        raise ArgumentError, 'Input hash must have an :inputData key.' unless entry.key?(:input_data)

        RequestEntry.new entry[:input_data], entry[:custom]
      else
        raise ArgumentError, 'Cannot map input data.'
      end
    end

    data = Request.new entries,
                       quality: quality
  elsif data.is_a?(RequestEntry)
    data = Request.new data,
                       quality: quality
  elsif !data.is_a?(Request)
    raise ArgumentError, "Unsupported data type #{data.class}"
  end

  # Completion callback

  if completion_callback.is_a?(Hash)
    completion_callback = Verifalia::EmailValidations::CompletionCallback.new(completion_callback['url'],
                                                                              completion_callback['version'],
                                                                              completion_callback['skip_server_certificate_validation'])
  end

  # Send the request to the Verifalia API

  wait_options_or_default = wait_options.nil? ? WaitOptions.default : wait_options

  payload = {
    entries: data.entries.map do |entry|
      {
        inputData: entry.input_data,
        custom: entry.custom
      }
    end,
    quality: quality,
    priority: priority,
    deduplication: deduplication,
    name: name,
    retention: retention,
    callback: (
      unless completion_callback.nil?
        {
          url: completion_callback&.url,
          version: completion_callback&.version,
          skipServerCertificateValidation: completion_callback&.skip_server_certificate_validation
        }
      end
    )
  }.compact.to_json

  response = @rest_client.invoke 'post',
                                 "email-validations?waitTime=#{wait_options_or_default.submission_wait_time}",
                                 {
                                   body: payload,
                                   headers:
                                     {
                                       'Content-Type': 'application/json',
                                       'Accept': 'application/json'
                                     }
                                 }

  if response.status == 202 || response.status == 200
    job = Job.from_json(JSON.parse(response.body))

    return job if wait_options_or_default == WaitOptions.no_wait || response.status == 200

    return get(job.overview.id, wait_options: wait_options_or_default)
  end

  raise "Unexpected HTTP response: #{response.status} #{response.body}"
end