Class: Amazon::Ecs::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/ecs.rb

Overview

Response object returned after a REST call to Amazon service.

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Response

XML input is in string format


157
158
159
160
161
162
# File 'lib/amazon/ecs.rb', line 157

def initialize(xml)
  @doc = Nokogiri::XML(xml, nil, 'UTF-8')
  @doc.remove_namespaces!
  # @doc.xpath("//*").each { |elem| elem.name = elem.name.downcase }
  # @doc.xpath("//@*").each { |att| att.name = att.name.downcase }
end

Instance Method Details

#/(path) ⇒ Object

Return a Nokogiri::XML::NodeSet of elements matching the given path.


170
171
172
173
174
# File 'lib/amazon/ecs.rb', line 170

def /(path)
  elements = @doc/path
  return nil if elements.size == 0
  elements
end

#docObject

Return Nokogiri::XML::Document object.


165
166
167
# File 'lib/amazon/ecs.rb', line 165

def doc
  @doc
end

#errorObject

Return error message.


200
201
202
# File 'lib/amazon/ecs.rb', line 200

def error
  Element.get(@doc, "//Error/Message")
end

#error_codeObject

Return error code


205
206
207
# File 'lib/amazon/ecs.rb', line 205

def error_code
  Element.get(@doc, "//Error/Code")
end

#first_itemObject

Return the first item (Amazon::Element)


215
216
217
# File 'lib/amazon/ecs.rb', line 215

def first_item
  items.first
end

#get_element(path) ⇒ Object

Return the first element found


184
185
186
187
# File 'lib/amazon/ecs.rb', line 184

def get_element(path)
  elements = get_elements(path)
  elements[0] if elements
end

#get_elements(path) ⇒ Object

Return an array of Amazon::Element matching the given path


177
178
179
180
181
# File 'lib/amazon/ecs.rb', line 177

def get_elements(path)
  elements = self./(path)
  return unless elements
  elements = elements.map{|element| Element.new(element)}
end

#has_error?Boolean

Return true if response has an error.

Returns:

  • (Boolean)

195
196
197
# File 'lib/amazon/ecs.rb', line 195

def has_error?
  !(error.nil? || error.empty?)
end

#is_valid_request?Boolean

Return true if request is valid.

Returns:

  • (Boolean)

190
191
192
# File 'lib/amazon/ecs.rb', line 190

def is_valid_request?
  Element.get(@doc, "//IsValid") == "True"
end

#item_pageObject

Return current page no if :item_page option is when initiating the request.


220
221
222
# File 'lib/amazon/ecs.rb', line 220

def item_page
  @item_page ||= Element.get(@doc, "//ItemPage").to_i
end

#itemsObject

Return an array of Amazon::Element item objects.


210
211
212
# File 'lib/amazon/ecs.rb', line 210

def items
  @items ||= (@doc/"Item").collect { |item| Element.new(item) }
end

#marshal_dumpObject


234
235
236
# File 'lib/amazon/ecs.rb', line 234

def marshal_dump
  @doc.to_s
end

#marshal_load(xml) ⇒ Object


238
239
240
# File 'lib/amazon/ecs.rb', line 238

def marshal_load(xml)
  initialize(xml)
end

#total_pagesObject

Return total pages.


230
231
232
# File 'lib/amazon/ecs.rb', line 230

def total_pages
  @total_pages ||= Element.get(@doc, "//TotalPages").to_i
end

#total_resultsObject

Return total results.


225
226
227
# File 'lib/amazon/ecs.rb', line 225

def total_results
  @total_results ||= Element.get(@doc, "//TotalResults").to_i
end