Permalink
Browse files

Merge pull request #65 from semperos/rss-entry-enum

Add `Enumerable` behavior to `Feedzirra::Parser::RSSEntry`
  • Loading branch information...
2 parents 1715a1e + 041dcd4 commit 1bcafd51bf7dd7d4520be24b3e9f74b16b8a141a @archiloque archiloque committed Sep 30, 2011
Showing with 68 additions and 7 deletions.
  1. +24 −6 lib/feedzirra/parser/rss_entry.rb
  2. +44 −1 spec/feedzirra/parser/rss_entry_spec.rb
@@ -3,30 +3,48 @@ module Feedzirra
module Parser
# Parser for dealing with RDF feed entries.
class RSSEntry
+ include Enumerable
include SAXMachine
include FeedEntryUtilities
+
element :title
element :link, :as => :url
-
+
element :"dc:creator", :as => :author
element :author, :as => :author
element :"content:encoded", :as => :content
element :description, :as => :summary
-
+
element :pubDate, :as => :published
element :pubdate, :as => :published
element :"dc:date", :as => :published
element :"dc:Date", :as => :published
element :"dcterms:created", :as => :published
-
-
+
+
element :"dcterms:modified", :as => :updated
element :issued, :as => :published
elements :category, :as => :categories
-
+
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
-end
+end
@@ -7,6 +7,13 @@
# but this is actually how it should work. You would never just pass entry xml straight to the AtomEnry
@entry = Feedzirra::Parser::RSS.parse(sample_rss_feed).entries.first
end
+
+ after(:each) do
+ # We change the title in one or more specs to test []=
+ if @entry.title != "Nokogiri’s Slop Feature"
+ @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"
@@ -39,4 +46,40 @@
it "should parse the guid as id" do
@entry.id.should == "http://tenderlovemaking.com/?p=198"
end
-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 == "Nokogiri’s Slop Feature"
+ 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 == "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"
+ end
+end

0 comments on commit 1bcafd5

Please sign in to comment.