Sha256: 07cae5677c015482f2af36c533b97963c50c38dfb4c6354c88af8abcb8212a8e

Contents?: true

Size: 1.97 KB

Versions: 67

Compression:

Stored size: 1.97 KB

Contents

module Fog
  module Parsers
    class Base < Nokogiri::XML::SAX::Document

      attr_reader :response

      def initialize
        reset
      end

      def reset
        @response = {}
      end

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

      def start_element(name, attrs = [])
        @value = nil
      end

    end
  end
end

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

67 entries across 67 versions & 3 rubygems

Version Path
fog-0.4.0 lib/fog/core/parser.rb
fog-0.3.34 lib/fog/core/parser.rb
fog-0.3.33 lib/fog/core/parser.rb
fog-0.3.32 lib/fog/core/parser.rb
fog-0.3.31 lib/fog/core/parser.rb
fog-0.3.30 lib/fog/core/parser.rb
fog-0.3.29 lib/fog/core/parser.rb
fog-0.3.28 lib/fog/core/parser.rb
fog-0.3.27 lib/fog/core/parser.rb
fog-0.3.26 lib/fog/core/parser.rb
fog-0.3.25 lib/fog/core/parser.rb
fog-0.3.24 lib/fog/core/parser.rb
bbcloud-0.8.1 lib/bbcloud/vendor/fog-0.3.23/lib/fog/core/parser.rb
fog-0.3.23 lib/fog/core/parser.rb
fog-0.3.22 lib/fog/core/parser.rb
fog-0.3.21 lib/fog/core/parser.rb
fog-0.3.20 lib/fog/core/parser.rb
fog-0.3.19 lib/fog/core/parser.rb
fog-0.3.18 lib/fog/core/parser.rb
fog-0.3.17 lib/fog/core/parser.rb