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, #value

Constructor Details

#initialize(result_name) ⇒ BaseParser

Returns a new instance of BaseParser.



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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 50

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.has_key?(name) || @tags[name] == :object
        @parse_stack.pop()
      elsif @tags.has_key?(name)
        set_value(name, value, @tags[name])
      end
  end

end

#get_parentObject



68
69
70
71
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 68

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

#resetObject



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

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 73

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 35

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.has_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



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/aws/parsers/beanstalk/parser.rb', line 22

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