Sha256: b9ffc1de04d72572f55f8f786165b1dbe133930c00205781f14fa950e605b7c5

Contents?: true

Size: 987 Bytes

Versions: 4

Compression:

Stored size: 987 Bytes

Contents

require 'nokogiri'

module Quickbooks::Parser::XMLParsing

  XML_DOCUMENT = Nokogiri::XML::Document
  XML_NODE_SET = Nokogiri::XML::NodeSet
  XML_NODE = Nokogiri::XML::Node
  XML_ELEMENT = Nokogiri::XML::Element
  XML_COMMENT= Nokogiri::XML::Comment
  XML_TEXT = Nokogiri::XML::Text

  COMMENT_START = "<!--"
  COMMENT_END = "-->"
  COMMENT_MATCHER = /\A#{COMMENT_START}.*#{COMMENT_END}\z/

  # remove all comment lines and empty nodes
  def cleanup_qbxml(qbxml)
    qbxml = qbxml.split('\n')
    qbxml.map! { |l| l.strip }
    qbxml.reject! { |l| l =~ COMMENT_MATCHER }
    qbxml.join('')
  end

  def leaf_node?(xml_obj)
    xml_obj.children.size == 1 && xml_obj.children.first.class == XML_TEXT
  end

  def parse_leaf_node_data(xml_obj)
    attr_name = underscore(xml_obj)
    text_node = xml_obj.children.first
    [attr_name, text_node.text]
  end

  def parse_xml_attributes(xml_obj)
    attrs = xml_obj.attributes
    attrs.inject({}) { |h, (n,v)| h[n] = v.value; h }
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
quickbooks_api-0.1.3 lib/quickbooks/parser/xml_parsing.rb
quickbooks_api-0.1.2 lib/quickbooks/parser/xml_parsing.rb
quickbooks_api-0.1.1 lib/quickbooks/parser/xml_parsing.rb
quickbooks_api-0.1.0 lib/quickbooks/parser/xml_parsing.rb