From a8ddf7c2824b5be7a40a2d7bb1d07232c4e47f35 Mon Sep 17 00:00:00 2001 From: orangeudav Date: Tue, 24 Jul 2012 10:41:56 +0400 Subject: [PATCH] support for multiple tag category inside each item --- lib/simple-rss.rb | 21 ++++++++++++++------- test/base/base_test.rb | 9 ++++++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/simple-rss.rb b/lib/simple-rss.rb index fc375ab..21bc12d 100644 --- a/lib/simple-rss.rb +++ b/lib/simple-rss.rb @@ -120,13 +120,20 @@ def parse nil end item[clean_tag("#{tag}_#{attrib}")] = clean_content(tag, attrib, $3) if $3 - else - if match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)}mi - nil - elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)/\s*>}mi - nil - end - item[clean_tag(tag)] = clean_content(tag, $2, $3) if $2 || $3 + elsif tag.to_s == 'category' + cats = match[3].scan(%r{<(rss:|atom:)?#{tag}(.*?)>(.*?)}mi).map{|m| m[2]} + unless cats.empty? + item[clean_tag(tag)] = clean_content(tag, nil, cats.last) + item[clean_tag(:categories)] = cats.map{|c| clean_content(tag, nil, c)} + end + else + if match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)}mi + nil + elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)/\s*>}mi + nil + end + + item[clean_tag(tag)] = clean_content(tag, $2, $3) if $2 || $3 end end def item.method_missing(name, *args) self[name] end diff --git a/test/base/base_test.rb b/test/base/base_test.rb index e5a0bcc..87b2c9f 100644 --- a/test/base/base_test.rb +++ b/test/base/base_test.rb @@ -68,5 +68,12 @@ def test_atom def test_bad_feed assert_raise(SimpleRSSError) { SimpleRSS.parse(open(File.dirname(__FILE__) + '/../data/not-rss.xml')) } - end + end + + def test_multi_category + assert_equal "Ruby", @rss20.items.first.category + assert_equal 2, @rss20.items.first.categories.size + assert_equal true, @rss20.items.first.categories.include?("Programming") + assert_equal true, @rss20.items.first.categories.include?("Ruby") + end end \ No newline at end of file