Sha256: 0f24d01d9cbe5489f251cc79fb16ec76a90467cb534efbe159910ed8ff724ed8

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

module Recurly
  class XML
    module NokogiriAdapter
      def initialize xml
        @root = Nokogiri(xml).root
      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

14 entries across 14 versions & 1 rubygems

Version Path
recurly-2.17.1 lib/recurly/xml/nokogiri.rb
recurly-2.17.0 lib/recurly/xml/nokogiri.rb
recurly-2.16.2 lib/recurly/xml/nokogiri.rb
recurly-2.16.1 lib/recurly/xml/nokogiri.rb
recurly-2.16.0 lib/recurly/xml/nokogiri.rb
recurly-2.15.4 lib/recurly/xml/nokogiri.rb
recurly-2.15.3 lib/recurly/xml/nokogiri.rb
recurly-2.14.1 lib/recurly/xml/nokogiri.rb
recurly-2.13.1 lib/recurly/xml/nokogiri.rb
recurly-2.15.2 lib/recurly/xml/nokogiri.rb
recurly-2.15.1 lib/recurly/xml/nokogiri.rb
recurly-2.15.0 lib/recurly/xml/nokogiri.rb
recurly-2.14.0 lib/recurly/xml/nokogiri.rb
recurly-2.13.0 lib/recurly/xml/nokogiri.rb