Permalink
Browse files

make all feed entries enumerable

  • Loading branch information...
1 parent 0eea9c8 commit 8434e783b84ca0a86efc29ab0feda312b4ddba74 @archiloque archiloque committed Sep 30, 2011
@@ -1,10 +1,12 @@
module Feedzirra
- module FeedEntryUtilities
+ module FeedEntryUtilities
+
+ include Enumerable
def published
@published ||= @updated
end
-
+
def parse_datetime(string)
begin
DateTime.parse(string).feed_utils_to_gm_time
@@ -13,34 +15,51 @@ def parse_datetime(string)
nil
end
end
-
+
##
# Returns the id of the entry or its url if not id is present, as some formats don't support it
- def id
+ def id
@entry_id ||= @url
end
-
+
##
# Writer for published. By default, we keep the "oldest" publish time found.
- def published=(val)
+ def published=(val)
parsed = parse_datetime(val)
- @published = parsed if !@published || parsed < @published
+ @published = parsed if !@published || parsed < @published
end
-
+
##
# Writer for updated. By default, we keep the most recent update time found.
- def updated=(val)
+ def updated=(val)
parsed = parse_datetime(val)
@updated = parsed if !@updated || parsed > @updated
end
def sanitize!
- self.title.sanitize! if self.title
- self.author.sanitize! if self.author
+ self.title.sanitize! if self.title
+ self.author.sanitize! if self.author
self.summary.sanitize! if self.summary
self.content.sanitize! if self.content
end
-
+
alias_method :last_modified, :published
+
+ def each
+ @rss_fields ||= self.instance_variables
+
+ @rss_fields.each do |field|
+ yield(field.to_s.sub('@', ''), self.instance_variable_get(field))
+ end
+ end
+
+ def [](field)
+ self.instance_variable_get("@#{field.to_s}")
+ end
+
+ def []=(field, value)
+ self.instance_variable_set("@#{field.to_s}", value)
+ end
+
end
end
@@ -5,6 +5,7 @@ module Parser
class AtomEntry
include SAXMachine
include FeedEntryUtilities
+
element :title
element :link, :as => :url, :value => :href, :with => {:type => "text/html", :rel => "alternate"}
element :name, :as => :author
@@ -5,6 +5,7 @@ module Parser
class AtomFeedBurnerEntry
include SAXMachine
include FeedEntryUtilities
+
element :title
element :name, :as => :author
element :link, :as => :url, :value => :href, :with => {:type => "text/html", :rel => "alternate"}
@@ -6,6 +6,7 @@ module Parser
class ITunesRSSItem
include SAXMachine
include FeedEntryUtilities
+
element :author
element :guid
element :title
@@ -3,7 +3,6 @@ module Feedzirra
module Parser
# Parser for dealing with RDF feed entries.
class RSSEntry
- include Enumerable
include SAXMachine
include FeedEntryUtilities
@@ -28,21 +27,6 @@ class RSSEntry
element :guid, :as => :entry_id
- def each
- @rss_fields ||= self.instance_variables
-
- @rss_fields.each do |field|
- yield(field.to_s.sub('@', ''), self.instance_variable_get(field))
- end
- end
-
- def [](field)
- self.instance_variable_get("@#{field.to_s}")
- end
-
- def []=(field, value)
- self.instance_variable_set("@#{field.to_s}", value)
- end
end
end
@@ -6,44 +6,81 @@
# but this is actually how it should work. You would never just pass entry xml straight to the AtomEnry
@entry = Feedzirra::Parser::Atom.parse(sample_atom_feed).entries.first
end
-
+
it "should parse the title" do
@entry.title.should == "AWS Job: Architect & Designer Position in Turkey"
end
-
+
it "should parse the url" do
@entry.url.should == "http://aws.typepad.com/aws/2009/01/aws-job-architect-designer-position-in-turkey.html"
end
-
+
it "should parse the url even when" do
Feedzirra::Parser::Atom.parse(load_sample("atom_with_link_tag_for_url_unmarked.xml")).entries.first.url.should == "http://www.innoq.com/blog/phaus/2009/07/ja.html"
end
-
+
it "should parse the author" do
@entry.author.should == "AWS Editor"
end
-
+
it "should parse the content" do
@entry.content.should == sample_atom_entry_content
end
-
+
it "should provide a summary" do
@entry.summary.should == "Late last year an entrepreneur from Turkey visited me at Amazon HQ in Seattle. We talked about his plans to use AWS as part of his new social video portal startup. I won't spill any beans before he's ready to..."
end
-
+
it "should parse the published date" do
@entry.published.to_s.should == "Fri Jan 16 18:21:00 UTC 2009"
end
it "should parse the categories" do
@entry.categories.should == ['Turkey', 'Seattle']
end
-
+
it "should parse the updated date" do
@entry.updated.to_s.should == "Fri Jan 16 18:21:00 UTC 2009"
end
-
+
it "should parse the id" do
@entry.id.should == "tag:typepad.com,2003:post-61484736"
end
+
+ it "should support each" do
+ @entry.respond_to? :each
+ end
+
+ it "should be able to list out all fields with each" do
+ all_fields = []
+ @entry.each do |field, value|
+ all_fields << field
+ end
+ all_fields.sort == ['author', 'categories', 'content', 'id', 'published', 'summary', 'title', 'url']
+ end
+
+ it "should be able to list out all values with each" do
+ title_value = ''
+ @entry.each do |field, value|
+ title_value = value if field == 'title'
+ end
+ title_value.should == "AWS Job: Architect & Designer Position in Turkey"
+ end
+
+ it "should support checking if a field exists in the entry" do
+ @entry.include?('title') && @entry.include?('author')
+ end
+
+ it "should allow access to fields with hash syntax" do
+ @entry['title'] == @entry.title
+ @entry['title'].should == "AWS Job: Architect & Designer Position in Turkey"
+ @entry['author'] == @entry.author
+ @entry['author'].should == "AWS Editor"
+ end
+
+ it "should allow setting field values with hash syntax" do
+ @entry['title'] = "Foobar"
+ @entry.title.should == "Foobar"
+ end
+
end
@@ -14,35 +14,35 @@
@entry.title = Feedzirra::Parser::RSS.parse(sample_rss_feed).entries.first.title
end
end
-
+
it "should parse the title" do
@entry.title.should == "Nokogiri’s Slop Feature"
end
-
+
it "should parse the url" do
@entry.url.should == "http://tenderlovemaking.com/2008/12/04/nokogiris-slop-feature/"
end
-
+
it "should parse the author" do
@entry.author.should == "Aaron Patterson"
end
-
+
it "should parse the content" do
@entry.content.should == sample_rss_entry_content
end
-
+
it "should provide a summary" do
@entry.summary.should == "Oops! When I released nokogiri version 1.0.7, I totally forgot to talk about Nokogiri::Slop() feature that was added. Why is it called \"slop\"? It lets you sloppily explore documents. Basically, it decorates your document with method_missing() that allows you to search your document via method calls.\nGiven this document:\n\ndoc = Nokogiri::Slop&#40;&#60;&#60;-eohtml&#41;\n&#60;html&#62;\n&#160; &#60;body&#62;\n&#160; [...]"
end
-
- it "should parse the published date" do
+
+ it "should parse the published date" do
@entry.published.to_s.should == "Thu Dec 04 17:17:49 UTC 2008"
end
it "should parse the categories" do
@entry.categories.should == ['computadora', 'nokogiri', 'rails']
end
-
+
it "should parse the guid as id" do
@entry.id.should == "http://tenderlovemaking.com/?p=198"
end
@@ -54,7 +54,7 @@
it "should be able to list out all fields with each" do
all_fields = []
@entry.each do |field, value|
- all_fields << field
+ all_fields << field
end
all_fields.sort == ['author', 'categories', 'content', 'id', 'published', 'summary', 'title', 'url']
end
@@ -72,12 +72,12 @@
end
it "should allow access to fields with hash syntax" do
- @entry['title'] == @entry.title && \
- @entry['title'].should == "Nokogiri’s Slop Feature" && \
- @entry['author'] == @entry.author && \
- @entry['author'].should == "Aaron Patterson"
+ @entry['title'] == @entry.title
+ @entry['title'].should == "Nokogiri’s Slop Feature"
+ @entry['author'] == @entry.author
+ @entry['author'].should == "Aaron Patterson"
end
-
+
it "should allow setting field values with hash syntax" do
@entry['title'] = "Foobar"
@entry.title.should == "Foobar"

0 comments on commit 8434e78

Please sign in to comment.