Permalink
Browse files

Reparse broken time instances. Closes issue 13.

git-svn-id: http://feed-normalizer.googlecode.com/svn/trunk@61 4b21cefc-af1e-0410-b352-37e3eef200da
  • Loading branch information...
sciodev
sciodev committed Oct 28, 2007
1 parent 9445d26 commit e7ed6e8ba349531d5192d3aa5db5c961852fcb01
Showing with 44 additions and 3 deletions.
  1. +32 −3 lib/structures.rb
  2. +12 −0 test/test_feednormalizer.rb
View
@@ -115,6 +115,23 @@ def clean!
end
end
+ module TimeFix
+ # Reparse any Time instances, due to RSS::Parser's redefinition of
+ # certain aspects of the Time class that creates unexpected behaviour
+ # when extending the Time class, as some common third party libraries do.
+ # See http://code.google.com/p/feed-normalizer/issues/detail?id=13.
+ def reparse(obj)
+ @parsed ||= false
+
+ return obj if @parsed
+
+ if obj.is_a?(Time)
+ @parsed = true
+ Time.at(obj) rescue obj
+ end
+ end
+ end
+
# Represents a feed item entry.
# Available fields are:
@@ -128,7 +145,7 @@ def clean!
# * copyright
# * categories
class Entry
- include Singular, ElementEquality, ElementCleaner
+ include Singular, ElementEquality, ElementCleaner, TimeFix
HTML_ELEMENTS = [:content, :description, :title]
SIMPLE_ELEMENTS = [:date_published, :urls, :id, :authors, :copyright, :categories]
@@ -142,6 +159,12 @@ def initialize
@urls = []
@authors = []
@categories = []
+ @date_published = nil
+ end
+
+ undef date_published
+ def date_published
+ @date_published = reparse(@date_published)
end
end
@@ -157,9 +180,9 @@ def initialize
# * urls / url
# * image
# * generator
- # * items / channel
+ # * items / channel
class Feed
- include Singular, ElementEquality, ElementCleaner
+ include Singular, ElementEquality, ElementCleaner, TimeFix
# Elements that can contain HTML fragments.
HTML_ELEMENTS = [:title, :description]
@@ -183,6 +206,12 @@ def initialize(wrapper)
@authors = []
@items = []
@parser = wrapper.parser.to_s
+ @last_updated = nil
+ end
+
+ undef last_updated
+ def last_updated
+ @last_updated = reparse(@last_updated)
end
def channel() self end
@@ -1,4 +1,6 @@
require 'test/unit'
+$:.unshift(File.dirname(__FILE__))
+$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'feed-normalizer'
require 'yaml'
@@ -209,5 +211,15 @@ def test_atom_content_contains_pluses
assert_equal 2, feed.entries.last.content.scan(/\+/).size
end
+ # http://code.google.com/p/feed-normalizer/issues/detail?id=13
+ def test_times_are_reparsed
+ feed = FeedNormalizer::FeedNormalizer.parse(XML_FILES[:rss20], :force_parser => RubyRssParser, :try_others => false)
+
+ Time.class_eval "alias :old_to_s :to_s; def to_s(x=1); old_to_s; end"
+
+ assert_equal "Sat Sep 09 07:57:06 -0700 2006", feed.last_updated.to_s(:foo)
+ assert_equal "Sat Sep 09 05:45:35 -0700 2006", feed.entries.first.date_published.to_s(:foo)
+ end
+
end

0 comments on commit e7ed6e8

Please sign in to comment.