class MailProvider::Parser

Constants

FAMOUS_CHECKS

Attributes

key[R]
path[R]

Public Class Methods

new() click to toggle source
# File lib/mail_provider/parser.rb, line 20
def initialize
  @data = { free: [], disposable: [], unknown: [] }
end
parse(sources) click to toggle source
# File lib/mail_provider/parser.rb, line 10
def self.parse(sources)
  parser = new
  sources.each do |key, path|
    parser.add key, path
  end
  parser.data
end

Public Instance Methods

add(key, path) click to toggle source
# File lib/mail_provider/parser.rb, line 24
def add(key, path)
  @key = key
  @path = path

  domains = read_domains_from_source
  type = categorize_source domains
  @data[type] << domains
end
data() click to toggle source
# File lib/mail_provider/parser.rb, line 33
def data
  @data.map do |type, items|
    items = items.flatten.group_by(&:itself)
    items = items.map { |k, v| [k, v.count] }
    items = items.sort_by { |r| r[1] }.reverse
    [type, items.to_h]
  end.to_h.slice(:free, :disposable)
end

Protected Instance Methods

categorize_source(domains) click to toggle source
# File lib/mail_provider/parser.rb, line 65
def categorize_source(domains)
  types = []
  FAMOUS_CHECKS.each do |type, checks|
    checks.each do |domain|
      types << type if domains.include?(domain)
    end
  end

  return types[0] if types.uniq.length == 1

  puts "-> Ignoring Source: #{@key}"
  :unknown
end
read_domains_from_source() click to toggle source
# File lib/mail_provider/parser.rb, line 44
def read_domains_from_source
  domains = File.readlines(@path)
  if domains.length == 1
    domains = domains.map do |line|
      line.split(',')
    end.flatten
  end

  domains.map { |d| sanitize_domain(d) }.compact.uniq
end
sanitize_domain(domain) click to toggle source
# File lib/mail_provider/parser.rb, line 55
def sanitize_domain(domain)
  domain = SimpleIDN.to_ascii(domain.strip)
  return if domain.empty? || domain =~ /\A\#/

  domain = domain.gsub(/\A(www|)\./, '')
  PublicSuffix.parse(domain).name
rescue PublicSuffix::DomainNotAllowed
  nil
end