<commit_before>require 'pp'

require 'faraday'
require 'faraday_middleware'
require 'multi_xml'

require_relative 'cwid/configuration'
require_relative 'cwid/person'

module CWID
  class << self
    attr_accessor :configuration

    def configure
      # Create a new configuration and yield it to a block
      self.configuration ||= Configuration.new

      yield(configuration)

      return configuration
    end

    # Perform a lookup.
    #
    # @param [Hash] The search terms in a hash (`cwid: 20045405`, `name: "<NAME>"`).
    # @return [Person] The person that belongs to that CWID.
    def lookup(terms = {})
      # Format the CWID if that's what we're getting.
      if terms[:cwid]
        terms = format(terms.values.first)
      else
        terms = terms.values.first
      end

      # Make the request to the LDAP server
      req = self.connection.get do |r|
        r.url self.configuration.search_path + terms
      end

      # Return nil if there aren't any results
      if req.body['directory'] && req.body['directory'].empty?
        nil

      # Otherwise, create a new Person out of it
      else
        results = []

        if req.body['directory']['person'].is_a?(Hash)
          results << Person.new(req.body['directory']['person'])
        else
          req.body['directory']['person'].each do |r|
            results << Person.new(r)
          end
        end

        results
      end
    end

    # Formats a CWID to remove special characters and extra numbers.
    #
    # @param [Integer, String] The CWID to be formatted
    # @return [String] The formatted CWID
    def format(cwid)
      # Convert the input to a string
      cwid = cwid.to_s

      # Remove anything other than the numbers
      cwid = cwid.gsub(/[^0-9 ]/, '')

      # Remove spaces
      cwid = cwid.gsub(/\s+/, '')

      # If it's more than 8 characters, it has the card code as well
      if cwid.length > 8
        cwid = cwid[0..7]
      end

      cwid
    end

    def connection
      Faraday.new(url: self.configuration.base_url) do |f|
        f.response :xml, :content_type => /\bxml$/
        f.adapter :net_http
        f.basic_auth self.configuration.username, self.configuration.password
      end
    end
  end
end<commit_msg>add connection checking.<commit_after>require 'pp'

require 'faraday'
require 'faraday_middleware'
require 'multi_xml'

require_relative 'cwid/configuration'
require_relative 'cwid/person'

module CWID
  class << self
    attr_accessor :configuration

    def configure
      # Create a new configuration and yield it to a block
      self.configuration ||= Configuration.new

      yield(configuration)

      return configuration
    end

    # Perform a lookup.
    #
    # @param [Hash] The search terms in a hash (`cwid: 20045405`, `name: "<NAME>"`).
    # @return [Person] The person that belongs to that CWID.
    def lookup(terms = {})
      # Format the CWID if that's what we're getting.
      if terms[:cwid]
        terms = format(terms.values.first)
      else
        terms = terms.values.first
      end

      # Make the request to the LDAP server
      req = self.connection.get do |r|
        r.url self.configuration.search_path + terms
      end

      # Return nil if there aren't any results
      if req.body['directory'] && req.body['directory'].empty?
        nil

      # Otherwise, create a new Person out of it
      else
        results = []

        if req.body['directory']['person'].is_a?(Hash)
          results << Person.new(req.body['directory']['person'])
        else
          req.body['directory']['person'].each do |r|
            results << Person.new(r)
          end
        end

        results
      end
    end

    # Formats a CWID to remove special characters and extra numbers.
    #
    # @param [Integer, String] The CWID to be formatted
    # @return [String] The formatted CWID
    def format(cwid)
      # Convert the input to a string
      cwid = cwid.to_s

      # Remove anything other than the numbers
      cwid = cwid.gsub(/[^0-9 ]/, '')

      # Remove spaces
      cwid = cwid.gsub(/\s+/, '')

      # If it's more than 8 characters, it has the card code as well
      if cwid.length > 8
        cwid = cwid[0..7]
      end

      cwid
    end

    def connection
      Faraday.new(url: self.configuration.base_url) do |f|
        f.response :xml, :content_type => /\bxml$/
        f.adapter :net_http
        f.basic_auth self.configuration.username, self.configuration.password
      end
    end

    def connected?
      result = true

      begin
        req = self.connection.get do |r|
          r.url self.configuration.search_path
          r.options.timeout = 2
          r.options.open_timeout = 1
        end
      rescue Faraday::ConnectionFailed
        result = false
      end

      result
    end
  end
end
{'fraction_non_alphanumeric': 0.09824561403508772, 'fraction_numerical': 0.006191950464396285, 'mean_word_length': 2.1184041184041185, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '3565792', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1392, 'n_words': 452}