<commit_before>require 'amazing_print'
require 'timeout'

module CryptCheck
  class Host
    MAX_ANALYSIS_DURATION = 600

    attr_reader :servers, :error

    def initialize(hostname, port)
      @hostname, @port = hostname, port

      first    = true
      @servers = resolve.collect do |args|
        _, ip = args
        first ? (first = false) : Logger.info { '' }
        result = begin
                   server = ::Timeout.timeout MAX_ANALYSIS_DURATION do
                     server(*args)
                   end
                   Logger.info ''
                   Logger.info { "Grade : #{server.grade.to_s.colorize server.grade_status}" }
                   Logger.info { server.states.ai }
                   server
                 rescue => e
                   Logger.error { e }
                   raise if ENV['DEV_MODE']
                   e
                 end
        [[@hostname, ip, @port], result]
      end.to_h
    rescue => e
      Logger.error { e }
      @error = e
      raise if CryptCheck.dev?
    end

    def key
      { hostname: @hostname, port: @port }
    end

    def to_h
      if @error
        target = [self.key.merge(error: @error.to_s)]
      else
        target = @servers.collect do |host, server|
          hostname, ip, port = host
          host               = {
            hostname: hostname,
            ip:       ip,
            port:     port
          }
          case server
          when Server
            host[:handshakes] = server.to_h
            host[:states]     = server.states
            host[:grade]      = server.grade
          else
            host[:error] = server.to_s
          end
          host
        end
      end
      target
    end

    private

    def resolve
      begin
        begin
          ip = IPAddr.new @hostname
          return [[nil, ip.to_s, ip.family, @port]]
        rescue IPAddr::InvalidAddressError
        end
        ::Addrinfo.getaddrinfo(@hostname, nil, nil, :STREAM)
          .collect { |a| [@hostname, a.ip_address, a.afamily, @port] }
      end.reject do |_1, _2, family, *_3|
        (ENV['DISABLE_IPv6'] && family == Socket::AF_INET6) ||
          (ENV['DISABLE_IPv4'] && family == Socket::AF_INET)
      end
    end
  end
end
<commit_msg>Ruby 2.3 compat<commit_after>require 'amazing_print'
require 'timeout'

module CryptCheck
  class Host
    MAX_ANALYSIS_DURATION = 600

    attr_reader :servers, :error

    def initialize(hostname, port)
      @hostname, @port = hostname, port

      first    = true
      @servers = resolve.collect do |args|
        _, ip = args
        first ? (first = false) : Logger.info { '' }
        result = begin
                   server = ::Timeout.timeout MAX_ANALYSIS_DURATION do
                     server(*args)
                   end
                   Logger.info ''
                   Logger.info { "Grade : #{server.grade.to_s.colorize server.grade_status}" }
                   Logger.info { server.states.ai }
                   server
                 rescue => e
                   Logger.error { e }
                   raise if ENV['DEV_MODE']
                   e
                 end
        [[@hostname, ip, @port], result]
      end.to_h
    rescue => e
      Logger.error { e }
      @error = e
      raise if CryptCheck.dev?
    end

    def key
      { hostname: @hostname, port: @port }
    end

    def to_h
      if @error
        target = [self.key.merge(error: @error.to_s)]
      else
        target = @servers.collect do |host, server|
          hostname, ip, port = host
          host               = {
            hostname: hostname,
            ip:       ip,
            port:     port
          }
          case server
          when Server
            host[:handshakes] = server.to_h
            host[:states]     = server.states
            host[:grade]      = server.grade
          else
            host[:error] = server.to_s
          end
          host
        end
      end
      target
    end

    private

    def resolve
      begin
        begin
          ip = IPAddr.new @hostname
          return [[nil, ip.to_s, ip.family, @port]]
        rescue IPAddr::InvalidAddressError
        end
        ::Addrinfo.getaddrinfo(@hostname, nil, nil, :STREAM)
          .collect { |a| [@hostname, a.ip_address, a.afamily, @port] }
      end.reject do |_, _, family, *_|
        (ENV['DISABLE_IPv6'] && family == Socket::AF_INET6) ||
          (ENV['DISABLE_IPv4'] && family == Socket::AF_INET)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09824794854734975, 'fraction_numerical': 0.003770237303171435, 'mean_word_length': 1.3563218390804597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1320868', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1303, 'n_words': 327}