Sha256: f34cff959c6167e7f0636b7c353802409d4b62c14067709bfc8713b09eea2e03

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 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 !@value.empty?
        @stack.last[name.to_sym] = @value
      end
      @value = ''
    end

    def body
      @stack.first
    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

1 entries across 1 versions & 1 rubygems

Version Path
fog-0.2.0 lib/fog/parser.rb