소스 검색

Fix a bug where an empty `<link>` is wrongly parsed

Due to a problem in sax-machine's internals, an empty `<link/>` in RSS
would be parsed to JSON as `{ "href": "no_buffer" }`.  Now empty
`<link/>` elements in RSS and ATOM are simply ignored just like other
collection elements like `<category>`.
Akinori MUSHA 8 년 전
부모
커밋
4d10132709
1개의 변경된 파일28개의 추가작업 그리고 8개의 파일을 삭제
  1. 28 8
      lib/feedjira_extension.rb

+ 28 - 8
lib/feedjira_extension.rb

@@ -80,6 +80,12 @@ module FeedjiraExtension
       attribute attr
     end
 
+    def empty?
+      LINK_ATTRS.all? { |attr|
+        __send__(attr).nil?
+      }
+    end
+
     def to_json(options = nil)
       LINK_ATTRS.each_with_object({}) { |key, hash|
         if value = __send__(key)
@@ -94,10 +100,20 @@ module FeedjiraExtension
 
     value :href
 
+    def empty?
+      !href.is_a?(String)
+    end
+
     def to_json(options = nil)
-      {
-        href: href
-      }.to_json(options)
+      case href
+      when String
+        { href: href }
+      else
+        # Ignore non-string values, because SaxMachine leaks its
+        # internal value :no_buffer when the content of an element
+        # is empty.
+        {}
+      end.to_json(options)
     end
   end
 
@@ -174,14 +190,18 @@ module FeedjiraExtension
           when /FeedBurner/
             elements :'atok10:link', class: AtomLink, as: :atom_links
 
-              def links
-                @links ||= [*rss_links, *atom_links]
-              end
+            def _links
+              [*rss_links, *atom_links]
+            end
           else
-            alias_method :links, :rss_links
+            alias_method :_links, :rss_links
           end
         else
-          elements :link, class: AtomLink, as: :links
+          elements :link, class: AtomLink, as: :_links
+        end
+
+        def links
+          _links.reject(&:empty?)
         end
 
         def alternate_link