Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing multiple tags to be returned as an Array with multiple values if parse is... #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/simple-rss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'time'

class SimpleRSS
VERSION = "1.3.1"
VERSION = "1.3.2"

attr_reader :items, :source
alias :entries :items
Expand Down Expand Up @@ -37,6 +37,8 @@ class SimpleRSS
:'media:category', :'media:category#scheme'
]



def initialize(source, options={})
@source = source.respond_to?(:read) ? source.read : source.to_s
@items = Array.new
Expand Down Expand Up @@ -95,10 +97,14 @@ def parse
end
end

# don't process any ignore_tags passed in through options
item_tags = @@item_tags - (@options[:ignore_tags] || [])
array_tags = @options[:array_tags] || []

# RSS items' title, link, and description
@source.scan( %r{<(rss:|atom:)?(item|entry)([\s][^>]*)?>(.*?)</(rss:|atom:)?(item|entry)>}mi ) do |match|
item = Hash.new
@@item_tags.each do |tag|
item_tags.each do |tag|
if tag.to_s.include?("+")
tag_data = tag.to_s.split("+")
tag = tag_data[0]
Expand All @@ -120,7 +126,14 @@ def parse
nil
end
item[clean_tag("#{tag}_#{attrib}")] = clean_content(tag, attrib, $3) if $3
else
elsif array_tags.index(tag)
# process this tag as an Array (scan!)
vals = []
match[3].scan( %r{<#{tag}.*?>(.*?)</#{tag}>}mi) do |tag_match|
vals << tag_match[0]
end
item[clean_tag(tag)] = vals
else
if match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)>(.*?)</(rss:|atom:)?#{tag}>}mi
nil
elsif match[3] =~ %r{<(rss:|atom:)?#{tag}(.*?)/\s*>}mi
Expand Down
11 changes: 10 additions & 1 deletion test/base/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
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')
@rss20 = SimpleRSS.parse open(File.dirname(__FILE__) + '/../data/rss20.xml'), :array_tags => [:category]
@media_rss = SimpleRSS.parse open(File.dirname(__FILE__) + '/../data/media_rss.xml')
@atom = SimpleRSS.parse open(File.dirname(__FILE__) + '/../data/atom.xml')
@rss20_no_categories = SimpleRSS.parse(open(File.dirname(__FILE__) + '/../data/rss20.xml'), :ignore_tags => [:category])
end

def test_channel
Expand Down Expand Up @@ -56,8 +57,16 @@ def test_rss20
assert_equal "http://feeds.feedburner.com/rufytech?m=68", @rss20.items.first.link
assert_equal "http://feeds.feedburner.com/rufytech?m=68", @rss20.items.first[:link]
assert_equal "This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site.", @rss20.channel.feedburner_browserFriendly
assert_kind_of Array, @rss20.items.first.category
assert_equal 2, @rss20.items.first.category.size # Should be an array
assert_equal ["Programming", "Ruby"], @rss20.items.first.category
end

def test_rss20_no_categories
assert_equal 10, @rss20_no_categories.items.size
assert_nil @rss20_no_categories.items.first.category
end

def test_atom
assert_equal 1, @atom.entries.size
assert_equal "dive into mark", @atom.title
Expand Down
Loading