From: Jan Provaznik <[email protected]>
Various chunks of XML response were parsed by Nokogiri multiple times.
Now responses is parsed only once and parsed object is passed to methods
instead of converting to string and then reparsing the string again.
---
client/lib/base_object.rb | 7 +++----
client/lib/client_bucket_methods.rb | 4 ++--
client/lib/deltacloud.rb | 21 +++++++++++----------
3 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/client/lib/base_object.rb b/client/lib/base_object.rb
index 6a28d5a..d5d17fa 100644
--- a/client/lib/base_object.rb
+++ b/client/lib/base_object.rb
@@ -371,10 +371,9 @@ module DeltaCloud
DeltaCloud::API.const_get(parent.classify).const_get(name.classify)
end
- def self.guess_model_type(response)
- response = Nokogiri::XML(response.to_s)
- return :action if ((response/'//actions').length >= 1) and
((response/'//state').length == 0)
- return :stateful if ((response/'//actions').length >= 1) and
((response/'//state').length >= 1)
+ def self.guess_model_type(xml)
+ return :action if ((xml/'//actions').length >= 1) and
((xml/'//state').length == 0)
+ return :stateful if ((xml/'//actions').length >= 1) and
((xml/'//state').length >= 1)
return :base
end
diff --git a/client/lib/client_bucket_methods.rb
b/client/lib/client_bucket_methods.rb
index a3dfda0..49035a9 100644
--- a/client/lib/client_bucket_methods.rb
+++ b/client/lib/client_bucket_methods.rb
@@ -19,7 +19,7 @@ module ClientBucketMethods
obj = nil
request(:post, "#{api_uri.to_s}/buckets", {:name =>
params['id'],:location=>params['bucket_location'] }) do |response|
handle_backend_error(response) if response.code!=201
- obj = base_object(:bucket, response)
+ obj = base_object(:bucket, Nokogiri::XML(response))
end
end
@@ -44,7 +44,7 @@ module ClientBucketMethods
end
resource.send(:post, {:blob_data => File.new(params['file_path'], 'rb'),
:blob_id => params[:id]}, headers) do |response, request, block|
handle_backend_error(response) if response.code.eql?(500)
- blob = base_object(:blob, response)
+ blob = base_object(:blob, Nokogiri::XML(response))
yield blob if block_given?
end
return blob
diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index c3efcde..d3ee9d4 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -136,13 +136,13 @@ module DeltaCloud
define_method model do |*args|
request(:get, entry_points[model], args.first) do |response|
- base_object_collection(model, response)
+ base_object_collection(model, Nokogiri::XML(response))
end
end
define_method :"#{model.to_s.singularize}" do |*args|
request(:get, "#{entry_points[model]}/#{args[0]}") do |response|
- base_object(model, response)
+ base_object(model, Nokogiri::XML(response))
end
end
@@ -159,7 +159,7 @@ module DeltaCloud
bucket = args[0]["bucket"]
blob = args[0][:id]
request(:get, "#{entry_points[:buckets]}/#{bucket}/#{blob}") do
|response|
- base_object("blob", response)
+ base_object("blob", Nokogiri::XML(response))
end
end
end
@@ -167,16 +167,17 @@ module DeltaCloud
end
end
- def base_object_collection(model, response)
-
Nokogiri::XML(response).xpath("#{model}/#{model.to_s.singularize}").collect do
|item|
- base_object(model, item.to_s)
+ def base_object_collection(model, xml)
+ xml.xpath("#{model}/#{model.to_s.singularize}").collect do |item|
+ base_object(model, item, false)
end
end
# Add default attributes [id and href] to class
- def base_object(model, response)
- c = DeltaCloud.add_class("#{model}",
DeltaCloud::guess_model_type(response))
- xml_to_class(c,
Nokogiri::XML(response).xpath("#{model.to_s.singularize}").first)
+ def base_object(model, xml, nested = true)
+ c = DeltaCloud.add_class("#{model}", DeltaCloud::guess_model_type(xml))
+ obj = nested ? xml.xpath("#{model.to_s.singularize}").first : xml
+ xml_to_class(c, obj)
end
# Convert XML response to defined Ruby Class
@@ -319,7 +320,7 @@ module DeltaCloud
obj = nil
request(:post, entry_points[:"#{$1}s"], {}, params) do |response|
- obj = base_object(:"#{$1}", response)
+ obj = base_object(:"#{$1}", Nokogiri::XML(response))
response_error(response) unless response_successful?(response.code)
yield obj if block_given?
end
--
1.7.11.7