bundles_all doesn't work if there are no bundles #21

Open
yyamano opened this Issue Nov 30, 2011 · 1 comment

1 participant

@yyamano

If there are no bundles, bundles_all doesn't work as below:

yyamano@junie% ruby19 foo.rb 
/usr/pkg/lib/ruby/gems/1.9/gems/www-delicious-0.4.0/lib/www/delicious.rb:625:in `parse_and_validate_response': undefined method `name' for nil:NilClass (NoMethodError)
    from /usr/pkg/lib/ruby/gems/1.9/gems/www-delicious-0.4.0/lib/www/delicious.rb:660:in `parse_bundle_collection'
    from /usr/pkg/lib/ruby/gems/1.9/gems/www-delicious-0.4.0/lib/www/delicious.rb:275:in `bundles_all'
    from foo.rb:4:in `<main>'
yyamano@junie% cat foo.rb 
require 'www/delicious'
d = WWW::Delicious.new('USER_NAME', 'PASSWORD')

bundles = d.bundles_all
bundles.each { |b| p b }

del.icio.us seems to return xml declaration only if no bundles.

yyamano@junie% curl https://USER_NAME:PASSWORD@api.del.icio.us/v1/tags/bundles/all?
<?xml version="1.0" encoding="UTF-8"?>

The following patch fixes my probelem, but I'm not sure if the current behaviour of del.icio.us is correct. test/testcases/response/bundles_all_empty.xml expects tag.

--- delicious.rb.orig   2011-11-30 12:16:14.000000000 -0800
+++ delicious.rb    2011-11-30 12:26:04.000000000 -0800
@@ -622,11 +622,13 @@
       def parse_and_validate_response(body, options = {})
         dom = REXML::Document.new(body)

-        if (value = options[:root_name]) && dom.root.name != value
-          raise ResponseError, "Invalid response, root node is not `#{value}`"
-        end
-        if (value = options[:root_text]) && dom.root.text != value
-          raise ResponseError, value
+        if (!dom.root.nil?)
+          if (value = options[:root_name]) && dom.root.name != value
+            raise ResponseError, "Invalid response, root node is not `#{value}`"
+          end
+          if (value = options[:root_text]) && dom.root.text != value
+            raise ResponseError, value
+          end
         end

         return dom
@@ -658,6 +660,9 @@
       # and returns an array of <tt>WWW::Delicious::Bundle</tt>.
       def parse_bundle_collection(body)
         dom = parse_and_validate_response(body, :root_name => 'bundles')
+        if (dom.root.nil?)
+          return []
+        end
         dom.root.elements.collect('bundle') { |xml| Bundle.from_rexml(xml) }
       end
@yyamano

Mmm, see the patch at yyamano@133075e
I will send a pull request if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment