Browse files

WIP

  • Loading branch information...
1 parent a515abf commit 82821b9104318bd141c561380bc9755db4fcbdcb @grempe committed Jun 11, 2009
Showing with 62 additions and 22 deletions.
  1. +5 −4 lib/EC2.rb
  2. +57 −18 lib/EC2/responses.rb
View
9 lib/EC2.rb
@@ -8,7 +8,7 @@
# Home:: http://github.com/grempe/amazon-ec2/tree/master
#++
-%w[ base64 cgi openssl digest/sha1 net/https rexml/document time ostruct ].each { |f| require f }
+%w[ base64 cgi openssl digest/sha1 net/https rexml/document time ostruct nokogiri ].each { |f| require f }
# Require any lib files that we have bundled with this Ruby Gem in the lib/EC2 directory.
# Parts of the EC2 module and Base class are broken out into separate
@@ -118,7 +118,7 @@ def initialize( options = {} )
raise ArgumentError, "No :use_ssl value provided" if options[:use_ssl].nil?
raise ArgumentError, "Invalid :use_ssl value provided, only 'true' or 'false' allowed" unless options[:use_ssl] == true || options[:use_ssl] == false
raise ArgumentError, "No :server provided" if options[:server].nil? || options[:server].empty?
-
+
if options[:port]
# user-specified port
@port = options[:port]
@@ -222,8 +222,9 @@ def response_generator( options = {} )
raise ArgumentError, ":action must be provided to response_generator" if options[:action].nil? || options[:action].empty?
http_response = make_request(options[:action], options[:params])
- http_xml = http_response.body
- return Response.parse(:xml => http_xml)
+ doc = Response.parse(:xml => http_response.body)
+puts doc.to_hash.inspect
+ return doc.to_hash
end
View
75 lib/EC2/responses.rb
@@ -34,31 +34,70 @@ module EC2
# may be raised by this library in YOUR code with a 'rescue' clauses. It is up to you
# how gracefully you want to handle these exceptions that are raised.
-
- require 'rubygems'
- begin
- require 'xmlsimple' unless defined? XmlSimple
- rescue Exception => e
- require 'xml-simple' unless defined? XmlSimple
- end
-
class Response
def self.parse(options = {})
- options = {
- :xml => "",
- :parse_options => { 'forcearray' => ['item'], 'suppressempty' => nil, 'keeproot' => false }
- }.merge(options)
+ options = { :xml => "" }.merge(options)
+ response = Nokogiri::XML(options[:xml])
+ end
+
+ end # class Response
+
+ # monkey patch NokoGiri to provide #to_hash
+ module Conversions #:nodoc:
+ module Document #:nodoc:
+ def to_hash
+ root.to_hash
+ end
+ end
- # NOTE: Parsing the response as a nested set of Response objects was extremely
- # memory intensive and appeared to leak (the memory was not freed on subsequent requests).
- # It was changed to return the raw XmlSimple response.
+ module Node #:nodoc:
+ CONTENT_ROOT = ''
- response = XmlSimple.xml_in(options[:xml], options[:parse_options])
+ # Convert XML document to hash
+ #
+ # hash::
+ # Hash to merge the converted element into.
+ def to_hash(hash = {})
+ hash[name] ||= attributes_as_hash
- return response
+ walker = lambda { |memo, parent, child, callback|
+ next if child.blank? && 'file' != parent['type']
+
+ if child.text?
+ (memo[CONTENT_ROOT] ||= '') << child.content
+ next
+ end
+
+ name = child.name
+
+ child_hash = child.attributes_as_hash
+ if memo[name]
+ memo[name] = [memo[name]].flatten
+ memo[name] << child_hash
+ else
+ memo[name] = child_hash
+ end
+
+ # Recusively walk children
+ child.children.each { |c|
+ callback.call(child_hash, child, c, callback)
+ }
+ }
+
+ children.each { |c| walker.call(hash[name], self, c, walker) }
+ hash
+ end
+
+ def attributes_as_hash
+ Hash[*(attribute_nodes.map { |node|
+ [node.node_name, node.value]
+ }.flatten)]
+ end
end
+ end
- end # class Response
+ Nokogiri::XML::Document.send(:include, Conversions::Document)
+ Nokogiri::XML::Node.send(:include, Conversions::Node)
end # module EC2

0 comments on commit 82821b9

Please sign in to comment.