Sha256: 88930b31e85599e27de9e85a3fac5aa4d6a9d8b3b564eaf01a36b9bdcede9460

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

module Fog
  class ToHashDocument < Nokogiri::XML::SAX::Document
    def initialize
      @stack = []
    end

    def characters(string)
      @value ||= ""
      @value << string.strip
    end

    def end_element(name)
      last = @stack.pop
      if last.empty? && @value.empty?
        @stack.last[name.to_sym] = ""
      elsif last == { :i_nil => "true" }
        @stack.last[name.to_sym] = nil
      elsif !@value.empty?
        @stack.last[name.to_sym] = @value
      end
      @value = ""
    end

    def body
      @stack.first
    end

    def response
      body
    end

    def start_element(name, attributes = [])
      @value = ""
      parsed_attributes = {}
      until attributes.empty?
        if attributes.first.is_a?(Array)
          key, value = attributes.shift
        else
          key, value = attributes.shift, attributes.shift
        end
        parsed_attributes[key.gsub(":", "_").to_sym] = value
      end
      if @stack.last.is_a?(Array)
        @stack.last << { name.to_sym => parsed_attributes }
      else
        data = if @stack.empty?
                 @stack.push(parsed_attributes)
                 parsed_attributes
               elsif @stack.last[name.to_sym]
                 unless @stack.last[name.to_sym].is_a?(Array)
                   @stack.last[name.to_sym] = [@stack.last[name.to_sym]]
                 end
                 @stack.last[name.to_sym] << parsed_attributes
                 @stack.last[name.to_sym].last
               else
                 @stack.last[name.to_sym] = {}
                 @stack.last[name.to_sym].merge!(parsed_attributes)
                 @stack.last[name.to_sym]
               end
        @stack.push(data)
      end
    end
  end
end

Version data entries

9 entries across 7 versions & 3 rubygems

Version Path
fog-xml-0.1.4 lib/fog/to_hash_document.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/fog-xml-0.1.3/lib/fog/to_hash_document.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/fog-xml-0.1.3/lib/fog/to_hash_document.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/fog-xml-0.1.3/lib/fog/to_hash_document.rb
fog-xml-0.1.3 lib/fog/to_hash_document.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/fog-xml-0.1.2/lib/fog/to_hash_document.rb
fog-xml-0.1.2 lib/fog/to_hash_document.rb
fog-xml-0.1.1 lib/fog/to_hash_document.rb
fog-xml-0.1.0 lib/fog/to_hash_document.rb