1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/parser.rb

94 lines
2 KiB
Ruby
Raw Normal View History

2009-05-19 02:06:49 -04:00
module Fog
module Parsers
class Base < Nokogiri::XML::SAX::Document
attr_reader :response
2009-05-19 02:06:49 -04:00
def initialize
reset
end
def reset
@response = {}
2009-05-19 02:06:49 -04:00
end
def characters(string)
@value ||= ''
2009-05-19 02:06:49 -04:00
@value << string.strip
end
def start_element(name, attrs = [])
@value = nil
2009-05-19 02:06:49 -04:00
end
end
end
end
2010-06-18 17:35:29 -04:00
module Fog
class ToHashDocument < Nokogiri::XML::SAX::Document
2010-06-18 17:35:29 -04:00
def initialize
@stack = []
end
def characters(string)
@value ||= ''
@value << string.strip
end
def end_element(name)
2010-06-18 17:53:35 -04:00
last = @stack.pop
if last.empty? && @value.empty?
2010-06-18 17:53:35 -04:00
@stack.last[name.to_sym] = ''
elsif last == {:i_nil=>"true"}
@stack.last[name.to_sym] = nil
elsif !@value.empty?
2010-06-18 17:35:29 -04:00
@stack.last[name.to_sym] = @value
end
2010-06-18 17:53:35 -04:00
@value = ''
2010-06-18 17:35:29 -04:00
end
def body
@stack.first
end
def response
body
end
2010-06-18 17:35:29 -04:00
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
2010-06-18 17:35:29 -04:00
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