Class: Fog::Parsers::AWS::ElasticBeanstalk::BaseParser

Inherits:
Base
  • Object
show all
Defined in:
lib/fog/aws/parsers/beanstalk/parser.rb

Instance Attribute Summary

Attributes inherited from Base

#response

Instance Method Summary collapse

Methods inherited from Base

#attr_value, #characters, #end_element_namespace, #start_element_namespace, #value

Constructor Details

#initialize(result_name) ⇒ BaseParser

Returns a new instance of BaseParser.



6
7
8
9
10
11
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 6

def initialize(result_name)
  @result_name = result_name # Set before super, since super calls reset
  super()
  @tags = {}
  @list_tags = {}
end

Instance Method Details

#end_element(name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 46

def end_element(name)
  case name
    when 'member'
      if @parse_stack.last[:type] != :object
        @parse_stack.last[:value] << value
      end
    when 'RequestId'
      @response['ResponseMetadata'][name] = value
    else
      if @list_tags.key?(name) || @tags[name] == :object
        @parse_stack.pop()
      elsif @tags.key?(name)
        set_value(name, value, @tags[name])
      end
  end
end

#get_parentObject



63
64
65
66
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 63

def get_parent
  parent = @parse_stack.last[:value]
  parent.is_a?(Array) ? parent.last : parent
end

#resetObject



13
14
15
16
17
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 13

def reset
  @response = { @result_name => {}, 'ResponseMetadata' => {} }
  # Push root object to top of stack
  @parse_stack = [ { :type => :object, :value => @response[@result_name]} ]
end

#set_value(name, value, type) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 68

def set_value(name, value, type)
  case type
    when :datetime
      get_parent[name] = Time.parse value
    when :boolean
      get_parent[name] = value == "true" # True only if value is true
    when :integer
      get_parent[name] = value.to_i
    else
      get_parent[name] = value
  end
end

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 31

def start_element(name, attrs = [])
  super
  if name == 'member'
    if @parse_stack.last[:type] == :object
      @parse_stack.last[:value] << {} # Push any empty object
    end
  elsif @list_tags.key?(name)
    set_value(name, [], :array) # Set an empty array
    @parse_stack.push({ :type => @tags[name], :value => get_parent[name] })
  elsif @tags[name] == :object
    set_value(name, {}, :object)
    @parse_stack.push({ :type => @tags[name], :value => get_parent[name] })
  end
end

#tag(name, *traits) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 19

def tag name, *traits
  if traits.delete(:list)
    @list_tags[name] = true
  end

  if traits.length == 1
    @tags[name] = traits.last
  else
    raise "Too many traits specified, only specify :list or a type"
  end
end