Sha256: 6f32997b3fef62d741fa405cbabdd96fa1833d142aef695780ccda60cc64c538

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

module Sucker
  module HashBuilder
    class << self

      # Builds a hash from a Nokogiri XML document.
      #
      # In earlier versions of Sucker, I was relying on the XML Mini
      # Nokogiri module in Active Support. This method essentially
      # accomplishes the same.
      #
      # Based on https://gist.github.com/335286
      def from_xml(xml)
        case xml
        when Nokogiri::XML::Document
          from_xml(xml.root)
        when Nokogiri::XML::Element
          result_hash = {}

          xml.attributes.each_pair do |key, attribute|
            result_hash[key] = attribute.value
          end

          xml.children.each do |child|
            result = from_xml(child)

            if child.name == 'text'
              if result_hash.empty?
                return result
              else
                result_hash['__content__'] = result
              end
            elsif result_hash[child.name]
              if result_hash[child.name].is_a? Array
                result_hash[child.name] << result
              else
                result_hash[child.name] = [result_hash[child.name]] << result
              end
            else
              result_hash[child.name] = result
            end
          end

          result_hash
        else
          xml.content.to_s
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sucker-2.1.0 lib/sucker/hash_builder.rb
sucker-2.0.0 lib/sucker/hash_builder.rb
sucker-2.0.0.pre.5 lib/sucker/hash_builder.rb
sucker-2.0.0.pre.4 lib/sucker/hash_builder.rb
sucker-2.0.0.pre.3 lib/sucker/hash_builder.rb
sucker-2.0.0.pre.2 lib/sucker/hash_builder.rb
sucker-2.0.0.pre.1 lib/sucker/hash_builder.rb