Class: Fog::ToHashDocument

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/fog/core/parser.rb

Instance Method Summary collapse

Constructor Details

#initializeToHashDocument

Returns a new instance of ToHashDocument.



35
36
37
# File 'lib/fog/core/parser.rb', line 35

def initialize
  @stack = []
end

Instance Method Details

#bodyObject



56
57
58
# File 'lib/fog/core/parser.rb', line 56

def body
  @stack.first
end

#characters(string) ⇒ Object



39
40
41
42
# File 'lib/fog/core/parser.rb', line 39

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

#end_element(name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/core/parser.rb', line 44

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

#responseObject



60
61
62
# File 'lib/fog/core/parser.rb', line 60

def response
  body
end

#start_element(name, attributes = []) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fog/core/parser.rb', line 64

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