Sha256: f7c638af5d6d244449ca659bbfd3da2f3b02bc42ff4c3a1716842ff124e53bb6

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true
require "email_assessor/email_validator"

module EmailAssessor
  DISPOSABLE_DOMAINS_FILE_NAME = File.expand_path("../../vendor/disposable_domains.txt", __FILE__)
  FASTPASS_DOMAINS_FILE_NAME = File.expand_path("../../vendor/fastpass_domains.txt", __FILE__)
  EDUCATIONAL_DOMAINS_FILE_NAME = File.expand_path("../../vendor/educational_domains.txt", __FILE__)
  BLACKLISTED_DOMAINS_FILE_NAME = File.expand_path("vendor/blacklisted_domains.txt")

  def self.domain_is_disposable?(domain)
    domain_in_file?(domain, DISPOSABLE_DOMAINS_FILE_NAME)
  end

  def self.domain_is_blacklisted?(domain)
    domain_in_file?(domain, BLACKLISTED_DOMAINS_FILE_NAME)
  end

  def self.domain_in_file?(domain, file_name)
    any_token_in_file?(tokenize_domain(domain), file_name)
  end

  def self.any_token_in_file?(domain_tokens, file_name)
    file_name ||= ""

    File.foreach(file_name, chomp: true).any? do |line|
      domain_tokens.key?(line)
    end
  end

  def self.tokenize_domain(domain)
    parts = domain.downcase.split(".")
    tokens = {}

    parts.length.times do
      tokens[parts.join(".")] = nil
      parts.shift
    end

    tokens
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
email_assessor-0.9.1 lib/email_assessor.rb