Sha256: ce3d6bb34b8b7be8b3435caf2383601ae6f5fb3376d9545caef6e07f812ad8b9

Contents?: true

Size: 1.27 KB

Versions: 34

Compression:

Stored size: 1.27 KB

Contents

module Recurly
  class XML
    module NokogiriAdapter
      def initialize xml
        @root = Nokogiri::XML(xml) { |config| config.strict }.root
      rescue Nokogiri::XML::SyntaxError
        raise ParseError
      end

      def add_element name, value = nil
        root.add_child(node = ::Nokogiri::XML::Element.new(name, root))
        node << value if value
        node
      end

      def each_element xpath = nil
        elements = xpath.nil? ? root.children : root.xpath(xpath)
        elements.each { |el| yield el }
      end

      def each element = root
        element.elements.each do |el|
          yield el
          each el, &Proc.new
        end
      end

      def name
        root.name
      end

      def [] xpath
        root.at_xpath xpath
      end

      def text xpath = nil
        if node = (xpath ? root.at_xpath(xpath) : root)
          if node.text?
            node.text
          else
            node.children.map { |e| e.text if e.text? }.compact.join
          end
        end
      end

      def text= text
        root.content = text
      end

      def to_s
        root.to_xml(
          indent: 0,
          save_with: Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS
        ).gsub(/$\n/, '')
      end
    end

    include NokogiriAdapter
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
recurly-2.18.23 lib/recurly/xml/nokogiri.rb
recurly-2.18.22 lib/recurly/xml/nokogiri.rb
recurly-2.18.21 lib/recurly/xml/nokogiri.rb
recurly-2.18.20 lib/recurly/xml/nokogiri.rb
recurly-2.18.19 lib/recurly/xml/nokogiri.rb
recurly-2.18.18 lib/recurly/xml/nokogiri.rb
recurly-2.18.17 lib/recurly/xml/nokogiri.rb
recurly-2.18.16 lib/recurly/xml/nokogiri.rb
recurly-2.18.15 lib/recurly/xml/nokogiri.rb
recurly-2.18.14 lib/recurly/xml/nokogiri.rb
recurly-2.18.13 lib/recurly/xml/nokogiri.rb
recurly-2.18.12 lib/recurly/xml/nokogiri.rb
recurly-2.18.11 lib/recurly/xml/nokogiri.rb
recurly-2.18.10 lib/recurly/xml/nokogiri.rb
recurly-2.18.9 lib/recurly/xml/nokogiri.rb
recurly-2.18.8 lib/recurly/xml/nokogiri.rb
recurly-2.18.7 lib/recurly/xml/nokogiri.rb
recurly-2.18.6 lib/recurly/xml/nokogiri.rb
recurly-2.18.5 lib/recurly/xml/nokogiri.rb
recurly-2.18.4 lib/recurly/xml/nokogiri.rb