Class: Nondisposable::DomainListUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/nondisposable/domain_list_updater.rb

Class Method Summary collapse

Class Method Details

.updateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nondisposable/domain_list_updater.rb', line 9

def self.update
  Rails.logger.info "Refreshing list of disposable domains..."

  url = 'https://raw.githubusercontent.com/disposable-email-domains/disposable-email-domains/master/disposable_email_blocklist.conf'

  begin
    uri = URI(url)
    response = Net::HTTP.get_response(uri)

    if response.is_a?(Net::HTTPSuccess)
      downloaded_domains = response.body.split("\n")
      raise "The list is empty. This might indicate a problem with the format." if downloaded_domains.empty?

      Rails.logger.info "Downloaded list of disposable domains..."

      domains = (downloaded_domains + Nondisposable.configuration.additional_domains).uniq
      domains -= Nondisposable.configuration.excluded_domains

      ActiveRecord::Base.transaction do
        Rails.logger.info "Updating disposable domains..."
        Nondisposable::DisposableDomain.delete_all

        domains.each { |domain| Nondisposable::DisposableDomain.create(name: domain.downcase) }
      end

      Rails.logger.info "Finished updating disposable domains. Total domains: #{domains.count}"
      true
    else
      Rails.logger.error "Failed to download the list. HTTP Status: #{response.code}"
      false
    end
  rescue SocketError => e
    Rails.logger.error "Network error occurred: #{e.message}"
    false
  rescue StandardError => e
    Rails.logger.error "An error occurred when trying to update the list of disposable domains: #{e.message}"
    false
  end
end