Class: Fog::ToHashDocument

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

Instance Method Summary (collapse)

Constructor Details

- (ToHashDocument) initialize

A new instance of ToHashDocument



54
55
56
# File 'lib/fog/core/parser.rb', line 54

def initialize
  @stack = []
end

Instance Method Details

- (Object) body



75
76
77
# File 'lib/fog/core/parser.rb', line 75

def body
  @stack.first
end

- (Object) characters(string)



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

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

- (Object) end_element(name)



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fog/core/parser.rb', line 63

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

- (Object) response



79
80
81
# File 'lib/fog/core/parser.rb', line 79

def response
  body
end

- (Object) start_element(name, attributes = [])



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fog/core/parser.rb', line 83

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