Class: EC2::Response

Inherits:
OpenStruct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/EC2/responses.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/EC2/responses.rb', line 48

def self.parse(options = {})
  options = {
    :xml => "",
    :parse_options => { 'ForceArray' => ['item'], 'SuppressEmpty' => nil }
  }.merge(options)
  response = Response.new(XmlSimple.xml_in(options[:xml], options[:parse_options]))

  # set the xml attribute of the response object to contain the original XML that was
  # returned by amazon.  This allows anyone to bypass our parsing if desired and just
  # get right at the raw XML response.
  response.xml = options[:xml]
  return response
end

Instance Method Details

#[](member) ⇒ Object

Alternative method for getting members.



94
95
96
# File 'lib/EC2/responses.rb', line 94

def [](member)
  send(member)
end

#[]=(member, value) ⇒ Object

Alternative method for setting members.



100
101
102
# File 'lib/EC2/responses.rb', line 100

def []=(member, value)
  send("#{member}=", value)
end

#eachObject

Required by the Enumerable module. Iterate over each item in the members array and pass as a value the block passed to each using yield.



73
74
75
76
77
78
# File 'lib/EC2/responses.rb', line 73

def each
  members.each do |method|
    yield send(method)
  end
  self
end

#each_pairObject

Same as the each method, but with both key and value.

Sample Use: obj.each_pair { |k,v| puts “key: #k, value: #v” }



85
86
87
88
89
90
# File 'lib/EC2/responses.rb', line 85

def each_pair
  members.each do |method|
    yield method, send(method)
  end
  self
end

#membersObject

Every member of an OpenStruct object has getters and setters, the latter of which has a method ending in “=”. Find all of these methods, excluding those defined on parent classes.



66
67
68
# File 'lib/EC2/responses.rb', line 66

def members
  methods(false).sort.grep(/=/).map { |m| m[0...-1] }
end

#to_sObject

Override of to string method.



129
130
131
# File 'lib/EC2/responses.rb', line 129

def to_s
  return to_string
end

#to_string(short = false) ⇒ Object

Helper for converting to string which support a long and short version to avoid recursion problems with parents.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/EC2/responses.rb', line 107

def to_string(short=false)
  s = "#<#{self.class}:0x#{(2 ** 32 + object_id).to_s(16).upcase}"
  if (short)
    s += " ..."
  else
    each_pair { |k,v|
      if (v == self.parent && v.kind_of?(Response))
        v = v.to_string(true)
      elsif (v.kind_of?(String))
        v = "\"#{v.gsub("\"", "\\\"")}\""
      elsif (v.kind_of?(NilClass))
        v = "nil"
      end
      s += " #{k}=#{v}"
    }
  end
  s += ">"
  return s
end