<commit_before># frozen_string_literal: true

module FirstACH
  # HTTP Client Interface
  class Client
    SUCCESS_CODE = '0000'

    def self.execute(method, payload)
      url = "https://#{FirstACH.configuration.environment}.firstach.com/API/processAPIRequest.aspx"

      raw_response = RestClient::Request.execute(
        method:  method,
        url:     url,
        headers: default_headers,
        payload: payload
      )

      return nil if raw_response.body.empty?

      response = parse_object(Nokogiri::XML(raw_response.body).root)

      raise FirstACH::Error, response.messages if response.messages.code != SUCCESS_CODE

      response[response.to_h.tap { |hash| hash.delete(:messages) }.keys.last]
    end

    # @!visibility private
    def self.parse_object(node)
      case node
      when Nokogiri::XML::Document, Nokogiri::XML::Element
        if node.children.count == 1 && node.child.class == Nokogiri::XML::Text
          parse_object(node.child)<commit_msg>Add raw data to error.<commit_after># frozen_string_literal: true

module FirstACH
  # HTTP Client Interface
  class Client
    SUCCESS_CODE = '0000'

    def self.execute(method, payload)
      url = "https://#{FirstACH.configuration.environment}.firstach.com/API/processAPIRequest.aspx"

      raw_response = RestClient::Request.execute(
        method:  method,
        url:     url,
        headers: default_headers,
        payload: payload
      )

      return nil if raw_response.body.empty?

      response = parse_object(Nokogiri::XML(raw_response.body).root)

      raise FirstACH::Error, "#{response.messages} RAW RESPONSE: #{raw_response.body}" if response.messages.code != SUCCESS_CODE

      response[response.to_h.tap { |hash| hash.delete(:messages) }.keys.last]
    end

    # @!visibility private
    def self.parse_object(node)
      case node
      when Nokogiri::XML::Document, Nokogiri::XML::Element
        if node.children.count == 1 && node.child.class == Nokogiri::XML::Text
          parse_object(node.child)
{'fraction_non_alphanumeric': 0.11232063334982682, 'fraction_numerical': 0.004948045522018802, 'mean_word_length': 3.126530612244898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1515378', 'n_tokens_mistral': 628, 'n_tokens_neox': 595, 'n_words': 124}