<commit_before>class MWS::API::Feeds::Envelope
  def initialize(params={})
    @envelope = build_envelope(params)
    validate! unless params[:skip_schema_validation] == true
  end

  def valid?
    self.errors.count == 0
  end

  def validate!
    unless valid?
      puts self
      raise "Invalid XML:\n" + self.errors.join("\n")
    end
  end

  def md5
    Digest::MD5.base64digest(self)
  end

  def to_str
    @envelope.target!
  end

  def to_s
    @envelope.target!
  end

  def xsd
    Nokogiri::XML::Schema(File.open(File.join(File.dirname(__FILE__),"xsd/amzn-envelope.xsd")))
  end

  def errors
    @errors ||= xsd.validate(Nokogiri::XML(self))
  end

  private
  def build_envelope(params={})
    xml = Builder::XmlMarkup.new(indent: 2)
    xml.instruct!

    xml.AmazonEnvelope do
      xml.Header do
        xml.DocumentVersion "1.01"
        xml.MerchantIdentifier params[:merchant_id]
      end

      xml.MessageType params[:message_type].to_s.camelize
      xml.PurgeAndReplace params[:purge_and_replace] || false

      xml << params[:message].to_xml(skip_instruct: true, root: "Message") 
    end<commit_msg>Remove unnecessary xml nodes created during Hash#to_xml<commit_after>class MWS::API::Feeds::Envelope
  def initialize(params={})
    @envelope = build_envelope(params)
    validate! unless params[:skip_schema_validation] == true
  end

  def valid?
    self.errors.count == 0
  end

  def validate!
    unless valid?
      puts self
      raise "Invalid XML:\n" + self.errors.join("\n")
    end
  end

  def md5
    Digest::MD5.base64digest(self)
  end

  def to_str
    to_s
  end

  def to_s
    result = @envelope.target!.gsub('<Items type="array">', "")
    result.gsub!('</Items>', "")
  end

  def xsd
    Nokogiri::XML::Schema(File.open(File.join(File.dirname(__FILE__),"xsd/amzn-envelope.xsd")))
  end

  def errors
    @errors ||= xsd.validate(Nokogiri::XML(self))
  end

  private
  def build_envelope(params={})
    xml = Builder::XmlMarkup.new
    xml.instruct!

    xml.AmazonEnvelope do
      xml.Header do
        xml.DocumentVersion "1.01"
        xml.MerchantIdentifier params[:merchant_id]
      end

      xml.MessageType params[:message_type].to_s.camelize
      xml.PurgeAndReplace params[:purge_and_replace] || false

      xml << params[:message].to_xml(skip_instruct: true, root: "Message") 
    end
{'fraction_non_alphanumeric': 0.12367190820229494, 'fraction_numerical': 0.007224819379515512, 'mean_word_length': 3.173758865248227, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, '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': '1860935', 'n_tokens_mistral': 841, 'n_tokens_neox': 801, 'n_words': 159}