aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib/active_resource
diff options
context:
space:
mode:
authorJosh Kalderimis <josh.kalderimis@gmail.com>2011-05-17 19:30:43 -0400
committerJosh Kalderimis <josh.kalderimis@gmail.com>2011-05-18 00:34:09 -0400
commit6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa (patch)
tree197f51ebb7b9b9c7987bb29aec73b7da9536cff7 /activeresource/lib/active_resource
parent4a9365ee18c055d970a5f8faaac9443a8bfb0d24 (diff)
downloadrails-6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa.tar.gz
rails-6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa.tar.bz2
rails-6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa.zip
updated all the tests in ARes to work with json
Diffstat (limited to 'activeresource/lib/active_resource')
-rw-r--r--activeresource/lib/active_resource/base.rb17
-rw-r--r--activeresource/lib/active_resource/connection.rb2
-rw-r--r--activeresource/lib/active_resource/custom_methods.rb4
-rw-r--r--activeresource/lib/active_resource/formats.rb8
-rw-r--r--activeresource/lib/active_resource/formats/json_format.rb2
-rw-r--r--activeresource/lib/active_resource/formats/xml_format.rb13
6 files changed, 26 insertions, 20 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 7f2a844723..935cfe5ecf 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -498,9 +498,9 @@ module ActiveResource
connection.format = format if site
end
- # Returns the current format, default is ActiveResource::Formats::XmlFormat.
+ # Returns the current format, default is ActiveResource::Formats::JsonFormat.
def format
- self._format || ActiveResource::Formats::XmlFormat
+ self._format || ActiveResource::Formats::JsonFormat
end
# Sets the number of seconds after which requests to the REST API should time out.
@@ -1232,9 +1232,16 @@ module ActiveResource
# your_supplier = Supplier.new
# your_supplier.load(my_attrs)
# your_supplier.save
- def load(attributes)
+ def load(attributes, remove_root = false)
raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
@prefix_options, attributes = split_options(attributes)
+
+ if attributes.keys.size == 1
+ remove_root = self.class.element_name == attributes.keys.first.to_s
+ end
+
+ attributes = Formats.remove_root(attributes) if remove_root
+
attributes.each do |key, value|
@attributes[key.to_s] =
case value
@@ -1285,7 +1292,7 @@ module ActiveResource
# resource's attributes, the full body of the request will still be sent
# in the save request to the remote service.
def update_attributes(attributes)
- load(attributes) && save
+ load(attributes, false) && save
end
# For checking <tt>respond_to?</tt> without searching the attributes (which is faster).
@@ -1339,7 +1346,7 @@ module ActiveResource
def load_attributes_from_response(response)
if !response['Content-Length'].blank? && response['Content-Length'] != "0" && !response.body.nil? && response.body.strip.size > 0
- load(self.class.format.decode(response.body))
+ load(self.class.format.decode(response.body), true)
@persisted = true
end
end
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb
index 765575d866..d923204dde 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -30,7 +30,7 @@ module ActiveResource
# The +site+ parameter is required and will set the +site+
# attribute to the URI for the remote resource service.
- def initialize(site, format = ActiveResource::Formats::XmlFormat)
+ def initialize(site, format = ActiveResource::Formats::JsonFormat)
raise ArgumentError, 'Missing site URI' unless site
@user = @password = nil
self.site = site
diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb
index 9879f8cded..38473c6cf7 100644
--- a/activeresource/lib/active_resource/custom_methods.rb
+++ b/activeresource/lib/active_resource/custom_methods.rb
@@ -54,7 +54,9 @@ module ActiveResource
#
# Person.find(:all, :from => :active)
def get(custom_method_name, options = {})
- format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body)
+ hashified = format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body)
+ derooted = Formats.remove_root(hashified)
+ derooted.is_a?(Array) ? derooted.map { |e| Formats.remove_root(e) } : derooted
end
def post(custom_method_name, options = {}, body = '')
diff --git a/activeresource/lib/active_resource/formats.rb b/activeresource/lib/active_resource/formats.rb
index 53b75b34e7..f7ad689cc5 100644
--- a/activeresource/lib/active_resource/formats.rb
+++ b/activeresource/lib/active_resource/formats.rb
@@ -10,5 +10,13 @@ module ActiveResource
def self.[](mime_type_reference)
ActiveResource::Formats.const_get(ActiveSupport::Inflector.camelize(mime_type_reference.to_s) + "Format")
end
+
+ def self.remove_root(data)
+ if data.is_a?(Hash) && data.keys.size == 1
+ data.values.first
+ else
+ data
+ end
+ end
end
end
diff --git a/activeresource/lib/active_resource/formats/json_format.rb b/activeresource/lib/active_resource/formats/json_format.rb
index 9980634921..827d1cc23a 100644
--- a/activeresource/lib/active_resource/formats/json_format.rb
+++ b/activeresource/lib/active_resource/formats/json_format.rb
@@ -18,7 +18,7 @@ module ActiveResource
end
def decode(json)
- ActiveSupport::JSON.decode(json)
+ Formats.remove_root(ActiveSupport::JSON.decode(json))
end
end
end
diff --git a/activeresource/lib/active_resource/formats/xml_format.rb b/activeresource/lib/active_resource/formats/xml_format.rb
index 3b2575cfa1..49cb9aa1ac 100644
--- a/activeresource/lib/active_resource/formats/xml_format.rb
+++ b/activeresource/lib/active_resource/formats/xml_format.rb
@@ -18,19 +18,8 @@ module ActiveResource
end
def decode(xml)
- from_xml_data(Hash.from_xml(xml))
+ Formats.remove_root(Hash.from_xml(xml))
end
-
- private
- # Manipulate from_xml Hash, because xml_simple is not exactly what we
- # want for Active Resource.
- def from_xml_data(data)
- if data.is_a?(Hash) && data.keys.size == 1
- data.values.first
- else
- data
- end
- end
end
end
end