diff --git a/NEWS b/NEWS index d08fa867ae..0b8caca210 100644 --- a/NEWS +++ b/NEWS @@ -176,6 +176,11 @@ with all sufficient information, see the ChangeLog file or Redmine * "name(#{NODE_SET})" case +* RSS + + * RSS::Parser.parse: Accept options as Hash. :validate, + :ignore_unknown_element, :parser_class options are available. + * Set * Aliased methods: diff --git a/lib/rss/parser.rb b/lib/rss/parser.rb index a9842e6d40..e271dc381c 100644 --- a/lib/rss/parser.rb +++ b/lib/rss/parser.rb @@ -72,13 +72,31 @@ module RSS end end - def parse(rss, do_validate=true, ignore_unknown_element=true, - parser_class=default_parser) + def parse(rss, *args) + if args.last.is_a?(Hash) + options = args.pop + else + options = {} + end + do_validate = boolean_argument(args[0], options[:validate], true) + ignore_unknown_element = + boolean_argument(args[1], options[:ignore_unknown_element], true) + parser_class = args[2] || options[:parser_class] || default_parser parser = new(rss, parser_class) parser.do_validate = do_validate parser.ignore_unknown_element = ignore_unknown_element parser.parse end + + private + def boolean_argument(positioned_value, option_value, default) + value = positioned_value + if value.nil? and not option_value.nil? + value = option_value + end + value = default if value.nil? + value + end end def_delegators(:@parser, :parse, :rss, diff --git a/test/rss/test_parser.rb b/test/rss/test_parser.rb index 7d64657d57..4e7cc1b963 100644 --- a/test/rss/test_parser.rb +++ b/test/rss/test_parser.rb @@ -61,5 +61,61 @@ EOR EOR end end + + def test_parse_option_validate_nil + assert_raise(RSS::MissingTagError) do + RSS::Parser.parse(make_RDF(<<-RDF), :validate => nil) + RDF + end + end + + def test_parse_option_validate_true + assert_raise(RSS::MissingTagError) do + RSS::Parser.parse(make_RDF(<<-RDF), :validate => true) + RDF + end + end + + def test_parse_option_validate_false + rdf = RSS::Parser.parse(make_RDF(<<-RDF), :validate => false) + RDF + assert_nil(rdf.channel) + end + + def test_parse_option_ignore_unknown_element_nil + assert_nothing_raised do + RSS::Parser.parse(make_RDF(<<-RDF), :ignore_unknown_element => nil) + +#{make_channel} +#{make_item} +#{make_textinput} +#{make_image} + RDF + end + end + + def test_parse_option_ignore_unknown_element_true + assert_nothing_raised do + RSS::Parser.parse(make_RDF(<<-RDF), :ignore_unknown_element => true) + +#{make_channel} +#{make_item} +#{make_textinput} +#{make_image} + RDF + end + end + + def test_parse_option_ignore_unknown_element_false + assert_raise(RSS::NotExpectedTagError) do + RSS::Parser.parse(make_RDF(<<-RDF), :ignore_unknown_element => false) + +#{make_channel} +#{make_item} +#{make_textinput} +#{make_image} + RDF + end + end end end