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
freeformz 30446e5ac1 Servicify & Hashify & more requests
Many changes:

-Become more of a proper Fog Service.
-Drop most of the Collection/Model customizations.
-Add a hook for services to do something after #new with the instance
-Move to the ToHashDocument parser ... so no more having to actually
make parsers.
-Fog::Vcloud::Extension is kind of like Fog::Service, but for writing
extension modules to Fog::Vcloud.
-Fix up existing specs/mocks (they're not complete atm, but the existing
ones are up to date).
-Fog::Vcloud::Terremark::Ecloud gets almost all extensions implemented
(almost).
-Fog::Vcloud::Terremark::Ecloud bumped to working with the current TMRK
API release.
-Factor out some TMRK/ecloud specifc mock data into the ecloud module.
-Probably forgetting something.
2010-06-28 02:10:27 +08:00

93 lines
2 KiB
Ruby

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