Skip to content

Commit

Permalink
Implemented RSS 0.9 or RDF
Browse files Browse the repository at this point in the history
  • Loading branch information
zedtux committed Apr 21, 2012
1 parent 1bdc6ce commit 5bd93ef
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 13 deletions.
9 changes: 6 additions & 3 deletions lib/urss/entry.rb
Expand Up @@ -4,14 +4,17 @@ class Urss::Entry
attr_accessor :title, :url, :comments_url, :created_at, :author, :categories, :content, :medias attr_accessor :title, :url, :comments_url, :created_at, :author, :categories, :content, :medias


# ~~~~ Class methods ~~~~ # ~~~~ Class methods ~~~~
def self.build(nokogiri_instance) def self.build(nokogiri_instance, namespace=nil)
raise Urss::NotANokogiriInstance unless nokogiri_instance.is_a?(Nokogiri::XML::Element) raise Urss::NotANokogiriInstance unless nokogiri_instance.is_a?(Nokogiri::XML::Element)


entry = self.new entry = self.new
entry.title = nokogiri_instance.xpath("./title").text entry.title = nokogiri_instance.xpath("./#{namespace}title").text
entry.url = nokogiri_instance.xpath("./link").text entry.url = nokogiri_instance.xpath("./#{namespace}link").text
entry.comments_url = nokogiri_instance.xpath("./comments").text entry.comments_url = nokogiri_instance.xpath("./comments").text
entry.created_at = nokogiri_instance.xpath("./pubDate").text entry.created_at = nokogiri_instance.xpath("./pubDate").text
if entry.created_at.nil? || entry.created_at.empty?
entry.created_at = nokogiri_instance.xpath("./dc:date").text
end
entry.author = nokogiri_instance.xpath("./dc:creator", nokogiri_instance.namespaces).text entry.author = nokogiri_instance.xpath("./dc:creator", nokogiri_instance.namespaces).text
entry.categories = nokogiri_instance.search("category").collect(&:text).join(", ") entry.categories = nokogiri_instance.search("category").collect(&:text).join(", ")
entry.content = nokogiri_instance.xpath("./description").text entry.content = nokogiri_instance.xpath("./description").text
Expand Down
20 changes: 14 additions & 6 deletions lib/urss/rss.rb
Expand Up @@ -7,13 +7,21 @@ class Urss::Rss
def self.build(nokogiri_instance) def self.build(nokogiri_instance)
raise Urss::NotANokogiriInstance unless nokogiri_instance.is_a?(Nokogiri::XML::Document) raise Urss::NotANokogiriInstance unless nokogiri_instance.is_a?(Nokogiri::XML::Document)


rss = self.new namespace = nokogiri_instance.namespaces["xmlns"] ? "xmlns:" : nil
rss.title = nokogiri_instance.xpath("//channel/*[local-name()='title']").text
rss.url = nokogiri_instance.xpath("//channel/*[local-name()='link']").text
rss.description = nokogiri_instance.xpath("//channel/*[local-name()='description']").text
rss.updated_at = nokogiri_instance.xpath("//channel/*[local-name()='pubDate']").text


nokogiri_instance.xpath("//item").each {|item| rss.entries << Urss::Entry.build(item)} rss = self.new
rss.title = nokogiri_instance.xpath("//#{namespace}channel/#{namespace}title").text
rss.url = nokogiri_instance.xpath("//#{namespace}channel/#{namespace}link").text
rss.description = nokogiri_instance.xpath("//#{namespace}channel/#{namespace}description").text
rss.updated_at = nokogiri_instance.xpath("//#{namespace}channel/#{namespace}pubDate").text
if rss.updated_at.nil? || rss.updated_at.empty?
begin
rss.updated_at = nokogiri_instance.xpath("//#{namespace}channel/dc:date").text
rescue Nokogiri::XML::XPath::SyntaxError
# No pubDate or date field
end
end
nokogiri_instance.xpath("//#{namespace}item").each {|item| rss.entries << Urss::Entry.build(item, namespace)}


rss rss
end end
Expand Down
3 changes: 2 additions & 1 deletion spec/support/webmocks.rb
@@ -1,4 +1,5 @@
stub_request(:get, "http://tech.rufy.com").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "rss20.xml")).read, :headers => {}) stub_request(:get, "http://tech.rufy.com").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "rss20.xml")).read, :headers => {})
stub_request(:get, "http://www.ruby-lang.org/en/feeds/news.rss").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "ruby.rss")).read, :headers => {}) stub_request(:get, "http://www.ruby-lang.org/en/feeds/news.rss").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "ruby.rss")).read, :headers => {})
stub_request(:get, "http://www.flickr.com/photos/herval/").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "media_rss.xml")).read, :headers => {}) stub_request(:get, "http://www.flickr.com/photos/herval/").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "media_rss.xml")).read, :headers => {})
stub_request(:get, "http://waxluxembourg.com/feed/").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "wax.rss")).read, :headers => {}) stub_request(:get, "http://waxluxembourg.com/feed/").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "wax.rss")).read, :headers => {})
stub_request(:get, "http://slashdot.org/").to_return(:status => 200, :body => File.open(File.join(File.dirname(__FILE__), "fixtures", "rss09.rdf")).read, :headers => {})
5 changes: 2 additions & 3 deletions spec/urss/rss_spec.rb
Expand Up @@ -27,13 +27,12 @@
subject.should respond_to(:entries) subject.should respond_to(:entries)
subject.should respond_to(:entries=) subject.should respond_to(:entries=)
end end
end


describe "Class Methods" do
it "should respond to build" do it "should respond to build" do
subject.class.should respond_to(:build) subject.class.should respond_to(:build)
end end
end

describe "Class Methods" do
describe "#build" do describe "#build" do
describe "argument" do describe "argument" do
it "should raise an Urss::NotANokogiriInstance exception when passing something different than a Nokogiri::XML::Document instance" do it "should raise an Urss::NotANokogiriInstance exception when passing something different than a Nokogiri::XML::Document instance" do
Expand Down
71 changes: 71 additions & 0 deletions spec/urss_spec.rb
Expand Up @@ -52,6 +52,77 @@
end end
end end


context "RSS 0.9" do
context "when parsing the rss09.xml example file at http://slashdot.org/" do
before { @parsed_rss = subject.at("http://slashdot.org/") }
it "should return an instance of Urss::Rss" do
@parsed_rss.should be_an_instance_of(Urss::Rss)
end
describe "Urss::Rss" do
describe "title" do
it "should return \"Slashdot\"" do
@parsed_rss.title.should == "Slashdot"
end
end
describe "url" do
it "should return \"http://slashdot.org/\"" do
@parsed_rss.url.should == "http://slashdot.org/"
end
end
describe "description" do
it "should return \"News for nerds, stuff that matters\"" do
@parsed_rss.description.should == "News for nerds, stuff that matters"
end
end
describe "updated_at" do
it "should return \"2005-09-09T02:52:31-07:00\"" do
@parsed_rss.updated_at.should == "2005-09-09T02:52:31-07:00"
end
end
describe"entries" do
describe "size" do
it "should be 10" do
@parsed_rss.entries.size.should be 10
end
end
describe "first" do
before { @first_parsed_rss = @parsed_rss.entries.first }
describe "title" do
it "should return \"JBoss - A Developer's Notebook\"" do
@first_parsed_rss.title.should == "JBoss - A Developer's Notebook"
end
end
describe "url" do
it "should return \"http://books.slashdot.org/article.pl?sid=05/08/29/1319236&amp;from=rss\"" do
@first_parsed_rss.url.should == "http://books.slashdot.org/article.pl?sid=05/08/29/1319236&from=rss"
end
end
describe "comments_url" do
it "should be empty" do
@first_parsed_rss.comments_url.should be_empty
end
end
describe "created_at" do
it "should return \"2005-09-09T02:52:31-07:00\"" do
@first_parsed_rss.created_at.should == "2005-09-09T02:52:31-07:00"
end
end
describe "author" do
it "should be empty" do
@first_parsed_rss.author.should be_empty
end
end
describe "categories" do
it "should be empty" do
@first_parsed_rss.categories.should be_empty
end
end
end
end
end
end
end

context "RSS 2.0" do context "RSS 2.0" do
context "without media" do context "without media" do
context "when parsing the rss20.xml example file at http://tech.rufy.com" do context "when parsing the rss20.xml example file at http://tech.rufy.com" do
Expand Down

0 comments on commit 5bd93ef

Please sign in to comment.