Sha256: e40a60c46c85c5059e0d269ff470bd9b94accf4e8c4cd75719152122452c8400

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module CiviCrm
  class XML
    class << self
      def parse(text)
        fixed_text = text.to_s.
                     gsub("\n", "").
                     gsub(/<\d+><\/\d+>/, "")

        doc = Nokogiri::XML.parse(fixed_text)

        results = doc.xpath('//Result')
        results.map do |result|
          hash = {}
          result.children.each do |attribute|
            next unless attribute.is_a?(Nokogiri::XML::Element)
            hash[attribute.name] = attribute.children[0].text rescue nil
          end
          hash
        end
      end
      def encode(resources)
        builder = Nokogiri::XML::Builder.new do |xml|
          xml.ResultSet do
            Array.wrap(resources).each do |resource|
              attributes = resource.respond_to?(:attributes) ? resource.attributes : resource
              xml.Result do
                attributes.each do |key, value|
                  xml.send key.to_sym, value
                end
              end
            end
          end
        end
        builder.to_xml
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
civicrm-1.0.4 lib/civicrm/xml.rb