Skip to content

Commit

Permalink
MediaRSS support, support for mapping tag attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
herval authored and Lucas Carlson committed Jul 6, 2010
1 parent 0915f34 commit 225a6d6
Show file tree
Hide file tree
Showing 4 changed files with 504 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README
Expand Up @@ -32,10 +32,11 @@ The parser does not care about the correctness of the XML as it does not use an
SimpleRSS.feed_tags << :some_new_tag
SimpleRSS.item_tags << :"item+myrel" # this will extend SimpleRSS to be able to parse RSS items or ATOM entries that have a rel specified, common in many blogger feeds
SimpleRSS.item_tags << :"feedburner:origLink" # this will extend SimpleRSS to be able to parse RSS items or ATOM entries that have a specific pre-tag specified, common in many feedburner feeds

SimpleRSS.item_tags << :"media:content#url" # this will grab the url attribute of the media:content tag

== Authors
* Lucas Carlson (mailto:lucas@rufy.com)
* Herval Freire (mailto:hervalfreire@gmail.com)

Inspired by Blagg (http://www.raelity.org/lang/perl/blagg) from Rael Dornfest.

Expand Down
18 changes: 16 additions & 2 deletions lib/simple-rss.rb
Expand Up @@ -2,7 +2,7 @@
require 'time'

class SimpleRSS
VERSION = "1.2.2"
VERSION = "1.2.3"

attr_reader :items, :source
alias :entries :items
Expand Down Expand Up @@ -30,7 +30,11 @@ class SimpleRSS
:category, :guid,
:'trackback:ping', :'trackback:about',
:'dc:creator', :'dc:title', :'dc:subject', :'dc:rights', :'dc:publisher',
:'feedburner:origLink'
:'feedburner:origLink',
:'media:content#url', :'media:content#type', :'media:content#height', :'media:content#width',
:'media:title', :'media:thumbnail#url', :'media:thumbnail#height', :'media:thumbnail#width',
:'media:credit', :'media:credit#role',
:'media:category', :'media:category#scheme'
]

def initialize(source, options={})
Expand Down Expand Up @@ -106,6 +110,16 @@ def parse
nil
end
item[clean_tag("#{tag}+#{rel}")] = clean_content(tag, $3, $4) if $3 || $4
elsif tag.to_s.include?("#")
tag_data = tag.to_s.split("#")
tag = tag_data[0]
attrib = tag_data[1]
if match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)#{attrib}=['"](.*?)['"](.*?)>(.*?)</(rss:|atom:)?#{tag}>}mi
nil
elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)#{attrib}=['"](.*?)['"](.*?)/\s*>}mi
nil
end
item[clean_tag("#{tag}_#{attrib}")] = clean_content(tag, attrib, $3) if $3
else
if match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)</(rss:|atom:)?#{tag}>}mi
nil
Expand Down
21 changes: 21 additions & 0 deletions test/base/base_test.rb
Expand Up @@ -3,6 +3,7 @@ class BaseTest < Test::Unit::TestCase
def setup
@rss09 = SimpleRSS.parse open(File.dirname(__FILE__) + '/../data/rss09.rdf')
@rss20 = SimpleRSS.parse open(File.dirname(__FILE__) + '/../data/rss20.xml')
@media_rss = SimpleRSS.parse open(File.dirname(__FILE__) + '/../data/media_rss.xml')
@atom = SimpleRSS.parse open(File.dirname(__FILE__) + '/../data/atom.xml')
end

Expand All @@ -28,6 +29,26 @@ def test_rss09
assert_equal Time.parse("Fri Sep 09 02:52:31 PDT 2005"), @rss09.channel.dc_date
end

def test_media_rss
assert_equal 20, @media_rss.items.size
assert_equal "Uploads from herval", @media_rss.title
assert_equal "http://www.flickr.com/photos/herval/", @media_rss.channel.link
assert_equal "http://www.flickr.com/photos/herval/4671960608/", @media_rss.items.first.link
assert_equal "http://www.flickr.com/photos/herval/4671960608/", @media_rss.items.first[:link]
assert_equal "http://farm5.static.flickr.com/4040/4671960608_10cb945d5c_o.jpg", @media_rss.items.first.media_content_url
assert_equal "image/jpeg", @media_rss.items.first.media_content_type
assert_equal "3168", @media_rss.items.first.media_content_height
assert_equal "4752", @media_rss.items.first.media_content_width
assert_equal "Woof?", @media_rss.items.first.media_title
assert_equal "http://farm5.static.flickr.com/4040/4671960608_954d2297bc_s.jpg", @media_rss.items.first.media_thumbnail_url
assert_equal "75", @media_rss.items.first.media_thumbnail_height
assert_equal "75", @media_rss.items.first.media_thumbnail_width
assert_equal "herval", @media_rss.items.first.media_credit
assert_equal "photographer", @media_rss.items.first.media_credit_role
assert_equal "pets frodo", @media_rss.items.first.media_category
assert_equal "urn:flickr:tags", @media_rss.items.first.media_category_scheme
end

def test_rss20
assert_equal 10, @rss20.items.size
assert_equal "Technoblog", @rss20.title
Expand Down

0 comments on commit 225a6d6

Please sign in to comment.