class AddressFinder::V1::Email::BatchVerification
Constants
- MAX_CONCURRENCY_LEVEL
Attributes
Public Class Methods
Source
# File lib/addressfinder/v1/email/batch_verification.rb, line 15 def initialize(emails:, http:, concurrency: 5, **args) @emails = emails @concurrency = concurrency @http = http @args = args end
Verifies an array of email addresses using concurrency to reduce the execution time. The results of the verification are stored in the ‘results` attribute, in the same order in which they were supplied.
@param [Array<String>] emails @param [AddressFinder::HTTP] http HTTP
connection helper @param [Integer] concurrency How many threads to use for verification @param [Hash] args Any additional arguments that will be passed onto the EV API
Public Instance Methods
Source
# File lib/addressfinder/v1/email/batch_verification.rb, line 22 def perform confirm_concurrency_level verify_each_email_concurrently self end
Private Instance Methods
Source
# File lib/addressfinder/v1/email/batch_verification.rb, line 35 def confirm_concurrency_level return unless @concurrency > MAX_CONCURRENCY_LEVEL warn "WARNING: Concurrency level of #{@concurrency} is higher than the maximum of #{MAX_CONCURRENCY_LEVEL}. Using #{MAX_CONCURRENCY_LEVEL}." @concurrency = MAX_CONCURRENCY_LEVEL end
Source
# File lib/addressfinder/v1/email/batch_verification.rb, line 42 def verify_each_email_concurrently @results = Concurrent::Array.new(emails.length) pool = Concurrent::FixedThreadPool.new(concurrency) @emails.each_with_index do |email, index_of_email| # Start a new thread for each task pool.post do @results[index_of_email] = verify_email(email) end end ## Shutdown the pool and wait for all tasks to complete pool.shutdown pool.wait_for_termination end
Source
# File lib/addressfinder/v1/email/batch_verification.rb, line 60 def verify_email(email) return if email.empty? AddressFinder::V1::Email::Verification.new(email: email, http: http.clone, **args).perform.result rescue AddressFinder::RequestRejectedError => e OpenStruct.new(success: false, body: e.body, status: e.status) end
Verifies a single email addresses, and writes the result into @results