Sha256: f558b3ba2b9729e104897c10b64019dd1699fe93e551c08bedb70fdeedc76c8f

Contents?: true

Size: 1.9 KB

Versions: 12

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require 'nokogiri'

module Boxcars
  class XNode
    attr_accessor :node, :children, :attributes

    def initialize(node)
      @node = node
      @valid_names = []
      @children = {}
      @attributes = node.attributes.values.to_h { |a| [a.name.to_sym, a.value] }

      node.children.each do |child|
        next if child.text?

        child_node = XNode.new(child)
        if @children[child.name].nil?
          @valid_names << child.name.to_sym
          @children[child.name] = child_node
        elsif @children[child.name].is_a?(Array)
          @children[child.name] << child_node
        else
          @children[child.name] = [@children[child.name], child_node]
        end
      end
    end

    def self.from_xml(xml)
      xml = xml[xml.index("<")..-1] unless xml.start_with?("<")
      xml = xml[0..xml.rindex(">")] unless xml.end_with?(">")
      doc = Nokogiri::XML.parse(xml)
      if doc.errors.any?
        Boxcars.debug("XML: #{xml}", :yellow)
        # rubocop:disable Lint/Debugger
        debugger if ENV.fetch("DEBUG_XML", false)
        # rubocop:enable Lint/Debugger
        raise XmlError, "XML is not valid: #{doc.errors.map { |e| "#{e.line}:#{e.column} #{e.message}" }}"
      end
      XNode.new(doc.root)
    end

    def xml
      @node.to_xml
    end

    def text
      @node.text
    end

    def xpath(path)
      @node.xpath(path)
    end

    def xtext(path)
      rv = xpath(path)&.text&.gsub(/[[:space:]]+/, " ")&.strip
      return nil if rv.empty?

      rv
    end

    def stext
      @stext ||= text.gsub(/[[:space:]]+/, " ").strip # remove extra spaces
    end

    def [](key)
      @children[key.to_s]
    end

    def method_missing(name, *args)
      return @children[name.to_s] if @children.key?(name.to_s)

      super
    end

    def respond_to_missing?(method_name, include_private = false)
      @valid_names.include?(method) || super
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
boxcars-0.6.6 lib/boxcars/x_node.rb
boxcars-0.6.5 lib/boxcars/x_node.rb
boxcars-0.6.4 lib/boxcars/x_node.rb
boxcars-0.6.3 lib/boxcars/x_node.rb
boxcars-0.6.2 lib/boxcars/x_node.rb
boxcars-0.6.1 lib/boxcars/x_node.rb
boxcars-0.5.1 lib/boxcars/x_node.rb
boxcars-0.4.10 lib/boxcars/x_node.rb
boxcars-0.4.9 lib/boxcars/x_node.rb
boxcars-0.4.8 lib/boxcars/x_node.rb
boxcars-0.4.7 lib/boxcars/x_node.rb
boxcars-0.4.6 lib/boxcars/x_node.rb