class RichEmailValidator::ListValidator

Validates list of emails and return the valid ones

Attributes

list[R]
result[R]
threads_count[R]
total_slices[R]

Public Class Methods

filter(list, options = {}) click to toggle source

Validates list of emails @param list [Enumerator] @param [Hash] options @option options [#to_int] :threads_count number of threads that will

be fired simultaneously to calculate the result. Default 20, max 100

@return [Array]

# File lib/rich_email_validator/list_validator.rb, line 14
def filter(list, options = {})
  new(list, options).filter
end
new(list, options = {}) click to toggle source

Validates list of emails @param list [Enumerator] @param [Hash] options @option options [#to_int] :threads_count number of threads that will

be fired simultaneously to calculate the result. Default 20, max 100
# File lib/rich_email_validator/list_validator.rb, line 26
def initialize(list, options = {})
  @list = list
  @threads_count = options.fetch(:threads_count) { 20 }
  fail ArgumentError, "Threads can't exceed 100" if @threads_count > 100
  @result = []
  @total_slices = (@list.size / @threads_count.to_f).ceil
end

Public Instance Methods

filter() click to toggle source

Validates list of emails @return [Array]

# File lib/rich_email_validator/list_validator.rb, line 36
def filter
  @filtered ||= run_filter
end

Private Instance Methods

filter_slice(slice_index) click to toggle source
# File lib/rich_email_validator/list_validator.rb, line 52
def filter_slice(slice_index)
  start_index = (threads_count * slice_index)
  end_index = [start_index + threads_count, list.size].min
  (start_index...end_index).each do |list_index|
    email = list[list_index]
    result[list_index] = email if EmailValidator.valid?(email)
  end
  sleep_timer(start_index)
end
run_filter() click to toggle source
# File lib/rich_email_validator/list_validator.rb, line 44
def run_filter
  threads = (0...total_slices).map do |slice_index|
    Thread.new { filter_slice(slice_index) }
  end
  threads.each { |thread| thread.join }
  result.select { |email| email }
end
sleep_timer(index) click to toggle source
# File lib/rich_email_validator/list_validator.rb, line 62
def sleep_timer(index)
  sleep(1) if (index % ((100 / threads_count) * threads_count)) == 0
end