<commit_before>require "gyoku"

module Savon
  class QualifiedMessage

    def initialize(types, used_namespaces, key_converter)
      @types = types
      @used_namespaces = used_namespaces
      @key_converter = key_converter
    end

    def to_hash(hash, path)
      return unless hash
      return hash.map { |value| add_namespaces_to_body(value, path) } if hash.kind_of?(Array)
      return hash.to_s unless hash.kind_of? Hash

      hash.inject({}) do |newhash, (key, value)|
        translated_key = Gyoku.xml_tag(key, :key_converter => @key_converter).to_s
        newpath = path + [translated_key]

        if @used_namespaces[newpath]
          newhash.merge(
            "#{@used_namespaces[newpath]}:#{translated_key}" =>
              add_namespaces_to_body(value, @types[newpath] ? [@types[newpath]] : newpath)
          )
        else
          add_namespaces_to_values(value, path) if key == :order!
          newhash.merge(key => value)
        end
      end
    end

    private

    def add_namespaces_to_body(value, path)
      QualifiedMessage.new(@types, @used_namespaces, @key_converter).to_hash(value, path)
    end

    def add_namespaces_to_values(values, path)
      values.collect! { |value|
        camelcased_value = Gyoku.xml_tag(value, :key_converter => @key_converter)
        namespace_path = path + [camelcased_value.to_s]<commit_msg>no need to create a new message<commit_after>require "gyoku"

module Savon
  class QualifiedMessage

    def initialize(types, used_namespaces, key_converter)
      @types = types
      @used_namespaces = used_namespaces
      @key_converter = key_converter
    end

    def to_hash(hash, path)
      return unless hash
      return hash.map { |value| to_hash(value, path) } if hash.kind_of?(Array)
      return hash.to_s unless hash.kind_of? Hash

      hash.inject({}) do |newhash, (key, value)|
        translated_key = Gyoku.xml_tag(key, :key_converter => @key_converter).to_s
        newpath = path + [translated_key]

        if @used_namespaces[newpath]
          newhash.merge(
            "#{@used_namespaces[newpath]}:#{translated_key}" =>
              to_hash(value, @types[newpath] ? [@types[newpath]] : newpath)
          )
        else
          add_namespaces_to_values(value, path) if key == :order!
          newhash.merge(key => value)
        end
      end
    end

    private

    def add_namespaces_to_values(values, path)
      values.collect! { |value|
        camelcased_value = Gyoku.xml_tag(value, :key_converter => @key_converter)
        namespace_path = path + [camelcased_value.to_s]
{'fraction_non_alphanumeric': 0.10251450676982592, 'fraction_numerical': 0.0, 'mean_word_length': 2.8597014925373134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '902109', 'n_tokens_mistral': 850, 'n_tokens_neox': 836, 'n_words': 171}