<commit_before>require 'nokogiri'
require 'httparty'

module EuroDNS
  class API
    include HTTParty

    def initialize username, password, environment = :production
      if environment.to_sym == :test
        @api_url = 'https://secure.tryout-eurodns.com:20015/v2/'
      elsif environment.to_sym == :production
        @api_url = 'https://secure.api-eurodns.com:20015/v2/'
      else
        raise Error, 'Invalid environment provided.'
      end

      @username = username
      @password = password
    end

    def call method, data = nil
      xml = generate_request_xml(method, data)

      api_response = request_from_api(xml)

      process_response(api_response)
    end

    def generate_request_xml method, data
      namespace = method.split(':').first

      document = Nokogiri::XML::Builder.new do |xml|
        xml.request("xmlns:#{namespace}" => "http://www.eurodns.com/#{namespace}") {
          xml.send(method) {
            unless data.nil?
              data.each do |variable, value|
                xml.send("#{namespace}:#{variable}", value)
              end
            end
          }
        }
      end

      document.to_xml
    end

    def request_from_api xml
      result = self.class.post @api_url, :xml => xml
    end

    def process_response response
      xml = Nokogiri::XML.parse(response)

      result = xml.root.xpath('/response/result').first
      result_code = result[:code].to_i

      if !result_code_means_success(result_code)
        raise InvalidApiResponse, "#{result_code} - #{result.xpath('//msg').first.text}"
      end

      xml.xpath('//resData')
    end

    protected
      def result_code_means_success result_code
        [1000, 1001].include?(result_code)
      end
  end
end<commit_msg>Fixing function calls to add username and password.<commit_after>require 'nokogiri'
require 'httparty'

module EuroDNS
  class API
    include HTTParty

    def initialize username, password, environment = :production
      if environment.to_sym == :test
        @api_url = 'https://secure.tryout-eurodns.com:20015/v2/'
      elsif environment.to_sym == :production
        @api_url = 'https://secure.api-eurodns.com:20015/v2/'
      else
        raise Error, 'Invalid environment provided.'
      end

      @username = username
      @password = password
    end

    def call method, data = nil
      xml = generate_request_xml(method, data)

      api_response = request_from_api(xml)

      process_response(api_response)
    end

    def generate_request_xml method, data
      namespace = method.split(':').first

      document = Nokogiri::XML::Builder.new do |xml|
        xml.request("xmlns:#{namespace}" => "http://www.eurodns.com/#{namespace}") {
          xml.send(method) {
            unless data.nil?
              data.each do |variable, value|
                xml.send("#{namespace}:#{variable}", value)
              end
            end
          }
        }
      end

      document.to_xml
    end

    def request_from_api xml
      self.class.post(@api_url, {
        :body => {:xml => xml},
        :basic_auth => {
          :username => @username,
          :password => "<PASSWORD>#{<PASSWORD>(<PASSWORD>)}"  
        }
      })
    end

    def process_response response
      xml = Nokogiri::XML.parse(response)

      result = xml.root.xpath('//xmlns:response/xmlns:result', {'xmlns' => 'http://www.eurodns.com/'}).first
      result_code = result[:code].to_i

      if !result_code_means_success(result_code)
        raise InvalidApiResponse, "#{result_code} - #{result.xpath('//xmlns:msg', {'xmlns' => 'http://www.eurodns.com/'}).first.text}"
      end

      xml.xpath('//xmlns:resData', {'xmlns' => 'http://www.eurodns.com/'})
    end

    protected
      def result_code_means_success result_code
        [1000, 1001].include?(result_code)
      end
  end
end
{'fraction_non_alphanumeric': 0.12360425863412101, 'fraction_numerical': 0.01038691249026227, 'mean_word_length': 2.3466550825369246, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 5, 'xml': 39}, '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': '1024321', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1176, 'n_words': 240}