<commit_before>require 'fileutils'

module LetsencryptWebfaction
  class DomainValidator
    def initialize(domains, client, public_dirs)
      @domains = domains
      @client = client
      @public_dirs = public_dirs.map { |dir| File.expand_path(dir) }
    end

    def validate!
      write_files!

      challenges.each(&:request_verification)

      10.times do
        break if no_challenges_pending?
        sleep(1)
      end

      return true if all_challenges_valid?

      print_errors
      false
    end

    private

    def authorizations
      @domains.map { |domain| @client.authorize(domain: domain) }
    end

    def challenges
      @challenges ||= authorizations.map(&:http01)
    end

    def no_challenges_pending?
      challenges.none? { |challenge| challenge.authorization.verify_status == 'pending' }
    end

    def all_challenges_valid?
      challenges.reject { |challenge| challenge.authorization.verify_status == 'valid' }.empty?
    end

    def write_files!
      challenges.each do |challenge|
        @public_dirs.each do |public_dir|
          # Save the file. We'll create a public directory to serve it from, and we'll creating the challenge directory.
          FileUtils.mkdir_p(File.join(public_dir, File.dirname(challenge.filename)))

          # Then writing the file
          File.write(File.join(public_dir, challenge.filename), challenge.file_content)
        end
      end
    end

    def print_errors
      validations = authorizations.map(&:domain).zip(challenges)
      $stderr.puts 'Failed to verify statuses.'
      validations.each { |tuple| Validation.new(*tuple).print_error }
    end

    class Validation
      def initialize(domain, challenge)
        @domain = domain
        @challenge = challenge
      end

      def print_error
        case @challenge.authorization.verify_status
        when 'valid'
          $stderr.puts "#{@domain}: Success"
        when 'invalid'
          $stderr.puts "#{@domain}: #{@challenge.error['detail']}"
          $stderr.puts "Make sure that you can access #{url}"
        when 'pending'
          $stderr.puts "#{@domain}: Still pending, but timed out"
        else
          $stderr.puts "#{@domain}: Unexpected authorization status #{@challenge.authorization.verify_status}"
        end
      end

      def url
        "http://#{@domain}/#{@challenge.filename}"
      end
    end
  end
end<commit_msg>For now, ignore rubocop lint<commit_after>require 'fileutils'

module LetsencryptWebfaction
  class DomainValidator
    def initialize(domains, client, public_dirs)
      @domains = domains
      @client = client
      @public_dirs = public_dirs.map { |dir| File.expand_path(dir) }
    end

    def validate!
      write_files!

      challenges.each(&:request_verification)

      10.times do
        break if no_challenges_pending?
        sleep(1)
      end

      return true if all_challenges_valid?

      print_errors
      false
    end

    private

    def authorizations
      @domains.map { |domain| @client.authorize(domain: domain) }
    end

    def challenges
      @challenges ||= authorizations.map(&:http01)
    end

    def no_challenges_pending?
      challenges.none? { |challenge| challenge.authorization.verify_status == 'pending' }
    end

    def all_challenges_valid?
      challenges.reject { |challenge| challenge.authorization.verify_status == 'valid' }.empty?
    end

    def write_files!
      challenges.each do |challenge|
        @public_dirs.each do |public_dir|
          # Save the file. We'll create a public directory to serve it from, and we'll creating the challenge directory.
          FileUtils.mkdir_p(File.join(public_dir, File.dirname(challenge.filename)))

          # Then writing the file
          File.write(File.join(public_dir, challenge.filename), challenge.file_content)
        end
      end
    end

    def print_errors
      validations = authorizations.map(&:domain).zip(challenges)
      $stderr.puts 'Failed to verify statuses.'
      validations.each { |tuple| Validation.new(*tuple).print_error }
    end

    class Validation
      def initialize(domain, challenge)
        @domain = domain
        @challenge = challenge
      end

      def print_error # rubocop:disable Metrics/MethodLength
        case @challenge.authorization.verify_status
        when 'valid'
          $stderr.puts "#{@domain}: Success"
        when 'invalid'
          $stderr.puts "#{@domain}: #{@challenge.error['detail']}"
          $stderr.puts "Make sure that you can access #{url}"
        when 'pending'
          $stderr.puts "#{@domain}: Still pending, but timed out"
        else
          $stderr.puts "#{@domain}: Unexpected authorization status #{@challenge.authorization.verify_status}"
        end
      end

      def url
        "http://#{@domain}/#{@challenge.filename}"
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0961380443714051, 'fraction_numerical': 0.002054231717337716, 'mean_word_length': 2.556610664718773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4731343', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1369, 'n_words': 351}