This is a topic in Basecamp API

Sample ruby Basecamp API implementation breaks Hash#to_xml

 
Avatar Vladimir Mer... 1 post

Hello.

My application uses ActiveSupport’s Hash#to_xml behaviour, it is called indirectly via serializing ActiveRecord object, and every time I got basecamp.rb loaded it breaks it, because it’s to_xml doesn’t accept any parameters, while ActiveSupport’s to_xml does accept, and it passes it to nested objects (I’m serializing array of hashes).

Test to reproduce:


$ ./script/console
Loading development environment (Rails 2.0.2)
>> [{1=>2}].to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<records type=\"array\">\n  <record>\n    <1 type=\"integer\">2</1>\n  </record>\n</records>\n" 
>> require 'basecamp'
=> ["Basecamp"]
>> [{1=>2}].to_xml
ArgumentError: wrong number of arguments (1 for 0)
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/array/conversions.rb:86:in `to_xml'
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/array/conversions.rb:86:in `to_xml'
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/array/conversions.rb:86:in `each'
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/array/conversions.rb:86:in `to_xml'
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb:134:in `call'
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb:134:in `_nested_structures'
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb:58:in `method_missing'
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb:31:in `tag!'
    from /var/lib/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/array/conversions.rb:84:in `to_xml'
    from (irb):3

UPDATE: And here is the fix:


Index: /home/stiff/workspace/app/lib/basecamp.rb
===================================================================
--- /home/stiff/workspace/app/lib/basecamp.rb    (revision 1239)
+++ /home/stiff/workspace/app/lib/basecamp.rb    (working copy)
@@ -387,7 +387,11 @@
     end

     def convert_body(body)
-      body = use_xml ? body.to_xml : body.to_yaml
+      if use_xml
+        XmlSimple.xml_out({:request => body}, 'keeproot' => true, 'noattr' => true)
+      else
+        body.to_yaml
+      end
     end

     def content_type
@@ -482,9 +486,3 @@
     to_s[*args]
   end
 end
-
-class Hash
-  def to_xml
-    XmlSimple.xml_out({:request => self}, 'keeproot' => true, 'noattr' => true)
-  end
-end

I think it would be nice to fix it on the developer.37signals.com site. Thanks

Signup or login to post a reply.